user1649972
user1649972

Reputation: 381

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.

Upvotes: 0

Views: 977

Answers (2)

KemanoThief
KemanoThief

Reputation: 617

You may be looking for git rebase

When you rebase, git essentially imagines that you branched off from the main branch (e.g. master) later than you actually did. It brings master up-to-date and then automatically applies your branch's changes to this version.

There might be clashes if the files you've been working on have also changed since you branched but the rebase process lets you check over both sets of changes and choose the correct ones.

When you push these changes, you'll be rewriting history so you'll need to git push --force. Please be careful - This command can be dangerous as it might effortlessly overwrite your colleagues' code etc.

More info: https://git-scm.com/book/en/v2/Git-Branching-Rebasing

Upvotes: 1

Scott Weldon
Scott Weldon

Reputation: 10227

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

Upvotes: 1

Related Questions