1. 程式人生 > >git 撤銷已經push上去的操作

git 撤銷已經push上去的操作

今天 github 上操作遇到了一個很頭疼的問題,在某個檔案中進行了誤操作(有可能是加入了BOM頭),diff 沒看到差異,但是線上就是不正常顯示。修改半天沒用,只要回退版本。

通過 git log 可以檢視近期 commit 的資訊:

commit bcdfd65ba3f16a0647e7687f92cca25d51738d2e
Author: Barret Lee <[email protected]>
Date:   Mon Apr 28 01:22:27 2014 +0800

    now post

commit 69eeaa60c5808c143aabce4d52feb104e2e4591b
Author: Barret <
[email protected]
> Date: Sat Apr 26 21:03:48 2014 +0800 fix bug commit b9e4b8c697d139ec35e17be7dd353f1338e9b92e Author: Jing Lee <[email protected]> Date: Sat Apr 26 19:35:37 2014 +0800 Update atom.xml

commit 後面的一串字元就是 SHA 字元。

git reset --hard SHA  

這句命令可以回退到指定版本。不過上傳的時候要注意了,如果你是:

git push -u master origin

肯定會出現這樣的錯誤提示:

error: failed to push some refs to 'https://github.com/barretlee/barretlee.github.io.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因是你本地版本要落後於伺服器上的版本(git reset 回退了嘛),如果想覆蓋伺服器上版本,應該加 -f ,強制提交,

git push -u master origin -f

不過這樣的操作要謹慎了,先把修改的位置備份(拿出來,複製到資料夾外),完成上述操作之後再複製回來處理。 (完)