Search code examples
gitgit-mergegit-resetfast-forward

What is the difference between git merge --ff <commit> and git reset <commit> --mixed


If I want to update my local branch, say master, to the remote branch gitlab/master, I see two options:

  • I can merge the local branch with the remote branch using a fast forward merge.
  • I can use reset to move my branch to the position of the remote branch.

The first operation can be achieved in my SmartGit GUI by right-click: merge. The latter operation can be achieved by dragging the local branch marker to a new position.

Is there any technical difference between git merge --ff <commit> and git reset <commit> --mixed?

I admit that reset could also be used to move the local branch to another branch, which is not in the fast-forward way.


Solution

  • If the merge resolves as a fast-forward, there should be no differences, but if it's not the case:

    • If you use git merge, you will have a merge commit and all commit from both branches.
    • If you use git reset, you will have only commits from gitlab/master and no merge commit. The commits from master absent from gitlab/master will be uncommited (reset) but their content will be present in the working directory.

    You can try this yourself, with the follwing two scenari:

    (Initialisation, common two both scenari, we use master as gitlab/master and branch1 as master)

    # Common commit
    git init
    echo a > a; git add .; git commit -m"a"
    # Commit on branch1
    git checkout -b branch1
    git init
    echo b > b; git add .; git commit -m"b"
    # Second commit on master
    git checkout master~1
    echo c > c; git add .; git commit -m"c"
    

    (Merge scenario)

    git checkout branch1
    git merge master
    

    (Reset scenario)

    git checkout branch1
    git reset master
    

    Note also that both options --ffand --mixed are default options and can be omitted.