2012/06/19

gitでremoteのコミットをなかったことにしたい

今日会社で困ったのでメモ

目的

remoteのコミットをなかったことにしたい

参考にしたのはこちら

基本的に内容は同じ。

シチュエーション

  • 大幅修正
  • commit
  • commit
  • push
  • やっぱ大幅修正前に戻したい
  • コミットもなかったことにしたい

シナリオ

  • ローカルでreset --hard
  • remoteのbranch消す
  • ローカルの状態をpush

方法

取りあえずremoteはoriginでbranchはmasterということで。


branchのバックアップを取る
% git push origin master:master_bak
ローカルのcommitをなかったことにする
% git reset --hard xxxxxxxxxxxx
gitのremote branchを削除
% git push origin :master
ローカルのmasterをpush
% git push origin master

別解

なんだこれでいけるのか。
% git push -f origin xxxxxxxxxxxxx:master

ところで
% git push origin master:master_bak
これでremoteのbranchのバックアップが取れるとは。(というかremoteのbranchを切れるわけか)
remoteで作ったブランチを手元に持ってくるには
% git checkout -b master_bak origin/master_bak
でいいんだな。

それと、
% git push origin :master
のとこで
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
みたいなメッセージが出る。その時はremoteのconfigに
[receive]
     denyDeleteCurrent = false
を追加してやるといいっぽい。

試しにやってみるとこんな感じ。
valvallow@ubuntu ~ % cd temp
valvallow@ubuntu ~/temp % mkdir -p playground/{repo,local}
valvallow@ubuntu ~/temp/playground/repo
 % git init --bare
Initialized empty Git repository in /home/valvallow/temp/playground/repo/
valvallow@ubuntu ~/temp/playground/repo
 % cd ../local
valvallow@ubuntu ~/temp/playground/local
 % git init
Initialized empty Git repository in /home/valvallow/temp/playground/local/.git/
valvallow@ubuntu ~/temp/playground/local
 % touch hoge
valvallow@ubuntu ~/temp/playground/local
 % echo 'Hello, world !!' > hoge
valvallow@ubuntu ~/temp/playground/local
 % git add .
valvallow@ubuntu ~/temp/playground/local
 % git commit -m 'initial commit'
[master (root-commit) 1876f96] initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hoge
valvallow@ubuntu ~/temp/playground/local
 % echo 'add new row' >> hoge
valvallow@ubuntu ~/temp/playground/local
 % git add .
valvallow@ubuntu ~/temp/playground/local
 % git commit -m 'add new row'
[master e532dc4] add new row
 1 files changed, 1 insertions(+), 0 deletions(-)
valvallow@ubuntu ~/temp/playground/local
 % echo 'piyo' >> hoge
valvallow@ubuntu ~/temp/playground/local
 % git add .
valvallow@ubuntu ~/temp/playground/local
 % git commit -m 'piyo'
[master 5e0355c] piyo
 1 files changed, 1 insertions(+), 0 deletions(-)
valvallow@ubuntu ~/temp/playground/local
 % git remote add origin ../repo
valvallow@ubuntu ~/temp/playground/local
 % git push origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 688 bytes, done.
Total 9 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
To ../repo
 * [new branch]      master -> master
valvallow@ubuntu ~/temp/playground/local
 % git branch -a
* master
  remotes/origin/master
valvallow@ubuntu ~/temp/playground/local
 % git push origin master:master_bak
Total 0 (delta 0), reused 0 (delta 0)
To ../repo
 * [new branch]      master -> master_bak
valvallow@ubuntu ~/temp/playground/local
 % git branch -a
* master
  remotes/origin/master
  remotes/origin/master_bak
valvallow@ubuntu ~/temp/playground/local
 % git branch
* master
valvallow@ubuntu ~/temp/playground/local
 % git checkout -b master_bak origin/master_bak
Branch master_bak set up to track remote branch master_bak from origin.
Switched to a new branch 'master_bak'
valvallow@ubuntu ~/temp/playground/local
 % git branch
  master
* master_bak
valvallow@ubuntu ~/temp/playground/local
 % cat hoge
Hello, world !!
add new row
piyo
valvallow@ubuntu ~/temp/playground/local
 % git push origin :master
To ../repo
 - [deleted]         master
valvallow@ubuntu ~/temp/playground/local
 % git log
commit 5e0355cb325a8ed90fe066131c72dead9faba329
Author: valvallow
Date:   Tue Jun 19 22:23:50 2012 +0900

    piyo

commit e532dc4dc21f72292e2a700e28ed544d31c21a31
Author: valvallow
Date:   Tue Jun 19 22:23:17 2012 +0900

    add new row

commit 1876f96ff3186b5082c9c32e4ed310ac8d4b8a9a
Author: valvallow
Date:   Tue Jun 19 22:22:39 2012 +0900

    initial commit
valvallow@ubuntu ~/temp/playground/local
 % git reset --hard e532dc4dc21f72292e2a700e28ed544d31c21a31
HEAD is now at e532dc4 add new row
valvallow@ubuntu ~/temp/playground/local
 % git log
commit e532dc4dc21f72292e2a700e28ed544d31c21a31
Author: valvallow
Date:   Tue Jun 19 22:23:17 2012 +0900

    add new row

commit 1876f96ff3186b5082c9c32e4ed310ac8d4b8a9a
Author: valvallow
Date:   Tue Jun 19 22:22:39 2012 +0900

    initial commit
valvallow@ubuntu ~/temp/playground/local
 % git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To ../repo
 * [new branch]      master -> master
valvallow@ubuntu ~/temp/playground/local
 % git log
commit e532dc4dc21f72292e2a700e28ed544d31c21a31
Author: valvallow
Date:   Tue Jun 19 22:23:17 2012 +0900

    add new row

commit 1876f96ff3186b5082c9c32e4ed310ac8d4b8a9a
Author: valvallow
Date:   Tue Jun 19 22:22:39 2012 +0900

    initial commit

0 件のコメント:

コメントを投稿