user1649972 user1649972 - 1 month ago 13
Git Question

GitHub desktop branching and merging issues

I was hoping someone might be able to help me. I am currently learning Git commands but for the moment I using the desktop version of GitHub. I am running into some issues with branching and merging and was wondering anyone could provide some insight into an issue I am having.

When I branch and carry out a large modification with various commits and commit messages I would like to be able to retain the commit history in the branch when I merge it back to

master
. At the moment when I merge,
master
will have the commits in its history (which I want too) but the branch lose all commit history.

I am getting pretty comfortable with the Git commands using PowerShell so if it would be possible to solve any of my issues with Git commands I would be happy to try it.

Answer

Here's an example of what your history probable looked like before the merge:

*--*--*--*--A [master]
             \
              *--*--*--* [myfeature]

Notice that master has no commits that myfeature doesn't have. (No commits have been added to master past the point that myfeature was branched off, commit A.) Because of this, by default Git does a fast-forward merge, which leads to history like this:

*--*--*--*--A--*--*--*--* [master,myfeature]

(As mentioned by @tkausl, the history isn't transferred, only master is modified. Unless you deleted it afterwards, myfeature should not have changed.)

My assumption is that you want your history to look more like this:

*--*--*--*--A------------* [master]
             \          /
              *--*--*--* [myfeature]

To do that, you have to do the merge like this:

git checkout master
git merge --no-ff myfeature

If you want to undo your fast-forward merge and redo it as a --no-ff, do:

git checkout master
git reset --hard A
git merge --no-ff myfeature

To figure out the commit hash for A, and to ensure that this is what happened, inspect the output of:

git log --all --oneline --decorate --graph