Git: Discard all changes on a diverged local branch

Git

Git Problem Overview


I have a local topic branch that's tracking a remote branch. For the sake of argument, say the commit histories look like this:

A--B--C--O1--O2--O3 (origin/phobos)
       \
         L1--L2--L3 (phobos)

Having looked at the relative commit histories, I now want to discard all the changes to the local phobos branch and get it back to being a direct copy of origin/phobos, so that the local history looks like this:

A--B--C--O1--O2--O3 (phobos origin/phobos)

I really don't want the local changes to the phobos branch, and I really don't want any merges to show up in the origin repository afterwards. (So, just merging isn't what I have in mind.)

This seems like it should be really easy, but my google-fu has failed me. How do I do this?

Git Solutions


Solution 1 - Git

git checkout phobos
git reset --hard origin/phobos

This tells Git to reset the head of phobos to the same commit as origin/phobos, and to update the working tree to match.

Solution 2 - Git

Delete the branch, then recreate it:

$ git branch -D phobos
$ git checkout --track -b phobos origin/phobos

Be aware that deleting the branch blows away the branch's reflog.

Resetting the branch (like shown in the other answer), on the other hand not only preserves the reflog, but actually records the reset in the reflog. This makes the operation easily reversible later, if needed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElectrons_AhoyView Question on Stackoverflow
Solution 1 - GitDan MouldingView Answer on Stackoverflow
Solution 2 - GitmipadiView Answer on Stackoverflow