初めてぐらいの勢いで特定のブランチにマージする業務を行ったときのメモ


自分が送ったコミットを特定のブランチ(今回はstaging)にマージしてくれと言われて対応した時のメモ。

まずこうやってリポジトリを clone して hub am −3 コマンドを使ってマージしようとした。


git clone git@github.com:(team name)/(repository name).git
cd (repository name)
git checkout staging
hub am -3 https://github.com/(team name)/(repository name)/pull/(pull number)

認証失敗


そしたら hub am -3 コマンドが認証失敗になりうまく動かない。


$ hub am -3 https://github.com/(team name)/(repository name)/pull/(pull number)
Error getting pull request: Unauthorized (HTTP 401)
Bad credentials

色々調べたけど結論としては ~/.config/hub に書いてある認証情報が古かったので消すと認証の再要求が発生して解消した


$ rm ~/.config/hub
$ hub am -3 https://github.com/(team name)/(repository name)/pull/(pull number)
github.com username:

hub am -3 コマンドでマージしようとしたらコンフリクト



$ hub am -3 https://github.com/(team name)/(repository name)/pull/(pull number)
Applying: (this is a comment of commit)
Using index info to reconstruct a base tree...
M circle.yml
Falling back to patching base and 3-way merge...
Auto-merging circle.yml
CONFLICT (content): Merge conflict in circle.yml
error: Failed to merge in the changes.
Patch failed at 0001 (this is a comment of commit)
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

circle.yml を確認すると確かにコンフリクトが発生していた。
また、メッセージを見ると .git/rebase-apply/patch に適用できなかったパッチが存在するようだ。

以下のようなページを拝見して自分なりに考え、

git amでコンフリクトしたときの対処 - Qiita https://qiita.com/maueki/items/c8476908f8a8601365a5

手動で解消してadd した。


vim circle.yml
add circle.yml

しかしパッチの内容を見ると、コンフリクトを手動で解消した時点で適用が不要になっていた。
同僚エンジニアに相談してみたところ「パッチは消せばいいんじゃね?」とのことなのでパッチ自体を削除してみた


rm .git/rebase-apply/patch

じゃあこれでcontinueすればいいのかな?ということで git am --continue を実行してみたが…


$ git am --continue
Applying: (this is a comment of commit) No changes - did you forget to use 'git add'? If there is nothing left to stage, chances are that something else already introduced the same changes; you might want to skip this patch. When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

No changes と言われてしまう。
git status で確認してみると確かに


$ git status
On branch staging
Your branch is up to date with 'origin/staging'.

You are in the middle of an am session.
  (fix conflicts and then run "git am --continue")
  (use "git am --skip" to skip this patch)
  (use "git am --abort" to restore the original branch)

nothing to commit, working tree clean

ブランチは clean だけど?と怪訝な顔をされてしまう。

コミットログを見ると確かにもうすでにstagingブランチにプルリクエストに含まれるコミットが適用されていた。


git log

じゃあ次だ!といことで、次のプルリクエストをマージしようとしたら…


$ hub am -3 https://github.com/(team name)/(repository name)/pull/(pull request next numbert)
fatal: previous rebase directory .git/rebase-apply still exists but mbox given.

なるほど、.git/rebase-apply が存在するかどうかを確認しているのねあなたは。
そして私がとった行動は…


$ hub am --skip
$ hub am -3 https://github.com/(team name)/(repository name)/pull/(pull request next numbert)
Applying: (this is a comment of commit)
Using index info to reconstruct a base tree...
M circle.yml
Falling back to patching base and 3-way merge...
Auto-merging circle.yml
CONFLICT (content): Merge conflict in circle.yml
error: Failed to merge in the changes.
Patch failed at 0001 (this is a comment of commit)
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

俺たちの戦いはこれからだ!

まとめ


  • hubコマンドをhomebrewでインストールして使おう
  • hub am コマンドで認証失敗が発生する場合は ~/.config/hub の情報がおかしい可能性がある
  • hub am コマンドは git-am - Apply a series of patches from a mailbox のラッパであり git commit --amend のことではない
  • hub am -3 の -3 は 3-way merge を意味している
  • 3-way merge は以下のような情報を参考にあとで学ぶ
  • gitのマージとコンフリクトを理解する - Qiita https://qiita.com/te2u/items/c23f82ec84cf65564554
  • 3ウェイマージというマージアルゴリズム: プログラマの思索 http://forza.cocolog-nifty.com/blog/2012/06/3-a4c2.html
  • ~/.config/hub の情報がおかしい場合は一旦削除して再作成させよう
  • コンフリクトが起こったらまずは頑張って解消する(手で)
  • 適用できなかったパッチが .git/rebase-apply/patch にあるので適用すべきか確認する
  • hub am を一度実行したら --skip するか --continue するか --abort しないと次の hub am は実行できない(.git/rebase-apply を消せできるだろうけど)


コメント

このブログの人気の投稿

オライリーのDocker本を読んだり色々な記事を読んだりしたので自分なりにまとめる(Dockerfileについて)

一部のユーザだけ NET::ERR_CERT_REVOKED でサイトにアクセスできない

[#CentOS][#Apache] CentOS6 でApacheが起動できない