Git: Discard all changes on a diverged local branch
I have a local topic branch that's tracking a remote branch. For the sake of argume开发者_开发问答nt, 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 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.
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.
精彩评论