如何将主分支恢复为 git 中的标记?

我们有分公司的起源和发展。 主控的初始状态在 tag_ABC被标记。

我们对发展分支做了一些改变,并将其推向原点。 然后我们不小心合并发展成为大师,推到了原点。

现在我们希望将 master 恢复到检查点 tag_ABC。我们如何做到这一点?

77434 次浏览

You can do

git checkout master
git reset --hard tag_ABC
git push --force origin master

Please note that this will overwrite existing history in the upstream repo and may cause problems for other developers who have this repo checked out.

As per Luke Wenke's comment, other developers who have got master checked out will have to do the following:

git pull
git reset --hard origin/master

This isn't a direct answer to the question but this page comes back when searching for ways to revert a branch's code to a tag release.

Another way is to create a diff between the current state of the branch and the tag you want to revert to and then apply that to the branch. This keeps the version history correct and shows the changes going in then coming back out again.

Assuming your branch is called master and the tag you want to go back to is called 1.1.1

git checkout 1.1.1
git diff master > ~/diff.patch
git checkout master
cat ~/diff.patch | git apply
git commit -am 'Rolled back to version 1.1.1'
git push origin master