Git - Branch

> Code - (Programming|Computer) Language > Code Versioning > Git

1 - About

Branch management in Git.

A branch is a refs.

         CA1--CA2--CA3 <-- Branch A
 C1--C2--C3 <-- master
         CB1--CB2 <-- Branch B

The term "branch head" (or just "head") reference to the most recent commit on a branch. In the example above, the branch head of:

  • the branch A is the commit CA3
  • the branch B is the commit CB2

The git branch list, create, or delete branches.


3 - Name

The identification of branch may have a / or a space

  • With a space, , you're referring to a remote branch (branch on a remote machine over the network)
remote name

4 - Type

4.1 - Local

A local branch is a branch that exists only on your computer. A local tracking branch is a local branch that is tracking another branch.

4.2 - Remote Tracking

4.3 - Tag

4.4 - Fork


5 - Usage / Strategy

Creating two features branch:

  • heart glasses
  • cowboy hat

Each feature is added back to the master individually. So if glasses are finished before the cowboy hat, no problem.

More see Git - Workflow (Branching Strategy)

6 - Example of lifecycle

Example of workflow

# Create a feature branch
git checkout -b feature-myfeature develop
# Merge your work
git checkout develop
git merge --no-ff feature-myfeature
# Delete your branch
git branch -d feature-myfeature
# Integrate your work into the master repo
git push origin develop

7 - Management

7.1 - Working

The starred branch is the local working branch

git branch
* branch-2.3


git rev-parse --abbrev-ref HEAD

7.2 - Create

7.3 - Update

Keeping your branch current with a parent branch (master for instance)

git rebase is one way of doing it.


Master Update

  • Switch to the local copy of master
git checkout master
git fetch origin
:: git fetch upstream
  • Merge
git merge origin/master
:: git merge usptream/master

Rebase (on the branch)

  • Go to the branch
git checkout branchName
  • Rebase
git rebase master

7.4 - Fetch

fetch all the remote branches from origin

git fetch origin

7.5 - List

7.6 - Checkout

Check out an existing branch

7.6.1 - From the actual commit

cd path/to/repository
git checkout brancheName
# Example
git checkout 1.01_hello_world 


7.6.2 - From a tag

See checkout

7.7 - Stash

stash In case of modification, revert to the HEAD branches

git stash # keep the modification in a stash branch
git pull # get the last one
git stash pop # Destash the modification

7.8 - Switch

See Git - Checkout

git checkout branchName
Switched to branch 'branchName'

7.9 - Share

When you want to share a branch with the world, you need to push it up to a remote that you have write access to.

git push <remote> <branch>

8 - Documentation / Reference