Git撤销操作


修改最后一次提交

如果提交后发现想要修改提交信息,可以直接使用 git commit --amend 命令,使用该命令后,Git会启动文本编辑器,然后可看到上次提交时的说明,编辑它确认没问题后保存退出,就会使用新的提交说明覆盖刚才的提交信息:

$ git commit --amend

如果刚才提交时忘了暂存某些修改,可以先补上暂存操作,然后再运行 --amend 提交:

$ git add forgotten_file
$ git commit --amend

取消已经暂存的文件

$ git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: README.config
modified: README.md

使用命令 git reset HEAD README.config 将README.config移出暂存区:

$ git reset HEAD README.config
Unstaged changes after reset:
M README.config

$ git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)
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: README.config

可看到,README.config已经为Changes not staged for commit状态。

取消对文件的修改

将README.config移出暂存区后,可以进一步使用命令 git checkout -- README.config 取消对README.config的修改。

$ cat README.config
hello

$ git checkout -- README.config

$ git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: README.md

$ cat README.config

《pro git》学习笔记

优质内容筛选与推荐>>
1、装饰器及进阶
2、微信 分享
3、Decorator 装饰(结构型)
4、Entity Framework 6新特性:全局性地自定义Code First约定
5、SpringMVC中REST风格


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号