git pushが弾かれた時にstashを挟まずpullしても平気か検証してみた
チームで開発していると、自分がpushしようとしたタイミングですでに誰かがpush済みで弾かれることがあります。そういう時、周りを見ていると2つのやり方をする人がいます。
一つは、いったん自分の変更をstashで退避してからpullし、stashを戻してpushするやり方です。もう一つは、stashを挟まずにそのままpullしてpushするやり方です。
自分は普段どちらかというと後者(そのままpull)をやってしまうタイプなのですが、「stash挟まなくて大丈夫なの」と聞かれて、答えに詰まりました。実際どちらが安全なのか、手元で本当に衝突を起こしながら確かめてみることにしました。
普段の作業環境はmacOS上のtmuxとzshとneovimですが、今回はコンテナ内のLinux環境でそのまま検証しています。gitのバージョンは以下の通りです。
~/work ❯ git --version
git version 2.47.3二人がpushし合う状況を作る
検証のために、ローカルにbareリポジトリをサーバー役として一つ立て、そこにaliceとbobという二人分の作業ディレクトリからpushとpullをする形にしました。ここは本題ではないので、コマンドだけ簡単に載せておきます。
~/work/gitpush-demo ❯ git init --bare server.git
Initialized empty Git repository in /home/app/work/gitpush-demo/server.git/~/work/gitpush-demo ❯ git clone server.git alice
Cloning into 'alice'...
warning: You appear to have cloned an empty repository.
done.~/work/gitpush-demo ❯ git clone server.git bob
Cloning into 'bob'...
warning: You appear to have cloned an empty repository.
done.aliceが最初のコミットをして、bobがそれをfetchして追いかける形にしました。ここから本題です。
まずbobがfile.txtに1行足してpushします。
~/work/gitpush-demo/bob ❯ echo "bob's line" >> file.txt && git add file.txt && git commit -m "bob adds a line"
[main c55ff61] bob adds a line
1 file changed, 1 insertion(+)~/work/gitpush-demo/bob ❯ git push
To /home/app/work/gitpush-demo/server.git
1560b05..c55ff61 main -> mainaliceはそんなことを知らず、自分の作業を進めていました。新しいファイルを一つコミットしつつ、file.txtには手をつけているけれどまだコミットしていない編集が残っている状態にしました。日々の作業でよくある「pushしたい変更」と「まだ手元に置いている作業中の変更」が同時にある状況です。
~/work/gitpush-demo/alice ❯ echo "alice's own feature" > feature.txt && git add feature.txt && git commit -m "add feature.txt"
[main 41846e1] add feature.txt
1 file changed, 1 insertion(+)
create mode 100644 feature.txt~/work/gitpush-demo/alice ❯ git status --short
M file.txtこの状態でpushします。
~/work/gitpush-demo/alice ❯ git push
To /home/app/work/gitpush-demo/server.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '/home/app/work/gitpush-demo/server.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.これが冒頭の状況です。ここからstashを挟まずにそのままpullするとどうなるか試していきます。
そのままpullするとどうなるか
まず何も考えずにgit pullを叩いてみました。
~/work/gitpush-demo/alice ❯ git pull
From /home/app/work/gitpush-demo/server
1560b05..c55ff61 main -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
fatal: Need to specify how to reconcile divergent branches.これはstashとは関係なく、mergeにするかrebaseにするか設定していないと今のgitは止まる、という話でした。まずはmerge方式(--no-rebase)で試します。file.txtはさっき見た通りまだ未コミットの編集が残っています。
~/work/gitpush-demo/alice ❯ git pull --no-rebase
error: Your local changes to the following files would be overwritten by merge:
file.txt
Please commit your changes or stash them before you merge.
Aborting
Merge with strategy ort failed.pullが失敗しました。ただし作業ツリーは何も壊れていません。
~/work/gitpush-demo/alice ❯ git status --short
M file.txtbobが触ったファイルと自分の未コミットの編集が重なっていると、gitはpullそのものを止めてくれます。これはgit pull固有の挙動ではなく、内部で呼ばれるgit mergeの仕様だと知って、少し驚きました。公式ドキュメントのgit-mergeにはこう書かれていました。
"git pull and git merge will stop without doing anything when local uncommitted changes overlap with files that git pull/git merge may need to update." (出典 https://git-scm.com/docs/git-merge の PRE-MERGE CHECKS)
日本語で言うと、pullやmergeが触ろうとしているファイルと自分の未コミットの編集が重なっている時は、gitは何もせずに止まる、ということです。だから重なっている場合は、stashを挟んでいようがいまいが、そのままgit pullしただけでは何も壊れません。
では重ならない場合はどうでしょうか。さっきのfile.txtの編集を元に戻して、代わりにbobが触っていないfeature.txtの方を編集した状態にしてみます。
~/work/gitpush-demo/alice ❯ git checkout -- file.txt && echo "alice edits her own feature file" >> feature.txt && git status --short
M feature.txt~/work/gitpush-demo/alice ❯ git pull --no-rebase
Merge made by the 'ort' strategy.
file.txt | 1 +
1 file changed, 1 insertion(+)今度はあっさり成功しました。作業ツリーを見ると、未コミットの編集はそのまま残っています。
~/work/gitpush-demo/alice ❯ git status --short
M feature.txtこのままpushもできます。
~/work/gitpush-demo/alice ❯ git push
To /home/app/work/gitpush-demo/server.git
c55ff61..dbccce9 main -> mainmerge方式のpullは、重なっていなければ黙って通してくれて、未コミットの編集には手を付けないまま進みます。ここまでの結果を見て、stashを挟まなくても事故が起きる場面は見当たらないと分かりました。gitが自分でチェックしてくれているからです。
rebase方式だと話が変わる
ここまではpullをmerge方式(pull.rebase=false)でやっていましたが、rebase方式(pull.rebase=trueまたは--rebase)を使っている人も多いと思います。同じように、bobがもう一段file.txtに1行足してpushした後、aliceは今度もbobと重ならないfeature.txtの方に未コミットの編集を残した状態で試してみます。
~/work/gitpush-demo/alice ❯ echo "alice's another wip edit" >> feature.txt && git status --short
M feature.txt~/work/gitpush-demo/alice ❯ git pull --rebase
error: cannot pull with rebase: You have unstaged changes.
error: Please commit or stash them.さっきのmerge方式とは違い、重なっていないのに即座に弾かれました。これは意外でした。rebaseは重なっているかどうかの判定を一切せず、未コミットの変更が少しでも残っていたらそもそも動かないようです。rebase方式でpullしている人にとっては、stashを挟むこと自体がほぼ必須の作業になります。
ちょうどここでgit pull --rebase --autostashというオプションがあることを知ったので試してみました。
~/work/gitpush-demo/alice ❯ git pull --rebase --autostash
From /home/app/work/gitpush-demo/server
dbccce9..ee34e9e main -> origin/main
Updating dbccce9..ee34e9e
Created autostash: 84f0d55
Fast-forward
Applied autostash.内部でstashしてpullしてから、自動で戻してくれています。作業ツリーの編集はちゃんと残っていました。
~/work/gitpush-demo/alice ❯ git status --short
M feature.txtgit-rebaseの公式ドキュメントには、このオプションについてこう書かれています。
"Automatically create a temporary stash entry before the operation begins, and apply it after the operation ends." (出典 https://git-scm.com/docs/git-rebase の --autostash の項目)
要するに、rebase方式の場合は自分で手打ちするか--autostashに任せるかの違いはあっても、結局どこかでstash相当の退避が挟まる、ということでした。
stashを挟む方でも衝突は起きる
ここまでで、そのままpullしても重なっていなければ問題なく、重なっていればgitが止めてくれることは分かりました。では逆に、stashを挟む方は本当に安全なのかも確かめておきます。bobにfile.txtの1行目を書き換えてpushしてもらい、aliceも同じ1行目を編集してからstashしてpullしてみます。
~/work/gitpush-demo/alice ❯ git stash
Saved working directory and index state WIP on main: 9c855d4 bob rewrites line1~/work/gitpush-demo/alice ❯ git pull --no-rebase
From /home/app/work/gitpush-demo/server
9c855d4..231ef8e main -> origin/main
Updating 9c855d4..231ef8e
Fast-forward
file.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)pull自体はきれいに終わります。ここでstashを戻してみます。
~/work/gitpush-demo/alice ❯ git stash pop
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
On branch main
Your branch is up to date with 'origin/main'.
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: file.txt
The stash entry is kept in case you need it again.stashを戻す時にも普通に衝突が起きました。file.txtの中身は他の衝突と同じマーカーが入った状態です。
~/work/gitpush-demo/alice ❯ cat file.txt
<<<<<<< Updated upstream
line1 rewritten by bob again
=======
line1 rewritten by alice concurrently
>>>>>>> Stashed changes
bob's line
bob's second line面白かったのは、衝突してもstashした内容自体は消えずに残っていたことです。
~/work/gitpush-demo/alice ❯ git stash list
stash@{0}: WIP on main: 9c855d4 bob rewrites line1手で直してから、stashを片付けます。
~/work/gitpush-demo/alice ❯ git add file.txt && git stash drop
Dropped refs/stash@{0} (f548f13a7b285bb19996562582e6ebc36962198c)stashを挟んだからといって衝突を避けられるわけではなく、衝突が起きるタイミングがpull時からpop時にずれるだけでした。それでも失敗した時にstashが消えずに残る点は、素直に安心材料だと感じました。
実はこのstashを挟むやり方自体、git-stashの公式ドキュメントのEXAMPLESに「Pulling into a dirty tree」という項目でそのまま載っていました。原文にはこういう例が出ています。
$ git pull
...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop出典は https://git-scm.com/docs/git-stash のEXAMPLESです
作業中に上流の変更を取り込みたいけれど、そのままpullすると衝突して止まってしまう場合に、この手順が紹介されています。裏を返すと、公式ドキュメントの想定でも「衝突する時だけstashを使う」という書き方であって、常にstashを挟むことを義務付けているわけではなさそうでした。
結局そのままpullしてpushして問題ないのか
一通り試してみて、そのままpullするやり方にデータが消えるような意味での危険は無いと分かりました。理由は単純で、pullが触ろうとしているファイルと自分の未コミットの編集が重なっていたら、merge方式のpullはそこで止まってくれますし、rebase方式のpullはそもそも重なっているかどうかに関係なく止まってくれるからです。stashを挟んでいても、戻す時に同じ内容の衝突が起きるので、結局手で直す作業からは逃げられません。
重なっていない時の挙動には違いが出ました。merge方式のpullは重なっていなければ黙って通してくれるので、そのままpullしてpushする方が手数が少なくて楽でした。rebase方式のpullは重なっているかどうかに関わらず未コミットの変更があるだけで止まるので、実質的にstash相当の退避を挟むことになります(手動でやるか--autostashに任せるかの違いだけです)。
聞かれた「stash挟まなくて大丈夫なの」への自分なりの答えは、mergeベースのpull運用なら大抵は大丈夫で、rebaseベースの運用ならどのみちstashされる、というものになりました。自分のリポジトリがどちらの設定になっているかは、以下で確認できます。
~/work/gitpush-demo/alice ❯ git config pull.rebase何も表示されなければmerge方式が既定のままです。手元の環境がどちらなのか、一度確認してみると良さそうです。