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


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

Tags:

Git