ぼくの技術日誌

日誌って銘打っていますが、更新頻度が…

Gitのリモートリポジトリへ誤ってgit pushした場合の取り消す方法

本日、Gitでmasterブランチを作業ブランチに切り替えることを忘れてそのままcommit、pushをしてしまいました…
急ぎ修正の方法を調べて治したのですが、今後もやってしまいそうなので作業内容をメモしておこうと思います。


先ず、git log で戻したいcommitのハッシュ値を調べます。

yosuke@ubuntu:~/bitbucket/stellaris$ git log
commit 666ce6c28a3e87ec8d4cc9f075436b9d876dcb30
Author: yosuke kirihata <yosuke@ubuntu.(none)>
Date:   Mon Mar 18 18:56:45 2013 +0900

    Mod start.S and vector.c.

commit 664ab6180dc7c37c2b073119556f590e68fbb58b
Author: Yosuke Kirihata <yosuke@ubuntu.(none)>
Date:   Thu Mar 14 21:06:15 2013 +0900

    First commit.

今回はcommit 664ab6180dc7c37c2b073119556f590e68fbb58bに戻したいと思います。
git branch でブランチの一覧を確認します(-aでリモートとローカル両方のブランチを表示)。

yosuke@ubuntu:~/bitbucket/stellaris$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/sandbox

git reset [hash]でブランチを戻します。

yosuke@ubuntu:~/bitbucket/stellaris$ git reset 664ab6180dc7c37c2b073119556f590e68fbb58b
Unstaged changes after reset:
M	startup.S
M	vector.c
yosuke@ubuntu:~/bitbucket/stellaris$ git log
commit 664ab6180dc7c37c2b073119556f590e68fbb58b
Author: Yosuke Kirihata <yosuke@ubuntu.(none)>
Date:   Thu Mar 14 21:06:15 2013 +0900

    First commit.

checkoutでファイルも前の状態に巻き戻します。

yosuke@ubuntu:~/bitbucket/stellaris$ git checkout -f
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.

リポジトリの状態を見てみます。

yosuke@ubuntu:~/bitbucket/stellaris$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#
nothing to commit (working directory clean)

最後に巻き戻した内容でリモートリポジトリへpushします。

yosuke@ubuntu:~/bitbucket/stellaris$ git push origin master
To git@bitbucket.org:yosuke_kirihata/stellaris.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:yosuke_kirihata/stellaris.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

しかし、ただpushするだけでは失敗してしまいます。
リモートブランチを削除したうえで改めてpushする必要があるとのことです。

yosuke@ubuntu:~/bitbucket/stellaris$ git push origin:master
ssh: Could not resolve hostname origin: Name or service not known
fatal: The remote end hung up unexpectedly

これで戻るはずが、なぜかsshで名前を解決できないようで失敗してしまいます…


正しいコマンドは

$ git push origin :master

だったらしいです。originの後にスペーズがありませんでした…


エラーメッセージやssh関係で検索してみましたが通らないので、別のWebページに載っていたgit push origin +masterを実行してみました。

yosuke@ubuntu:~/bitbucket/stellaris$ git push origin +master
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: yosuke_kirihata is allowed. accepted payload.
To git@bitbucket.org:yosuke_kirihata/stellaris.git
 + 666ce6c...664ab61 master -> master (forced update)

これで無事巻き戻すことができました!
(終わり良ければすべて良しということで)めでたしめだたし。

今回の方法は目についた記事に習って作業をしただけなので、もっと違う方法や良い方法があれば知りたいところです。
でもそのためにはGitを深く理解する必要がありそうです。


参考リンク

http://hylom.net/2011/03/01/how-to-reset-remote-git-branch/

http://blog.toshimaru.net/git-pushgithub