git

To share this page click on the buttons below;

GIT branches

A branch in GIT is just a pointer to a commit. A commit is a structure that points to a tree, the tree is the directory structure that points to blobs which are the files in the repository. Each commit keeps track of the commit from whom it has been derived. The active branch is pointed by an additional pointer called HEAD.

Create

To create a branch:

git branch <branch_name>

Move to

To move the working copy to the branch (although in reality just the HEAD pointer is moved to the desired commit).

git checkout <branch_name>

Create + move

To create a branch and immediately go to that branch

git checkout -b <branch_name>

Delete

To delete a branch

git branch -d <branch_name>

It is not possible to delete a branch which has not yet merged. But to force the deletion of a branch not yet merged it is possible to use

git branch -d <branch_name>

Merge

  1. move to the branch that is the destination of the merge
  2. run git merge <branch_name> where is the branch containing the changes we want to merge with the destination branch (the one we moved on point 1)

A merge with a branch which is an ancestor of the branch we want to merge is immediate because just the pointer is moved.

In case a branch is merged with some branch which has been modified conflicts can arise. To finalize the merge:

  1. all conflicts have to be solved
  2. file with solved conflict have to be added
  3. changes have to be committed

Merge tool

To set the tool to be used to solve conflicts

git <mergetool>

Information

To show all the branches

git branch

To show all the branches and the last commit for each branch

git branch -v

To show only merged branches

git branch --merged

To show only branches which are not merged yet

git branch --no-merged

Tracking branches

When a remote branch is checked out it is automatically created a tracking branch. Tracking branches are branch with a direct bind with a remote branch.

To share this page click on the buttons below;