git版本回退 2020-03-04(未经允许,禁止转载)

2020-03-04  本文已影响0人  9_SooHyun

Undo Commits

git的回退(Undo),通过git reset and git revert实现

git reset

作为版本控制系统,git拥有【回到过去】和【重返未来】的能力,使用的命令也相当相当简单,就是一句git reset

git reset命令可以实现在不同版本间穿梭跳越,分为 git reset --hard 和 git reset --soft

git revert

git revert commit_id

git revert will create a new commit that will undo all of the work that was done in the commit you want to revert.

To undo commits that have already been pushed and shared with the team, we cannot use the git reset command. Instead, we have to use git revert.

当某些commit已经push到远端中央仓库并且被整个开发团队共享后(比如合并入master),要想取消这些commit,我们就不能用git reset。为什么?
因为这些commit一旦push到远端中央仓库并合入master,别人就可能git pull拉到自己的本地仓库。假设:

这时,A 取消掉的commit会被B再次push上去,既不能真正实现undo的目的,也会让团队成员产生confuse

小结1:git reset就像抹杀历史;而git revert是在认可历史的基础上“拨乱反正”

小结2:不要去修改已被共享的代码的历史

实际上,对于已经被共享的代码,我们都不要去修改它们的历史,不要尝试git reset或者git rebase这些篡改历史的操作,因为:

查找commit_id的两个命令

上一篇 下一篇

猜你喜欢

热点阅读