DEV Community

Cover image for A quick guide to squashing Git commits

A quick guide to squashing Git commits

Victoria Drake on August 06, 2018

Let's get one thing out of the way first: rewriting Git history just for the sake of having a pretty tree, especially with public repositories, is ...
Collapse
 
kip13 profile image
kip • Edited

I'm always use this line to merge from another branch:

git merge --squash feature_branch

If you have conflicts and you trust in the changes of feature_branch, I mean you know the changes in the feature_branch are the correct, you can tell to git that take this confidence changes over the master or another branch where you need to apply the merge:

git merge --squash feature_branch -X theirs

In the other side:

git merge --squash feature_branch -X ours

And continue with the normal flow....

This is called "Strategy option"

Collapse
 
michaelgv profile image
Mike

Curiosity: In the event you have two branches you’ve been handed via a new project, which one can you trust as the source of truth? Typically I’ve trusted the most recent branch, but found that older branches have some better days than newer ones. Thoughts?

Collapse
 
sehetw profile image
sehe

I look at the contents. In the event that I'm unable to reach a verdict which one is better, I trust neither

Collapse
 
enguerran profile image
enguerran 🐐💨

If you want some colors in the git plog display, update the --pretty flag with this instead:

git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --branches
Collapse
 
jrtibbetts profile image
Jason R Tibbetts

rewriting Git history just for the sake of having a pretty tree, especially with public repositories, is generally not advisable

While I totally agree with this, your article has taught me some techniques that make me want to go rewrite my Git histories right now!

Collapse
 
blackode profile image
blackode

Thanks for the amazing article.

git commit --amend to add changes to the previous commit message. Hope you can give the better explanation on this.

Collapse
 
victoria profile image
Victoria Drake

Sure thing! Any other Git commands that everyone might like to see in plain English? I'll fold them into a future post. :)

Collapse
 
simbo1905 profile image
Simon Massey

If folks are using GitHub they can use squash-and-merge to merge a PR to get the “git merge —squash” behaviour. Then the PR lands as a single commit with two parents. On the branch you merged into the git log shoes a single clean commit. In the PR brach the git log shows all the original commits which can be noisy such as making minor changes due to code review.

Collapse
 
cripstian profile image
Cristian Popescu

So this squash thing got our production Admin JWT Token out of the public repository's history, so it is a cool reason to go ahead and fix your history.
Awesome article, that I will surely check back on to, when a squash will seem appropriate, of course.

Collapse
 
sehetw profile image
sehe

Keep in mind, it's not the squash (you could have edited the borked commit alone and kept the rest).

Also, once published the blobs will be in all clones' object databases for some time (until garbage collect). So, in all honesty this seems like a bad fix for a security risk. Instead, just revoke the admin token and generate a brand-new one.

Collapse
 
ikemkrueger profile image
Ikem Krueger

How do you fixed the merge conflicts?

Collapse
 
davedecahedron profile image
David Howell

git mergetool

Run that at the command line and it will launch whatever mergetool specified in your git config. You may need to install a mergetool first, some people use kdiff3, some hate it.

Collapse
 
sunbingfeng profile image
sunbingfeng

Nice tips!

Rewrite histories only on commits that have not been pushed to remote repo yet, and you must make sure that it will not cause troubles to others.