[Git] git push が non-fast-forward で reject された場合の対処

分からない点も含め memo.

[markdown]
## non-fast-forward の状況と対処

図解がわかりやすいこちらで勉強させていただきました。

> * [【git】git pushがrejectされたときの対応方法 at softelメモ](http://www.softel.co.jp/blogs/tech/archives/3569)
> * [Non-Fast-Forward Push の解決 – Linux 入門](http://linux.keicode.com/prog/git-resolve-non-fast-forward-push-problem.php)

以下、一般的な状況と対処法になると思われる。

* トピックブランチで開発を進める。
* その間に master に更新が加わっていた。

### master に更新分を持ってくる

本家リポジトリの更新を ローカルの master に反映する。

“`prettyprinted
% git checkout master
A Casks/omnioutliner-professional3.rb
Switched to branch ‘master’
Your branch is up-to-date with ‘origin/master’.
% git fetch upstream
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 2), reused 2 (delta 0)
Unpacking objects: 100% (4/4), done.
From github.com:caskroom/homebrew-versions
a2f1856..d9e546d master -> upstream/master
% git merge upstream/master
Updating a2f1856..d9e546d
Fast-forward
Casks/omnigraffle-professional5.rb | 7 +++++++
Casks/omniplan1.rb | 7 +++++++
2 files changed, 14 insertions(+)
create mode 100644 Casks/omnigraffle-professional5.rb
create mode 100644 Casks/omniplan1.rb
“`

同期した内容を Fork したリモートへ push する。

“`prettyprinted
% git push origin master
Counting objects: 10, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 648 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@github.com:DriftwoodJP/homebrew-versions.git
a2f1856..d9e546d master -> master
“`

この状態で non-fast-forward となっている。

2014-04-07_git_01

diff をみると更新分の差分が表示される。

“`prettyprinted
% git diff master add_omnioutliner-Professional3
diff –git a/Casks/omnigraffle-professional5.rb b/Casks/omnigraffle-professional
deleted file mode 100644
index e9a123d..0000000
— a/Casks/omnigraffle-professional5.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-class OmnigraffleProfessional5 < Cask - url 'http://www.omnigroup.com/ftp/pub/software/MacOSX/10.6/OmniGrafflePro-5.4 - homepage 'http://www.omnigroup.com/products/omnigraffle' - version '5.4.4' - sha256 '396a4adaadb82d651ea6ec8b141c9e5f5b0b5f53c6edee5e7806a896783892c9' - link 'OmniGraffle Professional 5.app' -end diff --git a/Casks/omnioutliner-professional3.rb b/Casks/omnioutliner-profession new file mode 100644 index 0000000..68136e2 --- /dev/null +++ b/Casks/omnioutliner-professional3.rb @@ -0,0 +1,7 @@ +class OmnioutlinerProfessional3 < Cask + url 'http://www.omnigroup.com/ftp/pub/software/MacOSX/10.4/OmniOutlinerPro-3. + homepage 'http://www.omnigroup.com/omnioutliner/' + version '3.10.6' ``` ### リベースを行い non-fast-forward 状態を解消する `add_omnioutliner-Professional3` に移動。 ```prettyprinted % git rebase -i master Successfully rebased and updated refs/heads/add_omnioutliner-Professional3. ``` diff で変更箇所を確認。 ```prettyprinted % git diff master add_omnioutliner-Professional3 diff --git a/Casks/omnioutliner-professional3.rb b/Casks/omnioutliner-profession new file mode 100644 index 0000000..68136e2 --- /dev/null +++ b/Casks/omnioutliner-professional3.rb @@ -0,0 +1,7 @@ +class OmnioutlinerProfessional3 < Cask + url 'http://www.omnigroup.com/ftp/pub/software/MacOSX/10.4/OmniOutlinerPro-3. + homepage 'http://www.omnigroup.com/omnioutliner/' + version '3.10.6' + sha256 'ec8ee1ec00870a1cc5920d2e899cd255eef6564c8e9944eadfdf5297f1cb348e' + link 'OmniOutliner Professional.app' +end ``` push が無事受け付けられた。 ```prettyprinted % git push origin add_omnioutliner-Professional3 Counting objects: 6, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 573 bytes | 0 bytes/s, done. Total 4 (delta 2), reused 0 (delta 0) To git@github.com:DriftwoodJP/homebrew-versions.git * [new branch] add_omnioutliner-Professional3 -> add_omnioutliner-Professional3
“`

## use “git branch –unset-upstream” to fixup といわれるケース

`git branch –unset-upstream` 後、上記と同じ手順で問題なくなる。

“`prettyprinted
% git checkout add_omnigraffle-pro5_spike
Switched to branch ‘add_omnigraffle-pro5_spike’
Your branch is based on ‘origin/add_omnigraffle-pro5_spike’, but the upstream is gone.
(use “git branch –unset-upstream” to fixup)
% git branch –unset-upstream
“`

`git push -u` の振る舞いがよく理解できていない。

## pull request したブランチをさらに更新するケース

修正を求められたので変更後、コミットをまとめ直すケース 。

> * [Squash Commits with Git](http://davidwalsh.name/squash-commits-git)

“`prettyprinted
% git rebase -i master
[detached HEAD 0be59a7] added omnioutliner-professional3
1 file changed, 7 insertions(+)
create mode 100644 Casks/omnioutliner-professional3.rb
Successfully rebased and updated refs/heads/add_omnioutliner-Professional3.
“`

すると non-fast-forward な状況が生まれる。

2014-04-07_git_02

リモートに push できない。

“`prettyprinted
% git push origin add_omnioutliner-Professional3
To git@github.com:DriftwoodJP/homebrew-versions.git
! [rejected] add_omnioutliner-Professional3 -> add_omnioutliner-Professional3 (non-fast-forward)
error: failed to push some refs to ‘git@github.com:DriftwoodJP/homebrew-versions.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
“`

`git diff` で問題がないことを確認して、force push し直した。
以下、ログを取り忘れたので曖昧な記憶。

“`prettyprinted
% git rebase -i add_omnioutliner-Professional3
Successfully rebased and updated refs/heads/add_omnioutliner-Professional3.
“`

`noop` と表示されたのでそのまま終了したような記憶。
no operation ということか。

“`prettyprinted
% git push -f origin add_omnioutliner-Professional3
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 368 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
To git@github.com:DriftwoodJP/homebrew-versions.git
+ 7a0e4fd…51919d9 add_omnioutliner-Professional3 -> add_omnioutliner-Professional3 (forced update)
“`

> * [Avoiding Git Disasters: A Gory Story | RandyFay.com](http://www.randyfay.com/node/89)
> * [Dealing with non-fast-forward errors · GitHub Help](https://help.github.com/articles/dealing-with-non-fast-forward-errors)

どういったときに force push してはいけないかなど判断するための理解が不足している。

> * [Git – リベース](http://git-scm.com/book/ja/Git-%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E6%A9%9F%E8%83%BD-%E3%83%AA%E3%83%99%E3%83%BC%E3%82%B9)
> * [Git – プロジェクトへの貢献](http://git-scm.com/book/ja/Git-%E3%81%A7%E3%81%AE%E5%88%86%E6%95%A3%E4%BD%9C%E6%A5%AD-%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%B8%E3%81%AE%E8%B2%A2%E7%8C%AE)

## 補遺

> * [Git – ブランチとマージの基本](http://git-scm.com/book/ja/Git-%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E6%A9%9F%E8%83%BD-%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%81%A8%E3%83%9E%E3%83%BC%E3%82%B8%E3%81%AE%E5%9F%BA%E6%9C%AC)
> * [【git】gitのアレコレ、勉強メモ大開放 – Qiita](http://qiita.com/mizzwithliam/items/b7ad79548baf4eb0370b#3-4)
[/markdown]