2016-10-09 Git 基础(四) - 撤销操作

情形一:提交完之后发现漏掉了几个文件没有添加,或者提交信息写错了。

使用命令:git commit --amend

git commit -m 'first commit'
git add forgotten_file
git commit --amend

情形二: 取消暂存的文件

使用命令:git reset HEAD <file>

例如:已经修改了两个文件并且想要将它们作为两次独立提交,但是却意外的输入了 git add . 暂存了他们两个。如何取消其中一个?git status 命令已经提示了: git reset HEAD <file>

➜  git_test git:(master) ✗ git add .
➜  git_test git:(master) ✗ gst
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   1
    modified:   2

➜  git_test git:(master) ✗ git reset HEAD 1
Unstaged changes after reset:
M   1
➜  git_test git:(master) ✗ gst
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   2

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:   1

情形三:撤销对文件的修改

使用命令: git checkout -- <file>...

如果要放弃修改,将它还原成上次提交的样子,git status 也提示了怎么去做.

注意 该命令是个危险的命令,你对文件的任何修改都会消失,你只是拷贝了另一个文件来覆盖它。除非你确实清楚不想要那个文件了,否则不要用这个命令。

➜  git_test git:(master) ✗ git checkout -- 1
➜  git_test git:(master) ✗ gst
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章阅读 8,667评论 1 26
  • 人生天地间,路路九曲弯,从来没有笔直的。 水能直至大海,就是因为它巧妙地避开所有障碍,不断拐弯前行。 许多聪明人没...
    遗忘的憾事阅读 341评论 0 2
  • 银蕨弯曲的嫩蕾 在摇晃的山道间探向车窗 它背面的银色是月亮的折射吗 回家的光 是绵羊国毛利人的家乡 我们作为外来的...
    青草香666阅读 291评论 1 1
  • 你生来是不是男丁?自己说了不算;你生来是不是老大?自己说了也不算。当你瓜瓜落地,一声啼哭能否响彻四海云霄,在父母看...
    hnyxinfeng阅读 111评论 0 0