git如何撤销操作
这篇文章主要介绍一些基本的撤销操作的相关命令。
一、撤销暂存区文件
假设我们有两个txt文件(分别为a.txt,b.txt),不小心使用了git add .
命令,两个文件全添加到了暂存区,如何撤销其中一个添加到暂存区的文件呢?
1.查看git状态
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: a.txt # new file: b.txt
2.撤销暂存区中的一个文件(例如我们要撤销的文件是b.txt),根据上面git status
命令显示的提示可知,我们应该输入
$ git reset HEAD b.txt
3.再次查看git状态
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: a.txt # Untracked files: # (use "git add <file>..." to include in what will be committed) # # b.txt
发现b.txt文件已经不在暂存区中了~
二、撤销已提交的文件
假设我们想提交的文件是a.txt,此时文件处于未跟踪状态
假设我们有一个a.txt文件处于未跟踪状态,如果提交文件后,我们想撤销此提交操作,回到上次未操作此文件时的状态(也即是a.txt还处于未跟踪状态),该怎么办呢?
1.添加到暂存区并提交
$ git add a.txt $ git commit -m "a.txt"
2.查看git状态
$ git status # Your branch is ahead of 'origin/master' by 1 commit. # (use "git push" to publish your local commits)
3.撤销文件回到上次未操作时的状态
$ git reset HEAD^
4.再次查看git状态,发现文件还原到未跟踪状态了
$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # a.txt