Merge GIT branch without commit log

GitMerge

Git Problem Overview


Currently when I'm using GIT I create a branch for each job and make various commits before I'm finished. I then merge back with my master branch and push upstream. I'm likely to have several branches at any one time and also flick between them mid-job as things crop up.

But most of these commits are just save points, i.e. not that important in the grand scheme of things. So when I merge the branch, I'd like it if the branch logs didn't merge with the master logs.

Is there a way to just merge the log message for commit g below (and not commits c or e)?

a [master] 
|
b (create branch 'job')
|\
| \
|  c 
|  |
d  e
|  |
f  g (next step is to merge 'job' branch with 'master')

Git Solutions


Solution 1 - Git

I consider merge --squash extremely useful when I use a "branch per feature" approach. My commit history in temporary branches is a complete garbage, absolutely meaningless. And I really want to be unable to see that history anymore, not just to be able to skip it.

When the commits go to code review, it is important not to create extra commits.

Solution 2 - Git

There is, but it is nonsense. Why do you want to do that? You'll lose all branch history and information.

You could use g's commit message for your merge commit and then browse history with the --first-parent option.

If you really want to lose history from your branch use git merge --squash, i do not recommend it though

Edit

In case you are not happy because you do not consider your history very clean, you can use Git's rebase feature:

You can retroactively edit old commits and create new commits from your existing branch (in effect rewriting it). It allows you to reword commit messages, split commits, squash commits into a single commit, re-order commits, etc.

You should only use rebasing when you haven't published your branch (i.e. it is only a private branch), because it will give other developers headache and could cause merge problems later on, if someone kept working on the old (before rebased) branch.

Solution 3 - Git

As mentioned in "[Trimming GIT Checkins/Squashing GIT History][1]", if your commits c and e were done with a comment prefixed with fixup!, then the rebase --interactive --autosquash (git1.7+, February 2010) can do exactly what you are after.

> with the fixup! directive, you could keep that squashing "invisible" in the commit message, while still benefiting from the automatic commit reordering with the --autosquash option.

To commit with a fixup! prefix, you can define the alias

[alias]
    fixup = !sh -c 'git commit -m \"fixup! $(git log -1 --format='\\''%s'\\'' $@)\"' -
    squash = !sh -c 'git commit -m \"squash! $(git log -1 --format='\\''%s'\\'' $@)\"' -

The fixup alias would then apply for those "commits (which) are just save points, i.e. not that important in the grand scheme of things". [1]: https://stackoverflow.com/questions/2302736/trimming-git-checkins-squashing-git-history/2302947#2302947

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamView Question on Stackoverflow
Solution 1 - GitElliohView Answer on Stackoverflow
Solution 2 - GitknittlView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow