Search code examples
gitgit-branchgit-remote

Overwrite local branch with remote tracking branch (branches have diverged)


I have create a new branch named my-4.3.y using the following command (note: my-4.3.y is set up to track remote branch 4.3.y from origin):

git checkout -b my-4.3.y origin/4.3.y

I haven't worked on the my-4.3.y branch after checking it out. Now, several days later, when I run:

git status

It tells me that my-4.3.y and origin/4.3.y have diverged.

I don't care where and why the branches have diverged, I don't want to merge the remote branch into my. I just want my branch to be equal to the remote branch again.

So, what I am doing is: (1) checkout some other branch (2) delete my-4.3.y and (3) check it out again:

git checkout some_other_branch
git branch -D my-4.3.y
git checkout -b my-4.3.y origin/4.3.y

Is there an easier way to that?


Solution

  • You can use git reset --hard to force your currently checked out branch to any arbitrary point you would like.

    Note: using --hard is not working directory safe, and will throw out any changes you have.

    For the specific case of updating to your tracking branch you can use @{u} to specify your upstream

    git reset --hard @{u}
    

    Generally, you can pass in any branch reference or anything else Git resolves to a commit e.g.

    git reset --hard origin/4.3.y