GIt后悔药:还原提交操作(谨慎操作)
一、背景:
偶尔会遇到git的版本分支的文件被误改的情况,需要还原,此篇文章可能会帮助到你。
PS:
来理解下 Git 工作区、暂存区和版本库概念,可以更好的理解以下的还原操作。
* 工作区:就是你在电脑里能看到的目录。
* 暂存区:英文叫 stage 或 index。一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
* 版本库:工作区有一个隐藏目录 .这个不算工作区,而是 Git 的版本库。
详解的流程图如下:

二、git常用命令详解
1.git status:
查看工作区代码相对于暂存区的差别
2.git add .:
将当前目录下修改的所有代码从工作区添加到暂存区,.代步当前目录
3.git commit -m 'message':
将缓存区的内容添加至本地仓库
4.git pull orgin master:
先将远程仓库master中的信息同步到本地仓库master中
5.git push orgin master:
orgin是远程主机,master表示是远程服务器上的master分支和本地分支重名的简写,分支名是可以修改的
6.git checkout -- test001.txt (或还原全部 git checkout -- *)
将工作区修改的文件直接还原为最新版本
7.git reset HEAD
回退至当前版本(本地仓库,并移出暂存区)
8.git reset HEAD^
回退至上个版本(本地仓库,并移出暂存区)
9.git reset --hard HEAD^
回退至上一个版本(本地仓库,并移出暂存区,同时更新工作区的文件)
10.git reset --hard HEAD^^
回退至上上个版本(本地仓库,并移出暂存区,同时更新工作区的文件)
11.git reset –hard commitID
回退到某一次提交记录(不可逆,之前的提交记录会清除)
12.git revert commitID -m 1
撤销某一次的提交记录
三、还原至上一次的提交后(工作区修改了文件,未提交至暂存区)
1.举例说明,git仓库内修改文件test001.txt从 001 -> 001modify
此时界面查看文件包含感叹号.未入暂存区
echo -e 001modify > test001.txt

2.使用git checkout -- test001.txt 将工作区修改的文件直接还原.
此时查看:界面文件为绿色标记,命令行git status状态正常,无提交内容
git checkout -- test001.txt



四、还原至上一次的提交后(工作区修改了文件,并提交至暂存区,未提交到本地仓库)
1.举例说明,git仓库内修改文件test001.txt从 001 -> 001modify,并add加入暂存区:
echo -e 001modify > test001.txt

2.回退当前的版本:
界面文件为感叹号标记, 查看状态暂存区内容被移出,本地文件任为修改后.
git reset HEAD



3.使用git checkout -- test001.txt 将工作区修改的文件直接还原.
此时界面文件为绿色标记,查看状态无提交内容.
git checkout -- test001.txt


五、还原至上一次的提交后(工作区修改了文件,并提交至暂存区,已提交到本地仓库,未推送远程仓库)
1.举例说明,git仓库内修改文件test001.txt从 001 -> 001modify,并add加入暂存区:
此时界面文件为感叹号标记
echo -e 001modify > test001.txt


2.使用git commit将修改提交至本地仓库,此时文件状态无感叹号,提示未push远程仓库


3.使用git reset HEAD^回退到上一个版本:
此时文件状态为感叹号,修改移出暂存区
git reset HEAD^


4. 使用git checkout -- test001.txt 将工作区修改的文件直接还原.
此时文件状态绿色
git checkout -- test001.txt

5.git push无法将本地仓库推送至远程仓库
使用git push提示报错,因为本地仓库与远程仓库不一致
此时需要推送的话使用强制提交
git push --force


六、还原至上一次的提交后(工作区修改了文件,并提交至暂存区,已提交到本地仓库,并已推送远程仓库)
1.本地文件修改,add加入暂存区,commit提交至本地仓库,push推送到远程仓库
此时文件内容被修改,windows文件为绿色标记,状态无提交内容.


2.使用git reset HEAD^直接回退至上个版本:
此时工作区文件仍为修改后,文件状态显示已移出暂存区
git reset HEAD^

3.使用git checkout -- test001.txt 将工作区修改的文件直接还原.
此时界面文件为绿色标记,查看状态无提交内容.
git checkout -- test001.txt


4.使用git push --force强制提交本地仓库至远程仓库
git push --force

七、还原至上一次的提交后(当前版本已push,一步操作)
1.本地文件修改,add加入暂存区,commit提交至本地仓库,push推送到远程仓库
此时文件内容被修改,windows文件为绿色标记,状态无提交内容.

2.使用git reset --hard HEAD^还原至上一个版本
git reset --hard HEAD^
此时同时将文件从暂存区移除,并替换工作区为上个版本,界面标记为绿色


3.使用git push --force强制提交本地仓库至远程仓库
git push --force

八.还原至上上次的提交的版本
1.两次提交记录文件修改test001.txt与test002.txt,并分别push远程仓库
通过git log查看提交的记录:

2.使用git reset --hard HEAD^^还原至上一个版本
git reset --hard HEAD^^
此时同时将文件从暂存区移除,并替换工作区为上个版本

3.如需要将本地仓库推送至远程仓库使用强制提交
git push -f
九、还原至某一次提交记录
1.使用git log查看提交的记录信息

2. 使用git reset –hard commitID 还原到某一次提交
git reset --hard 508523eb52ffd6ca274cb01605503fb8bd6bc65a

此时只会把远程版本(对应的commitId)覆盖到本地仓库,然后如果本地做了修改想要提交现修改后的版本到远程就必须强行推送
3.git push -f强制推送本地仓库直远程仓库
git push -f

4.注意:操作不可逆,此时查看git log,还原版本后的记录不复存在

十.还原掉其中一次提交并保留其余的提交
1.git log查看日志并找到需要撤销的记录

2.使用git revert commitID -m 1 命令来撤销某一次的提交
git revert 44e1cf32e0d914865d3476d3a765a83a7253586c -m 1
进入对话框保存后记录被撤销


3.查看git log已经撤销,无需推送

ps:当存在分支合并的情况下慎用git revert,会还原已经合并记录的提交.