如何使用 git 将一个分支重置为另一个分支?

假设我们有一个从 master创建的 hotfixes分支。我们添加了对 hotfixes的提交,但是这些提交并不有用,所以现在我们希望再次从 master的一个新拷贝开始。

为了更好地澄清,这是参考工作流程: http://nvie.com/posts/a-successful-git-branching-model/

我们还可以说,我们将 hotfixes推到 origin远程,因为我们的设置非常糟糕,这是测试某些东西的唯一方法,所以我们需要重置远程服务器上的分支。

如何将 hotfixes重置为 master的副本?

101704 次浏览

You mean you want to push your local master to the remote hotfixes branch? Like this:

git push origin +master:hotfixes

However, this requires that you are allowed to re-write the history on the remote side.

If I understood your question correctly, what you're looking for is a way to move the branch pointer of origin/hotfixes to point to the current revision of origin/master.

If that be the case, these set of command should work (assuming you already have checked out hotfixes in your local git repo any time in the past):

# git branch -f does not allow modifying the currently checked out
# branch, so checkout any other branch than hotfixes
git checkout <SOME_OTHER_BRANCH_THAN_HOTFIXES>


# Move the branch pointer of hotfixes to the commit currently
# pointed by origin/master
git branch -f hotfixes origin/master


# Force push the history rewrite in the hotfixes branch
# into origin
git push -f origin hotfixes

this is how i did it with basic Git commands:

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

of course it's important to notify everyone working on hotfixes. most likely they will have to delete their local copy and start from a fresh one. an alternative, less invasive idea is to create a new branch:

git checkout master
git branch -tb hotfixes-2 # this creates branch `hotfixes-2` from a copy of `master`
git push origin HEAD # this creates `hotfixes-2` on the remote server

The answers here are solid. I have needed this exact change when resetting my staging branch to master. In that case I want to both reset the origin to match master and also reset my local to match that. So here is a git alias that allows you to pass in the branch name and do both commands in one move. (It's a little dangerous)

reorient = "!f() { git push origin +master:$1 && git reset --hard origin/$1 ; }; f"

Then use it like:

git reorient hotfixes

The answers above were totally correct. But this will simply allow for fewer keystrokes and a quicker turnaround! Hope it helps.

Based on a few of the answers in this thread, I did the following script with a few prompts to reduce risk of messing stuff up:

#!/bin/bash
# Questions for loop:
for value in {1..3}
do
# Asking if user wants to reset hotfix:
if [ "$value" == "1" ] ;then
echo -n "Are you sure you want to hard reset the hotfix branch (y/n)? "
read answer
if [ "$answer" == "${answer#[Yy]}" ] ;then
echo 'Okay, maybe next time.'
exit
fi
fi
# Asking if user is in void:
if [ "$value" == "2" ] ;then
echo -n "Are you in the void branch (y/n)? "
read answer
if [ "$answer" == "${answer#[Yy]}" ] ;then
echo 'You should checkout to the void branch.'
exit
fi
fi
# Asking if user has any uncommited changes:
if [ "$value" == "3" ] ;then
echo -n "Do you have any uncommited changes (y/n)? "
read answer
if [ "$answer" == "${answer#[Nn]}" ] ;then
echo 'You should commit your changes to avoid losing them.'
exit
fi
fi
done


echo 'Resetting...'
git checkout void
git branch -f hotfix origin/master
git push -f origin hotfix

100% open to any feedback to improve this script.