Sometimes you have a branch checked out locally that someone else owns and they do a force push. Perhaps it's during a review when they have pushed all the needed fixups and got them approved. So now they squash and force-push the branch. It's time for you to take a new look at the updated commits, so you checkout the branch and try to update it.
$ git checkout some-topic-branch
$ git pull
fatal: Not possible to fast-forward, aborting.
What's happening here? Well, the branch has diverged, you have the old commits locally that have been changed on the remote, so you need a way of reseting the some-topic-branch
to be same as the version at origin/some-topic-branch
.
git reset --hard origin/some-topic-branch
The downside here is that it doesn't just reset the branch, it also resets the working directory (any changed files)
git checkout origin/some-topic-branch
git checkout -B some-topic-branch
Start by checking out another branch (because you can't delete the branch you're currently on),
specifically we checkout the origin version of the branch.
And then we run checkout again with -B
so that it will recreate the branch if it already exists.
So any commits in the branch are thrown away, but the working tree remains untouched!
git checkout -B some-topic-branch
Work well.