撤消操作
在最近使用 git 的過程中,有時候遇到這樣的一個問題:習慣性的 "add -A",這會將所有的修改都添加到暫存區,可是有兩個文件的修改暫時不想添加的呀,這該怎么辦?git 提供了一些撤銷操作的方法。比如:
取消已經暫存的修改
就像前面說的,習慣性的 "add -A" 將暫時不想添加的修改添加到了暫存區。而取消已經暫存的修改的方法,git 已經在你每次使用 git status
查看文件狀態的時候給出了解決方案,
➜ hexo-theme git:(master) ✗ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: processing/README.md
modified: processing/layout/_partial/navigation.jade
modified: processing/layout/_widget/archive.jade
modified: processing/layout/_widget/categories.jade
modified: processing/source/css/_base/base.scss
modified: processing/source/css/_base/variables.scss
modified: processing/source/css/_partial/navigation.scss
modified: processing/source/css/style.scss
可以使用 git reset HEAD
➜ hexo-theme git:(master) ✗ git reset HEAD *
Unstaged changes after reset:
M processing/README.md
M processing/layout/_partial/navigation.jade
M processing/layout/_widget/archive.jade
M processing/layout/_widget/categories.jade
M processing/source/css/_base/base.scss
M processing/source/css/_base/variables.scss
M processing/source/css/_partial/navigation.scss
M processing/source/css/style.scss
這時再使用 git status
查看文件狀態可以看到
➜ hexo-theme git:(master) ✗ git status
On branch master
Your branch is ahead of 'origin/master' by 2 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: processing/README.md
modified: processing/layout/_partial/navigation.jade
modified: processing/layout/_widget/archive.jade
modified: processing/layout/_widget/categories.jade
modified: processing/source/css/_base/base.scss
modified: processing/source/css/_base/variables.scss
modified: processing/source/css/_partial/navigation.scss
modified: processing/source/css/style.scss
no changes added to commit (use "git add" and/or "git commit -a")
可以看到,現在所有的修改都沒有被暫存。
取消對文件的修改
有一次,我正修復一個bug,修改本地倉庫的一個文件,還沒有完成時,同伴告訴我他已經修改好了,並且已經提交到遠程了。我停下手頭的工作,准備 pull ,這時候意識到,如果直接 pull,merge 的時候必然會沖突,因為我和同事同時修改了同一個文件的差不多相同的地方。但是我自己修改了文件很多地方,一味的 CTRL+Z 也難以解決問題,此時我需要將我修改的文件返回到修改之前的狀態。很湊巧的是,在執行 git status
時,同樣也給出了具體的撤銷方法。
➜ hexo-theme git:(master) git status
On branch master
Your branch is up-to-date with 'origin/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)
modified: processing/layout/_widget/archive.jade
modified: processing/layout/_widget/categories.jade
modified: processing/layout/_widget/tags.jade
modified: processing/layout/layout.jade
modified: processing/source/css/_base/base.scss
no changes added to commit (use "git add" and/or "git commit -a")
使用 git checkout -- <filename>
來取消工作目錄中的修改。
➜ hexo-theme git:(master) ✗ git checkout -- *
➜ hexo-theme git:(master) git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
效果顯而易見!!這條命令有些危險,所有對文件的修改都沒有了。如果一個不小心將自己需要的修改 discard 了,那就只有哭了……
修改最后一次的提交
有時候在提交時,發現自己漏掉或者多選了幾個文件,亦或者提交信息寫錯了,想要撤銷剛才的提交操作,可以使用 --amend
這個選項,重新提交
➜ hexo-theme git:(master) ✗ git add -A
➜ hexo-theme git:(master) ✗ git commit -m "commit wrong"
[master ebcbab2] commit wrong
4 files changed, 78 insertions(+), 106 deletions(-)
rewrite processing/source/css/_base/base.scss (64%)
發現自己提交了一個 "commit wrong" 的錯誤提交信息,也不用太緊張,輸入指令
git commit --amend
之后,會跳轉到命令行中的 vim 中,提示你修改提交信息。只要沒有推送到遠程端,一切都好說~