在未进行git push
前的全部操做,都是在“本地仓库”中执行的。咱们暂且将“本地仓库”的代码还原操做叫作“撤销”git
状况一:文件被修改了,但未执行git add
操做(working tree内撤销) (modify file) <==> git checkout <filename>/.
spa
git checkout fileName git checkout .
状况二:同时对多个文件执行了git add
操做,但本次只想提交其中一部分文件 git add <==> git reset HEAD <filename>
.net
$ git add * $ git status # 取消暂存 $ git reset HEAD <filename>
状况三:文件执行了git add
操做,但想撤销对其的修改(index内回滚) (modify file and add) <==> git reset HEAD <filename> && git checkout <filename>
指针
# 取消暂存 git reset HEAD fileName # 撤销修改 git checkout fileName
状况四:修改的文件已被git commit
,但想再次修改再也不产生新的Commit git commit <==> git commit --amend -m 'msg'
code
# 修改最后一次提交 $ git add sample.txt $ git commit --amend -m"说明"
状况五:已在本地进行了屡次git commit
操做,如今想撤销到其中某次Commit (git multiple commit) <==> git git reset [--hard|soft|mixed|merge|keep] [commit|HEAD]
blog
通常使用 --softip
git reset [--hard|soft|mixed|merge|keep] [commit|HEAD]
上述场景二,已进行git push
,即已推送到“远程仓库”中。咱们将已被提交到“远程仓库”的代码还原操做叫作“回滚”!注意:对远程仓库作回滚操做是有风险的,需提早作好备份和通知其余团队成员!get
状况一:切换到 tag 或 branchit
若是你每次更新线上,都会打tag,那恭喜你,你能够很快的处理上述场景二的状况 git tag <==> git checkout <tag>
ast
git checkout <tag>
若是你回到当前HEAD指向 (git current HEAD) <==> git checkout <branch_name>
git checkout <branch_name>
状况二:撤销指定文件到指定版本 (git file in history) <==> git checkout <commitID> <filename>
# 查看指定文件的历史版本 git log <filename> # 回滚到指定commitID git checkout <commitID> <filename>
状况三:删除最后一次远程提交 git revert HEAD || git reset --hard HEAD^
方式一:使用revert 会有新的 commit 记录
git revert HEAD git push origin master
方式二:使用reset 不会产生新的 commit 记录
git reset --hard HEAD^ git push origin master -f
两者区别:
状况四:回滚某次提交 (git commit in history) <==> git revert <commitID>
# 找到要回滚的commitID git log git revert commitID
一样的,revert 会出现一次新的 commit 提交记录,这里也可使用 reset
删除某次提交 (git commit in history) <==> git rebase -i <commitID>
git log --oneline -n5 git rebase -i <commit id>^
注意:须要注意最后的_^_号,意思是commit id的前一次提交
git rebase -i "5b3ba7a"^
在编辑框中删除相关commit,如pick 5b3ba7a test2
,而后保存退出(若是遇到_冲突_须要先解决_冲突_)!
git push origin master -f
经过上述操做,若是你想对历史多个commit进行处理或者,能够选择git rebase -i
,只需删除对应的记录就好。rebase还可对 commit 消息进行编辑,以及合并多个commit。
转自:https://blog.csdn.net/ligang2...