ぼくの技術日誌

日誌って銘打っていますが、更新頻度が…

Gitのブランチを上書きする方法

Gitを開発に利用する途中、git pushするとあるブランチ(uartブランチ)が

 ! [rejected]        uart -> uart (non-fast-forward)

といわれるようになりました。
調べてみると、リモートから取得した内容に変更を加えてpushしようとしたが、他からの変更が先にpushされたことが原因とのことです。
要は競合したということでしょうか。

他のブランチで作業を進めるようになり、放置していたのですが、上書きして再利用することを思いつきました。
ブランチの上書きは現在のブランチでgit branchに-fを付けて上書き対象のブランチを指定することでできるようです。
実際にやってみました。

yosuke@yosuke-vb:~/bitbucket/stellaris$ git branch -a
  master
* sandbox
  uart
  remotes/origin/master
  remotes/origin/sandbox
  remotes/origin/uart
yosuke@yosuke-vb:~/bitbucket/stellaris$ branch -f uart
yosuke@yosuke-vb:~/bitbucket/stellaris$ git checkout sandbox
yosuke@yosuke-vb:~/bitbucket/stellaris$ git push origin uart
Password for 'https://yosuke_kirihata@bitbucket.org': 
remote: bb/acl: yosuke_kirihata is allowed. accepted payload.
To https://yosuke_kirihata@bitbucket.org/yosuke_kirihata/stellaris.git
   9bbc3dc..cd46432  uart -> uart

sandboxブランチの中身をuartブランチに上書きをしたいので、sandbox上でgit branch -fを実行します。
checkoutでブランチを移動し、(中身が上書きされたことを確認して)pushでリモートへ結果を反映します。
以上で終了です。


http://linux.keicode.com/prog/git-resolve-non-fast-forward-push-problem.php

http://transitive.info/article/git/command/branch/