Git - git merge


How can we merge from a specific branch?

git merge branchName  # merge changesets from branchName (Git >= 1.5)
git merge linux-work

git pull . branchName # merge changesets from branchName (all Git versions)

What are the default behaviors of the 'git merge' command?

If changes were made on only one of the branches since the last merge, they are simply replayed on your other branch (so-called fast-forward merge). If changes were made on both branches, they are merged intelligently (so-called three-way merge): if any changes conflicted, git merge will report them and let you resolve them, updating the rest of the tree already to the result state; you can git commit when you resolve the conflicts. If no changes conflicted, a commit is made automatically with a convenient log message (or you can do git merge —no-commit branch to review the merge result and then do the commit yourself).

How can we merge our changes from the test branch to the master branch?

If you make a commit while on the test branch, only this branch will point to the most recent commit. The master branch will still point at the commit it was pointing to when you checked out to the test branch. So, if you switch back to the master branch, none of your recent changes made on the test branch will exist on the master branch.

Use the git merge command.

What is fast forward merging?

With a fast-forward merge, your current branch’s pointer moves forward to the most recent commit for the branch being merged in – there is no divergent work to merge together because the latter branch is directly upstream in relation to the former one.

$ git checkout master (switches you to the master branch)
$ git merge test (merges in changes from test branch)
Updating c58d775 .. 8b9205d
index.html| 4 ++
1 file changed, 4 insertions(+)

What is the purpose of the 'git merge --no-ff' command?

When you use the 'git merge --no-ff <branch>' command, instead of a branch simply moving its pointer forward, a new commit object is created. The --no-ff flag is often used to prevent the loss of historical information regarding a merged-in branch.

git merge test --no-ff

What is a 3-way merge?

In cases of branches diverging, fast-forward merges are not an option. A three-way merge can be used, however. This type of merge involves the two latest commit snapshots pointed to by both branches, and their common ancestor. Here is an example of creating a three-way merge:

$ git checkout master
$ git merge test

Instead of just moving the branch pointer forward, Git creates a new snapshot that results from this three-way merge and automatically creates a new commit that points to it. This is referred to as a merge commit, which is special because it has more than one parent.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License