Michael Durrant
Michael Durrant

Reputation: 96484

How to know which Git commits to squash in my branch when looking at history

As with many places we like to squash our commits, within our feature branches before merging to master.

To do this we frequently squash commits with an interactive rebase (git rebase -i HEAD~10) before merging

How do I know which commits to squash within the branch?

If I do git log I see my history of commits - plus prior commits to master.
Is there an easy way to see/know which commits were made on this branch vs. commits that are already on the master branch? git history doesn't seem to distinguish them in an obvious way. I think I know which was the first commit on this branch - is there an easy way to be sure?

Upvotes: 0

Views: 395

Answers (2)

strikeralley
strikeralley

Reputation: 31

While you're writing your code if you know the work your committing is going to eventually be squashed into another commit you can use fixup! <identical commit message> as your commit message and git will automatically rearrange and squash the commits on rebase.

This is particularly useful to maintain the notion of atomic commits, where a commit is a meaningful chunk of work, as oppposed to oops fixed typo from last commit commits in your history, then later trying to remember where to squash what.

As far checking the graph, I recommend Aasmund's answer of using git log with some decorators. I personally use git extensions because I like its commit window and graph visualization, while using posh-git in powershell for everything else.

If you'd like an alias to interactively rebase back to your starting base you can use git rebase -i $(git merge-base master HEAD). This will take your current HEAD position and rebase all the way back to where you started. I find this useful for squashing my work before a merge.

Upvotes: 0

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37950

git rebase -i master

instructs git to choose all commits on your branch starting with the commit after the common ancestor of your branch and master, and the result will be placed on top of master.

git log --graph --oneline --decorate master yourbranch

shows the log as a tree (with some impressive ASCII artistry).

Upvotes: 2

Related Questions