git_merge

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
git_merge [2019/03/06 15:13] – [Query containedness and mergeness] rpjdaygit_merge [2019/03/13 13:09] (current) – [Global merge.ff option] rpjday
Line 66: Line 66:
              /          master              /          master
         D---E         D---E
 +</code>
 +
 +or, depicted another way:
 +
 +<code>
 +        D---E---A---B---C master
 +                          topic
 </code> </code>
  
Line 226: Line 233:
 Welcome to the second edition of the Pro Git book. Welcome to the second edition of the Pro Git book.
 ... etc ... ... etc ...
 +</code>
 +
 +===== Advanced merging =====
 +
 +==== Fast-forwarding ====
 +
 +Recall a fast-forward merge:
 +
 +<code>
 +              A---B---C topic
 +             /          master
 +        D---E
 +</code>
 +
 +You may or may not want that:
 +
 +<code>
 +--ff
 +    When the merge resolves as a fast-forward, only update the
 +    branch pointer, without creating a merge commit. This is
 +    the default behavior.
 +
 +--no-ff
 +    Create a merge commit even when the merge resolves as a
 +    fast-forward. This is the default behaviour when merging an
 +    annotated (and possibly signed) tag that is not stored in
 +    its natural place in refs/tags/ hierarchy.
 +
 +--ff-only
 +    Refuse to merge and exit with a non-zero status unless the
 +    current HEAD is already up to date or the merge can be
 +    resolved as a fast-forward.
 +</code>
 +
 +==== Committing ====
 +
 +<code>
 +--commit, --no-commit
 +    Perform the merge and commit the result. This option can be
 +    used to override --no-commit.
 +
 +    With --no-commit perform the merge but pretend the merge
 +    failed and do not autocommit, to give the user a chance to
 +    inspect and further tweak the merge result before
 +    committing.
 +</code>
 +
 +==== Editing ====
 +
 +<code>
 +--edit, -e, --no-edit
 +    Invoke an editor before committing successful mechanical
 +    merge to further edit the auto-generated merge message, so
 +    that the user can explain and justify the merge. The
 +    --no-edit option can be used to accept the auto-generated
 +    message (this is generally discouraged). The --edit (or -e)
 +    option is still useful if you are giving a draft message
 +    with the -m option from the command line and want to edit
 +    it in the editor.
 +</code>
 +
 +==== Squashing ====
 +
 +<code>
 +--squash, --no-squash
 +    Produce the working tree and index state as if a real merge
 +    happened (except for the merge information), but do not
 +    actually make a commit, move the HEAD, or record
 +    $GIT_DIR/MERGE_HEAD (to cause the next git commit command
 +    to create a merge commit). This allows you to create a
 +    single commit on top of the current branch whose effect is
 +    the same as merging another branch (or more in case of an
 +    octopus).
 +
 +    With --no-squash perform the merge and commit the result.
 +    This option can be used to override --squash.
 +</code>
 +
 +As in:
 +
 +<code>
 +$ git checkout master
 +$ git merge --squash <branch with many commits>
 +$ git commit
 +</code>
 +
 +===== Setting merge options =====
 +
 +==== Per-branch merge options ====
 +
 +From ''man git-config'', you can set (either locally or globally) //per-branch// merge options:
 +
 +<code>
 +branch.<name>.mergeOptions
 +    Sets default options for merging into branch <name>. The
 +    syntax and supported options are the same as those of git-
 +    merge(1), but option values containing whitespace
 +    characters are currently not supported.
 +</code>
 +
 +For example, if you wanted to prevent any fast-forward merges to any ''master'' branch across all of your repositories, you could:
 +
 +<code>
 +$ git config --global branch.master.mergeOptions "--no-ff"
 +</code>
 +
 +==== Global merge.ff option ====
 +
 +For merging, there is at least one repo-wide merge option you can set (see ''man git-merge''):
 +
 +<code>
 +merge.ff
 +    By default, Git does not create an extra merge commit when
 +    merging a commit that is a descendant of the current
 +    commit. Instead, the tip of the current branch is
 +    fast-forwarded. When set to false, this variable tells Git
 +    to create an extra merge commit in such a case (equivalent
 +    to giving the --no-ff option from the command line). When
 +    set to only, only such fast-forward merges are allowed
 +    (equivalent to giving the --ff-only option from the command
 +    line).
 +</code>
 +
 +So for widespread fast-forward merge configuration:
 +
 +<code>
 +$ git config --global merge.ff "true"
 +</code>
 +
 +This setting is superseded by any per-branch fast-forward merge setting.
 +
 +===== Merge strategies =====
 +
 +==== ours ====
 +
 +Technically record a merge while totally ignoring its content -- for record-keeping.
 +
 +Example:
 +
 +<code>
 +$ git checkout master
 +$ git merge --strategy=ours <obsolete branch>
 +</code>
 +
 +How to make ''master'' look exactly like ''develop'':
 +
 +<code>
 +$ git checkout topic
 +$ git merge -s ours master
 +$ git checkout master
 +$ git merge --ff-only topic
 +</code>
 +
 +To do this for an arbitrary commit, create a temporary branch:
 +
 +<code>
 +$ git checkout -b tempbranch <commit ID>
 +$ git merge -s ours master
 +$ git checkout master
 +$ git merge --ff-only tempbranch
 </code> </code>
  • git_merge.1551885198.txt.gz
  • Last modified: 2019/03/06 15:13
  • by rpjday