Skip to content

Instantly share code, notes, and snippets.

@billbonney
Last active November 9, 2021 23:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billbonney/76de0f1a2e79f3a8a1a5 to your computer and use it in GitHub Desktop.
Save billbonney/76de0f1a2e79f3a8a1a5 to your computer and use it in GitHub Desktop.
How to fix master when it diverges using git

If your master branch of your fork has diverged from upstream master you can check your master by typing

git checkout master
git fetch upstream master
git merge --ff-only upstream/master # if this fails your master is not the same as upstream i.e. it won't fast-forward

To clean up your master try (Option 2 is simpler, Option 1 downloads less)

# (Option 1) Fix master from point of divergence
#WARNING: you will lose your changes to master
git checkout master
git fetch upstream master
git reset --hard <commit_where_it_diverges>
git merge --ff-only upstream/master # if this fails you have found the point far back enough in time before the divergence
git push --force origin master #This step is only required if you pushed your 'mess' to origin ;)

#(Option 2) Fix by deleting master and starting again with a fresh copy
#Warning this will delete any changes you have made on master
git checkout upstream/master 
git branch -D master
git branch master
git checkout master
git push --force origin master #This step is only required if you pushed your 'mess' to origin ;)

Also useful comamnds are

make clean # Do this in the root build dir

git clean -d -f -x # get rid of all not git files (be warned you could lose work if you haven't committed those changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment