How to reset a branch to another branch with git?
GitGit Problem Overview
let's say that we have an hotfixes
branch which was created from master
. we added commits to hotfixes
, but those commits were not useful, so now we want to start from a fresh copy of master
again.
to clarify better, this is the reference workflow: http://nvie.com/posts/a-successful-git-branching-model/
let's also say that we pushed hotfixes
to the origin
remote because we have an awful set up and that's the only way to test something, so we need to reset the branch also on the remote server.
how to reset hotfixes
to a copy of master
?
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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.
Solution 3 - Git
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
Solution 4 - Git
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.
Solution 5 - Git
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.