介紹一些基本的撤銷命令
- 提交說明修改
- 同個commit添加遺漏文件
- 取消已暫存的文件
- 取消工作目錄已修改的文件
請注意,有些撤銷操作是不可逆的汁果,所以請務必謹慎小心,一旦失誤鳄乏,就有可能丟失部分工作成果。
修改最后一次提交
在提交完后發(fā)現(xiàn)有漏掉的文件沒加或是提交信息寫錯汞窗,需要撤銷剛才的操作可使用--amend
選項重新提交
修改commit(提交)說明
# 修改提交內(nèi)容 提交快照不會改變
$ git commit --amend
啟動文本編輯器后仲吏,會看到上次提交時的說明蝌焚,編輯它確認沒問題后保存退出,就會使用新的提交說明重寫(覆蓋)剛才失誤的提交许帐。
[圖片上傳失敗...(image-aa9160-1513738059742)]
添加遺漏文件
# 增加遺漏文件
$ git add c.py #添加遺漏文件
$ git commit --amend #修改最后一次提交內(nèi)容
[圖片上傳失敗...(image-1c4380-1513738059742)]
修改完內(nèi)容后查看狀態(tài)毕谴,兩次add的文件在一次commit里了。
$ git log -2
commit 94bd713367061b415e3c1c132b0e8d50126d6c70 (HEAD -> master)
Author: yin <yjd@zhuming.com>
Date: Wed Dec 20 09:50:20 2017 +0800
fix c
amend
fix b
撤銷文件修改
撤銷已暫存文件
git reset HEAD file_name
該如何撤消暫存其中的一個文件循帐。其實舀武,git status
的命令輸出已經(jīng)告訴我們了。
$ git add .
$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: b.py
就在 “Changes to be committed” 下面瘪匿,括號中有提示棋弥,可以使用 git reset HEAD <file>...
的方式取消暫存。好吧嘁锯,我們來試試取消暫存 b.py 文件:
$ git reset HEAD b.py
Unstaged changes after reset:
M b.py
$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
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)
modified: b.py
no changes added to commit (use "git add" and/or "git commit -a")
現(xiàn)在b.py文件又回到了之前的已修改未暫存的狀態(tài)
撤銷對文件的修改
git checkout file_name
如果覺得對某個文件的修改完全沒必要(要恢復到修改前)家乘。
git status
同樣提示了具體的撤銷方法
# git status 一部分內(nèi)容
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)
modified: b.py
# 撤銷文件b.py的修改
$ git checkout b.py
$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
可以看到藏澳,該文件已經(jīng)恢復到修改前的版本。你可能已經(jīng)意識到了业崖,這條命令有些危險,所有對文件的修改都沒有了双炕,因為我們剛剛把之前版本的文件復制過來重寫了此文件。所以在用這條命令前摇锋,請務必確定真的不再需要保留剛才的修改站超。