git中的撤销操作

修改最后一次提交的信息

有的时候我们提交commit后,不想提交了,想取消。或者想修改最后一次提交的信息

比如我们的提交历史如下:

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (dev)
$ git log
commit 453724d4c64351885a8b96c929aa3c5427606ec9
Author: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 15:00:55 2015 +0800

    0303

commit 0be21561767575dabe4902e427e47100bbcfded2
Author: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:50:37 2015 +0800

    first commit

commit 557d1095b610b0cbb26d679528af551a6bcdb10f
Author: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:24:29 2015 +0800

现在我看第一个commit的信息不爽怎么办?git为我们提供了git commit –amend命令

我们将0303改为2015.3.3,保持退出后,输出如下信息:

$ git commit --amend [dev e0793fc] 20150303 Committer: unknown <[email protected]> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email [email protected] After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) 

此时再执行git log命令会发现如下信息:

commit e0793fce0e83ba7e64aa60acc1b26d2ccd1c0f1e
Author: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 15:00:55 2015 +0800

    20150303

commit 0be21561767575dabe4902e427e47100bbcfded2
Author: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:50:37 2015 +0800

    first commit

说明修改成功了。

取消最后一次add的文件

$ git status
On branch dev
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:   README.md
        modified:   shell/screenshot.sh

no changes added to commit (use "git add" and/or "git commit -a")

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (dev)
$ git add .

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
        modified:   shell/screenshot.sh

例如上面的信息,我们将2个文件的修改一起暂存了。但是我发现
shell/screenshot.sh并不需要暂存,我想取消怎么办,很简单,可以看到上面的提示信息就有这个命令git reset HEAD 。我们来试试

$ git reset HEAD shell/screenshot.sh
Unstaged changes after reset:
M       shell/screenshot.sh

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

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:   shell/screenshot.sh

我们成功的把shell/screenshot.sh从准备提交列表(Changes to be committed)赶到了没有暂存文件列表(Changes not staged for commit)中

你可能感兴趣的:(git)