Skip to content

Instantly share code, notes, and snippets.

@decentraliser
Last active April 18, 2021 16:54
Show Gist options
  • Save decentraliser/1a218386f66e53e40d6a11ae21afe4f8 to your computer and use it in GitHub Desktop.
Save decentraliser/1a218386f66e53e40d6a11ae21afe4f8 to your computer and use it in GitHub Desktop.
Git: rebasing a (broken) branch
/**
* Rebase, having non-committed changes
*/
git remote update origin
git stash
git checkout master
git pull
git checkout MY_BRANCH
git rebase master
git stash pop
git commit -m "MY_COMMIT_MESSAGE"
git push -f
/**
* Rebase, when master changed but not my branch
*/
git remote update origin
git checkout master
git pull
git checkout MY_BRANCH
git rebase master
git commit -m "MY_COMMIT_MESSAGE"
git push -f
/**
* If the rebase is impossible (broken branch)
* The brute squash technique
*/
// checkout to your branch
git checkout MY_BRANCH
// create a copy of your branch
git checkout -b MY_BRANCH_COPY
// update your local master branch
git checkout master
git pull
// just in case... reset master
git reset --hard origin/master
// go back to your branch
git checkout MY_BRANCH
// reset to master
git reset --hard master
// squash the copy of your branch on master
git merge --squash MY_BRANCH_COPY
git push -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment