在最近使用 git 的过程当中,有时候遇到这样的一个问题:习惯性的 "add -A",这会将全部的修改都添加到暂存区,但是有两个文件的修改暂时不想添加的呀,这该怎么办?git 提供了一些撤销操做的方法。好比:css
就像前面说的,习惯性的 "add -A" 将暂时不想添加的修改添加到了暂存区。而取消已经暂存的修改的方法,git 已经在你每次使用 git status
查看文件状态的时候给出了解决方案,git
➜ 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
➜ 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
时,一样也给出了具体的撤销方法。code
➜ 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>
来取消工做目录中的修改。jade
➜ 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 了,那就只有哭了……get
有时候在提交时,发现本身漏掉或者多选了几个文件,亦或者提交信息写错了,想要撤销刚才的提交操做,可使用 --amend
这个选项,从新提交scss
➜ 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" 的错误提交信息,也不用太紧张,输入指令it
git commit --amend
以后,会跳转到命令行中的 vim 中,提示你修改提交信息。只要没有推送到远程端,一切都好说~