git复原命令

覆盖上一个commit

有时候你在某个提交可能忘加一个文件,git commit -- amend命令就排上用场

命令可以在你git add filename之后,对上一次提交进行覆盖,具体的就是加上

这个文件,当然你也可以修改 commit message。注意总的来说只有一个commit

git add two.txt
git commit --amend
#只能看到更新后的一个提交
git log  

暂存区文件回退到untracked状态

有时候你也可能使用git add .,把当前目录下的所有文件加入缓存区,其实有些

文件是不需要加入缓存区的,git reset HEAD filename,可以unstage a staged file

具体的

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   three.txt

开始three.txt已经处于暂存区

执行git reset HEAD three.txt将得到一下结果, three.txt又处于untracked状态

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        three.txt

nothing added to commit but untracked files present (use "git add" to track)

文件回退到上一次提交的版本

在使用git的过程中,你肯定会遇到已经修改的文件,希望它回退到上一次提交的版本。

git checkout -- filename,就可以实现上述功能

比如four.txt被修改,后没有提交, 内容为4444, 5555

$ git status
On branch 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:   four.txt

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

$ cat four.txt
4444
5555

使用 git checkout -- four.txt之后,回退到了上一次提交的状态

$ git status
On branch master
nothing to commit, working tree clean

$ cat four.txt
4444

猜你喜欢

转载自blog.csdn.net/zycxnanwang/article/details/85634210