GIT - The 'm*' Git commands

mail

git merge-base

Usage

git merge-base master feature
Find the common ancestor between commits, which can be used to find the commit at which a branch was created.
  • The description above might be over-simplified, see DISCUSSION for details.
  • Don't forget a branch name is just a reference to the commit that's at the tip of this branch.

Flags

Flag Usage
-a --all Output all merge bases for the commits, instead of just one
mail

git merge

Usage

Join two or more development histories together

Synopsis

  • git merge sourceBranch destinationBranch
  • git merge sourceBranch
    when destinationBranch is the current branch, it can be omitted.

Details

  • There is no such thing like source or destination branches when merging, but this reminds me of the new "arrow" that will be added to the commit tree :
    before :
    	      A---B---C feature
    	     /
    	D---E---F---G master
    after git merge feature master :
    	      A---B---C feature
    	     /         
    	D---E---F---G---H master
  • The order branch_A branch_B or branch_B branch_A matters in that it will replay commits of the 1st one onto the 2nd one since they diverged. This means the whole set of commits "lands" on the 2nd one. This is important when it comes to deleting merged branches.
  • git merge parameters are commit IDs (which is somewhat equivalent to branch names, since all of this relates to objects IDs anyway)
  • Merging generates a new commit that has 2 parents, except if the destination branch remained untouched since the creation of the branch. In such a case, a fast-forward merge will be performed automatically (source).

After the merge (source) :

  1. delete the remote branch
    • git push remote :branch
    • or : git push -d remote branch
      -d is an alias of --delete
  2. delete the local branch :
    git branch -d fullyMergedBranch

Alternate workflow with GitLab & al. :

  1. push the branch to the remote
  2. perform merge request, asking the merged branch to be deleted after a successful merge
  3. then locally :
    1. git checkout master
    2. git pull
    3. git branch -d theBranchThatHasJustBeenMerged

Flags

Flag Usage
--allow-unrelated-histories By default, git merge refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently.
Specifies how a merge is handled when the merged-in history is already a descendant of the current history :
  • --ff (default unless pretty specific situation, please ) :
    • when possible : resolve the merge as a fast-forward, i.e. : only update the branch pointer to match the merged branch; do not create a merge commit
    • when not possible (when the merged-in history is not a descendant of the current history) : create a merge commit
  • --no-ff : create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward
  • --ff-only : resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.
--squash
After :
git merge --squash feature
git branch -d feature
Git complains :
error: The branch feature is not fully merged.
If you are sure you want to delete it, run git branch -D feature.
This is a normal Git behavior (full details). All you have to do is delete the branch as shown in the error message .