[git rebase] ブランチにmasterの情報を持ってきて作業するにはどうすればいいの?


やり方や仕組みを記述したちゃんとした記事はたくさんありますのでリンクをご参照ください。

ブランチで作業してるけど、作業中に他のメンバーがマスターにマージしてマスターの情報が
新しくなった!
という時に、作業中の自分のブランチにマスターの最新情報を追加して自分がマージ
した時のコンフリクトを最小限に抑えたい&マージされた内容をもとに実装をしたい
と言ったことを考え使うコマンドがリベースという認識でおります。
(もっと他にあると思いますが、自分なりの解釈です)

環境

■GithubDesktop使用
■ターミナル
■プルリクはしていません(プルリクあげてるものはGithubでプルリクをcloseしています)
プルリクエストをクローズする
■commitはしてます

猿とおんなじレベルの自分でも理解できるように自分なりに噛み砕いて記述します。
実行したこととエラーについて。

Git rebaseの方法(エラーが出る場合)

①自分が作業したいブランチに移動してください。

コマンドでやる方法がありますが、自分はGithubDesktopを使っているので
そちらでブランチ選択をして移動しています。

自分のいるブランチを確認する方法

ターミナル
$ git branch --contains
* ブランチ名

②masterの情報をリベースします

ターミナル
$ git rebase master
ターミナル
#エラー
error: cannot rebase: You have unstaged changes.
error: Please commit or stash them.

commitは全て完了していたので
GithubDesktopでfetch originを行いブランチにマスターの情報が降りている状態にします

再挑戦

ターミナル
$ git rebase master
ターミナル
#エラー

First, rewinding head to replay your work on top of it...
Applying: gretel_gemfile_create
Using index info to reconstruct a base tree...
M   Gemfile
M   Gemfile.lock
M   app/views/items/index.html.haml
Falling back to patching base and 3-way merge.
Auto-merging app/views/items/index.html.haml
Removing app/views/detail/ファイル名.html.haml
Auto-merging Gemfile.lock
CONFLICT (content): Merge conflict in Gemfile.lock
Auto-merging Gemfile
CONFLICT (content): Merge conflict in Gemfile
error: Failed to merge in the changes.
Patch failed at 0001 gretel_gemfile_create
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

詳しく見ていきます

#Mと書いてある部分のファイルに差分があるようです
M   Gemfile
M   Gemfile.lock
M   app/views/items/index.html.haml
#「index」にレンダーしてた「_ファイル名」がremoveされてるで
Auto-merging app/views/items/index.html.haml
Removing app/views/detail/_ファイル名.html.haml
#Gemfile.lockがコンフリクトしてんで
Auto-merging Gemfile.lock
CONFLICT (content): Merge conflict in Gemfile.lock
#Gemfileがコンフリクトしてんで
Auto-merging Gemfile
CONFLICT (content): Merge conflict in Gemfile
#hint:(解決へのヒント)が書いてあります
#今回は手動で全部のコンフリクトをなおしてねと言っております
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
#コンフリクト直したのを
#git rebase --continue".か
#git rebase --skipか
#git rebase --abortのどれかをしてねと出てきます
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

③やったこと

①VSコードを開くとコンフリクト箇所が出てくるので正しいと思う方を選択し、コンフリクトを直していきます。
②直したらGit hubDesktopを開くとこんな表示が出る(Desktop使ってない方はターミナルgit rebase --continuegit rebase --skipgit rebase --abort
どれかを打ち込む)


私はContinue Rebaseを選びました

③GithubDesktopでPushをおす。すると下記が出てくる。

これはブランチに上書いちゃうけど大丈夫なんかい?ってこと(だと思う)ので
今回ブランチは自分しかいじってないのでI'm sureを選択

完了

(fetchとかなんかその辺は心配でやったかもです。)

Git rebaseの方法(エラーが出ない場合)

①自分が作業したいブランチに移動してください。

コマンドでやる方法がありますが、自分はGithubDesktopを使っているので
そちらでブランチ選択をして移動しています。

自分のいるブランチを確認する方法

ターミナル
$ git branch --contains
* ブランチ名

②masterの情報をリベースします

ターミナル
$ git rebase master
#この文字は問題ないようです
First, rewinding head to replay your work on top of it...

③GithubDesktopに移動します

Pullします

Pullだった場所がPushになるので押します

完了

終わりに

今回、自分が行なった方法で解決できた(と思っておりますが)記述や説明の不備があるかと存じます。
ご指摘いただければ幸いです。
参照記事はrebaseのことをもっとしっかり書かれてますので、ぜひ参照くださいませ。

参照記事

git rebaseを初めて使った際のまとめ
git rebase master に失敗した模様のとき【git】