Search code examples
gitrebase

How to replace one git branch with another?


Sometimes I will checkout a test branch to see if a rebase / whatever works. But this leaves me in a situation where I have some branch feat-234, and some branch test, and I want to replace feat-234 with test.

Example workflow:

# on branch feat-234
git checkout -b test
git rebase main 
# apply fixes required to make rebase work

At this point I want to use test instead of feat-234, as what I wanted to do has worked.

edit 1

I tried to do the following whilst on branch test:

  • git branch -D feat-234, delete feat-234
  • git branch -m feat-234`

I don't think this worked though as I think I've lost all the remote information that was on feat-234.


Solution

  • After you've verified that test is exactly what you want the feature branch to become:

    git checkout feat-234
    git reset --hard test
    

    and you're done. You'll probably need to push with --force as well, since you're not simply adding commits to the branch.