状态流转图
- 各种场景下撤销修改
撤销修改
- 当前本地分支状态
- 修改 ReleaseNote.txt文件(tracked)
- 新增test.txt文件(untracked)
git status
On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.
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: ReleaseNote.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
no changes added to commit (use "git add" and/or "git commit -a")
- 撤销 (tracked)文件 (git checkout)
git checkout <file>
git checkout ReleaseNote.txt
git status
On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
- 撤销(untracked)文件(git clean)
git clean -f
Removing test.txt
git status
On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.
nothing to commit, working tree clean
git reset撤销
- git reset的撤销功能,其实可以覆盖所有的撤销。除了未跟踪状态
- git reset本质做的事情就是重置HEAD(当前分支的版本顶端)到另外一个commit 版本。
- 命令参数
- git reset --soft :重置HEAD到另外一个commit,仅此而已,不会回退暂存区和工作区
- git reset --mix :默认参数;重置HEAD返回到另外一个commit,并且重置暂存区,保留工作区
- git reset --hard :重置HEAD返回到另外一个commit,重置所有区,会丢失代码
- 撤销commit之后的文件,需要指定回退的话需要重置到上一个版本的commitId, 单纯的git reset还是回退到当前commitId是没有意义的。所以用到–soft也是要指定commitId;同样–hard,–参数不带版本号,只能
- git reset HEAD^,意思是上一个版本;也可以写成HEAD~1;如果你进行了2次commit,想都撤回,可以使用HEAD~2
- git log先获取到每次提交的commit_id git reset <commit_id> 要撤销本次提交,则要恢复到上个版本提交的commit_id
- 当前有4个文件
- git status 查看文件状态
git status
On branch bthss_V3.0.5
Your branch is ahead of 'origin/bthss_V3.0.5' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test-stage.txt
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: ReleaseNote.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test-untract.txt
- git reset --soft 撤销
$ git reset HEAD^ --soft
git status
On branch bthss_V3.0.5
Your branch is up to date with 'origin/bthss_V3.0.5'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: ReleaseNote.txt
new file: test-commit.txt
new file: test-stage.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test-untract.txt
- git reset --mix 撤销
git reset --mix
Unstaged changes after reset:
M ReleaseNote.txt
git status
On branch bthss_V3.0.5
Your branch is ahead of 'origin/bthss_V3.0.5' by 1 commit.
(use "git push" to publish your local commits)
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: ReleaseNote.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test-stage.txt
test-untract.txt
no changes added to commit (use "git add" and/or "git commit -a")
- git reset --hard 撤销
$ git reset --hard
HEAD is now at 1b4d720 test
git status
On branch bthss_V3.0.5
Your branch is ahead of 'origin/bthss_V3.0.5' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
test-untract.txt
nothing added to commit but untracked files present (use "git add" to track)
撤销已推送
这个应该没有办法再撤销了。