Git and Visual Studio 2017 part 5 : Merging the changes

In previous article, we did branching, therefore we do merge in this article, as that’s the main reason for branching. I was planning to do re-basing in this article, but I realized it is too much. So I postpone re-basing to next article.

Merge in Git

The reason I created a branch is to keep master branch intact but then I have to merge the change at some point. So let’s do it now.

1. Run ‘git branch’ to make sure you have master and dev branch and you are on dev. If not, make your branch and checkout.

image

2. Run ‘git log –oneline –graph’ to make sure master branch is behind the dev branch. If not, add some change and commit in dev. As you see, I didn’t commit anything on master so it is straight graph.

image

3. When you merge, you do from the branch which takes the changes in. In this case, master branch will take changes from dev branch. Run ‘git checkout master’ and ‘git merge dev’. All the changes comes into master now. It also says “fast-forwad”. What does this mean?

image

4. Run ‘git log –oneline –graph’. Now you see master branch simply pointing to the same commit as dev branch. From Git points of view, nothing changes in master branch, so simply moves master pointer is the easiest and fastest way to merge. But it shows straight graph, so I cannot tell what I did in dev branch.

image

5. Run ‘git reset –hard 473591c’ to reset the merge. When I did that, the git log in master branch shows master points to  commit 473591c. If I checkout dev and see log, then you see everything goes back to previous state.

image

6. This time, do merge without fast-forward. Run ‘git checkout master’ and ‘git merge –no-ff dev’. The option stands for no-fast-forward. This time, it says “recursive strategy” instead of ‘fast-forward, but changes seem to be same.

image

7. Run ‘git log –oneline –graph’. You see when I created dev branch, what I did and when I merged from the graph. And it also created new commit (8ac08e6) on top of everything. By doing this, you won’t lose a history in dev branch.

image

8. Run ‘git reset –hard 473591c’ again to reset the merge.

Merge in VS

1. Before doing merge, check the current status from View History action. It sure is back to before the merge.

image

2. Go to Team Explorer and select Branches. Checkout master and click “Merge”. Same as Git command, you need to be on master to merge dev branch.

image

3. Select dev on “Merge from branch” menu and click Merge. Note that I keep “Commit changes after merging” checkbox on.

image

4. Once merge completed, see history again. You see this is “fast forward” merge.

image

5. Do reset hard to commit 473591c9 from the history window to go back to original state.

image

6. Let’s do merge again, but this time, uncheck the “Commit changes after merging”.

image

7. Then you see following message. What shall I do now? As the message says, I need to “commit” explicitly.

image

8. Go back to “home” of Team Explorer by clicking home button.

image

9. Select “Changes” menu and there you go, the commit awaits you.

image

10. Enter commit message and click “Commit Staged”.

image

11. See the history again. This time, VS adds another commit on top of everything and graph shows dev branch history was kept.

image

Merge commit default setting in VS

If you don’t want to use “fast forward” commit as default, then you can change it.

1. Back to home of Team Explorer and select Settings.

image

2. Select “Global Settings”.

3. Change the “Commit changes after merge by default” settings as you need.

image

Summary

Merge in Visual Studio is a bit confusing, from my point of view. I hope this clears out what you are really doing! In the next article, I investigate re-basing 🙂 Go to next article.

Ken