【コンフリクト】git push origin ~~の失敗(non-fast-forward)、コンフリクト解消、プッシュに至るまで


問題

masterから切ったブランチでの作業が終わり、プッシュしようとすると、


$ git push origin ブランチ名
To bitbucket.org:〜〜〜.git
 ! [rejected]        ブランチ名 -> ブランチ名 (non-fast-forward)
error: failed to push some refs to '[email protected]:〜〜〜.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 pushがreject(拒否)されたときの対処法
リモートとローカルの最新情報が異なっている事が原因であるとわかる。

解決へ向けて、、

$ git pull origin 〇〇

を行う。
すると、


From bitbucket.org:〜〜〜/アプリ名
 * branch            ブランチ名     -> FETCH_HEAD
Auto-merging spec/spec_helper.rb
CONFLICT (content): Merge conflict in spec/spec_helper.rb
CONFLICT (add/add): Merge conflict in 
 (中略)
Auto-merging Gemfile.lock
Auto-merging Gemfile
CONFLICT (content): Merge conflict in Gemfile
Auto-merging .gitignore
CONFLICT (content): Merge conflict in .gitignore
Automatic merge failed; fix conflicts and then commit the result.

と、人生初のコンフリクト発生。

Git コンフリクト解消手順

こちらの記事を参考に、git status で状況を確認。


$git status
On branch ブランチ名
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
Unmerged paths:
  (use "git add <file>..." to mark resolution)

 >   ※ここにコンフリクトが発生したファイルが一覧で表示される。

VScodeを確認すると、該当のファイルのコンフリクトが起きている箇所に、ローカルとリモートどちらの変更を適用するか選択するボタンが表示されている為、順番に確認しながらコンフリクトを解消していく。

ちなみに、コンフリクトした状態を一旦もとに戻したい場合は、

$ git merge --abort

終わりに

転職の為、未経験の状態からRailsを学習しております。正しい知識を着実に身に着け、実力のあるエンジニアになりたいと考えています。継続して投稿していく中で、その為のインプットも必然的に増え、成長に繋がるかと考えています。
今現在、初心者だからといって言い訳はできないですが、投稿の内容に間違っているところや、付け加えるべきところが多々あるかと思いますので、ご指摘頂けると幸いです。この記事を読んで下さりありがとうございます。