ゆるこあプログラミング

新卒社員がプログラミングとたたかう

ある時点のcommitの状態に戻す

特定のSHAを指定して,その時までローカルを戻す

git logなどすると,過去のcommit情報が見えるので
任意の時点のものを選びます.

$ git revert <対象のSHA>
[master 8c62542] Revert "latest_130820_1052"
 9 files changed, 28 insertions(+), 617 deletions(-)
 delete mode 100644 hoge.txt

もしコンフリクトしてしまった場合…

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
#	modified:   hoge_dir (modified content)
#
no changes added to commit (use "git add" and/or "git commit -a")

対象のディレクトリに移動してからcheckoutすると治ります.

$ cd hoge_dir
$ git checkout .
$ cd
$ git status
# On branch master
nothing to commit, working directory clean
ローカルでrevertした変更を,リモートにも反映させる

さて,そのあとリモートにpushしようとしたら
最新のcommitとコンフリクトしてしまいました.

$ git push origin master
To git@***.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@***.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

ふむ.
エラーでぐぐると下記のような方法が見つかりますが,

【git】git pushがrejectされたときの対応方法 at softelメモ
http://www.softel.co.jp/blogs/tech/archives/3569

今回はリモートの最新のcommitの状態をpullしたくないので

強制的にpushします.

$ git push --force origin master
Counting objects: 51, done.
Compressing objects: 100% (26/26), done.
Writing objects: 100% (28/28), 3.23 KiB, done.
Total 28 (delta 15), reused 1 (delta 0)
To git@***.git
 + 56d0ff4...8c62542 master -> master (forced update)

リモートで確認して,反映されていればおっけーです.