Git - Branch

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

remote/name
  • 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

5 - Usage

Creating two features:

  • heart glasses
  • cowboy hat

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

Create a branch off of develop called “feature-[feature name]”, work and commit into that branch.

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
  master

or

git rev-parse --abbrev-ref HEAD
branch-2.3

7.2 - Create

7.3 - Fetch

fetch all the remote branches from origin

git fetch origin

7.4 - List

7.5 - Checkout

Check out an existing branch

7.5.1 - From the actual commit

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

where:

7.5.2 - From a tag

See checkout

7.6 - 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.7 - Switch

See Git - Checkout

git checkout branchName
Switched to branch 'branchName'

7.8 - 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

code/version/git/branch.txt · Last modified: 2018/07/17 12:19 by gerardnico