Adam
Adam

Reputation: 1108

Merge Git branch without commit log

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')

Upvotes: 62

Views: 54148

Answers (3)

knittl
knittl

Reputation: 265211

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 (not really, the old commits still exist and you simply create new ones that look like the old ones) 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.

Upvotes: 22

Ellioh
Ellioh

Reputation: 5330

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.

Upvotes: 100

VonC
VonC

Reputation: 1324168

As mentioned in "Trimming GIT Checkins/Squashing GIT History", 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".

Upvotes: 4

Related Questions