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

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

Details

  • 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.
  • destinationBranch can be omitted if it's the current branch :
    git merge sourceBranch
  • 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.
--squash