15 Merging Commits Between Branches

You have to merge changes from another branch into your current branch in order to be able to use them. The simplest way to do this is through git merge.

git merge takes two options: the name of the other branch you want to merge and the optional local branch you want to merge into. You can leave off the current branch when you’re merging changes into your current branch.

By default, git merge commits the merged changes if they can be successfully merged. You can short-circuit this with the --no-commit option. This is useful when you want to review, and possibly edit, the changes from the merge before making a commit.

We touched on how Git attempts to merge commits in the introduction to Part III. Fast-forward merges are often useful, but sometimes you want to log that a merge happened. This is common in projects when a big feature that was developed in a separate branch is merged in; it provides a single commit you can revert if it needs to be removed in the future. You can do this with the --no-ff option. It forces Git to create a merge commit, showing that the two branches were merged.

Another extra you can add when merge commits are created is the --log option. Traditional merge log messages contain Merge branch ’development’. There are two ways you change this. First, you can add the --log, which adds the subject line from each commit to the merge commit message. Or, you can use -m and a message, which lets you specify the entire message just like git commit.

Git tries to figure out how to merge all the changes, but sometimes it can’t. This is called a conflict and requires your intervention. Task 24, Handling Conflicts shows you how to handle these cases.

What To Do...
  • Merge changes from development to the master branch.
     
    prompt>​ git checkout master
     
    Switched to branch 'master'
     
    prompt>​ git merge development
     
    Updating af0fe21..290b0d2
     
    Fast-forward
     
    old/README.rst | 8 ++------
     
    1 files changed, 2 insertions(+), 6 deletions(-)
  • Merge changes, but don’t commit.
     
    prompt>​ git merge --no-commit development
     
    Automatic merge went well; stopped before committing...
  • Force the creation of a merge commit.
     
    prompt>​ git merge --no-ff development
     
    Merge made by recursive.
     
    old/README.rst | 8 ++------
     
    1 files changed, 2 insertions(+), 6 deletions(-)
  • Add a one-line log message from each merged commit to the merge message.
     
    prompt>​ git merge --log development
  • Specify a custom log message for a merge commit, if created.
     
    prompt>​ git merge -m "my message" development

    You can use git commit --amend to modify the commit message after the fact too. Here’s an example:

     
    prompt>​ git merge --log --no-ff development
     
    prompt>​ git commit --amend -c HEAD
     
    ...​ editor launches ...

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.7