weblog of key_amb

主にIT関連の技術メモ

git push する前に stash して pull --rebase してくれるシェルスクリプト書いた

git push しようとして陥りがちなパターン:

  • remote branch が更新されてて push がこける
  • local 差分があるため git pull --rebase できない

こういうケースでイライラしないために、 git stash && git pull --rebase && git push && git stash pop みたいなことをやってくれるシェルスクリプトを書いた。

https://github.com/key-amb/bash-git-push-carefully

ちょっとしたデモ

こんな感じ:

## (1) 下準備
# 最新状態から1個巻き戻しておく
% bash-git-push-carefully (master) % git reset HEAD~ --hard
HEAD is now at 932ca7b First commit

# ワーキングツリーに変更を加える
% bash-git-push-carefully (master) % touch foo
% bash-git-push-carefully (master) % git add .
% bash-git-push-carefully (master +) % git commit -m "add foo"
[master 4ac0674] add foo
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
% bash-git-push-carefully (master) % git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

## (2) 実行
% bash-git-push-carefully (master) % git-push-carefully origin master
[START]
No local changes to save
First, rewinding head to replay your work on top of it...
Applying: add foo
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:key-amb/bash-git-push-carefully.git
   58889b3..096b538  master -> master
No stash found.
[END] Finished.

スクリプトは若干頑張って書いた感じになっているが、冒頭のワンライナーのような形でもよかったような気がしないでもない。