Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple squash without rebase
## within current branch, squashes all commits that are ahead of master down into one
## useful if you merged with upstream in the middle of your commits (rebase could get very ugly if this is the case)
## commit any working changes on branch "mybranchname", then...
git checkout master
git checkout -b mybranchname_temp
git merge --squash mybranchname
git commit -am "Message describing all squashed commits"
git branch -m mybranchname mybranchname_unsquashed
git branch -m mybranchname
## optional, not recommended if you want to keep the unsquashed history around for a bit longer
git branch -D mybranchname_unsquashed
## if squashing already-pushed commits...
git push --force
@youneedamichelle

This comment has been minimized.

Copy link

commented Jun 23, 2016

thank you for saving me from rebase hell 👍

@Qiki

This comment has been minimized.

Copy link

commented Aug 5, 2016

I used for three years. Scott! you are the best!

@damianmr

This comment has been minimized.

Copy link

commented Nov 30, 2016

Thank you, this is a masterpiece of gitmanship :shipit:

@DevSide

This comment has been minimized.

Copy link

commented Dec 7, 2016

Another way, you can rebase on the parent of your first commit which is the pointer where your branch diverged.

git rebase -i FIRST_COMMIT_SHA1~

@aseem2625

This comment has been minimized.

Copy link

commented Jul 20, 2017

lol.. I actually had to get some conflicts with rebase with master.. But above steps effectively got me the same thing as below could do:

  1. git reset --soft HEAD^^^ (as many new commits in your feature branch. Now, you can see all your changed files in git st )
  2. git stash (stashing all changed files of feature branch)
  3. git rebase master (no conflicts because feature branch now has no commits of its own)
  4. git stash pop (with conflicts in your changed files adding <<<< ==== wherever you made changes in your files..)

Anyways, this is good enough.

@shahi645

This comment has been minimized.

Copy link

commented Aug 25, 2017

cheers bro, swish swish swish...another one bites the dust

@ncullen

This comment has been minimized.

Copy link

commented Apr 17, 2018

I frequently consult this GIST as I can never remember how to do this, and wish it were a single command in git. Thank you so much for saving me from rebase hell.

@shahi645

This comment has been minimized.

Copy link

commented Jun 11, 2018

Cheers again bro!

@sgupta-paymerang

This comment has been minimized.

Copy link

commented Aug 16, 2018

Thanks a lot !! You saved my time :)

@shahi645

This comment has been minimized.

Copy link

commented Aug 17, 2018

Once again this has served me well...thanks!

@shahi645

This comment has been minimized.

Copy link

commented Sep 19, 2018

Fair play, this has once again pulled me from the darkness

@shahi645

This comment has been minimized.

Copy link

commented Oct 8, 2018

Nice one m8!

@dbcoderain

This comment has been minimized.

Copy link

commented Jun 20, 2019

Brilliant, thanks for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.