How to reset a branch to another branch with git?
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
?
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
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
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.