Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge conflicts ruin my commit message while squashing commits

Tags:

git

merge

My usual workflow with git is to create a new feature branch, do some work with frequent commits, and then merge back into the development branch when the feature is working and/or stable.

Usually, when I do git merge --squash feature-branch, I get a nice "squashed commit of the following" message that auto-populates with all of the commit messages from the feature branch.

However, if there are any merge conflicts (say I finished and merged another feature while working on this one), I seem to lose all of my commit messages from the branch. The auto-populated commit message fills in the conflicts, but not the commit messages. Where did my commit messages go? Can I get them back?

like image 735
Ryan Avatar asked Aug 31 '10 01:08

Ryan


People also ask

What happens if you squash a merge commit?

Squash and merge your commits Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit and merged into the default branch. Pull requests with squashed commits are merged using the fast-forward option.

Should you squash commits on merge?

Squash merges, as it's proponents argue, are more valuable than merge commits because entire new features or bug fixes can be compressed into a single commit and therefore easier to code review and read at some point in the future.

Is it good practice to squash commits?

Before you start, keep in mind that you should squash your commits BEFORE you ever push your changes to a remote repository. If you rewrite your history once others have made changes to it, you're asking for trouble… or conflicts. Potentially lots of them!

Should you squash commits before merging to master?

As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.


3 Answers

Yes, you can get the squash commit message back. It's stored in .git/SQUASH_MSG.

You can use it as a template with the following command:

git commit -t .git/SQUASH_MSG
like image 71
MikeK Avatar answered Sep 28 '22 07:09

MikeK


This doesn't answer your question directly, but you should be able to avoid the conflict in the first place.

Consider doing a

git rebase master topic

before performing the merge. The DESCTIPTION section of this page http://git-scm.com/docs/git-rebase should be helpful.This may also obviate the need for the squash as an interactive rebase would allow you to to squash commits of your choosing.

EDIT: See also: In git, what is the difference between merge --squash and rebase?

like image 25
bc17 Avatar answered Sep 28 '22 08:09

bc17


Nothing is really lost with git. The list of the commits on the feature branch can be obtained using:

git cherry feature-branch

Then simply pipe this to git cat-file:

git cherry feature-branch | cut -f2 -d' ' | git cat-file --batch

You need to clean-up the output though. I don't know a way to automated it better.

like image 45
gawi Avatar answered Sep 28 '22 08:09

gawi