Git Branch - Working with branches

Git

In this tutorial we will learn about branches in Git.

What is a branch?

A branch in Git represents an independent line of development having its own working directory, stagging area and committed project history.

The master branch

The first branch that we get when we create a Git repository is called the master branch.

Commonly, the master branch holds the production code, the one running in the production environment and your users are interacting with it.

Development work is generally done in a separate dev branch which is later merged with the master branch when they are ready for production server.

Single project multiple branches.

Generally a project has multiple branches like master, testing, dev1, dev2 and so on.

In the above image we have a master branch and a pointer pointing at the last commit.

All changes committed using the git commit command is saved in the branch we are currently on.

List all the branches

To list all the branches we use the git branch command.

$ git branch
* master

The above output tells us that there is only one branch master and the * denotes that we are currently on it.

Create a new branch

To create a new branch we use the git branch [name] command.

$ git branch dev

The above command creates a new branch called dev and a new pointer pointing at the last commit.

The above image represents the master and dev branch.

Branches are just pointers to commits.

Now, if we list the branches we will see two names.

$ git branch
  dev
* master

Checkout a branch

To start committing changes to a new branch we have to first jump (switch) on it. To checkout a branch we use the git checkout [name] command.

$ git checkout dev
Switched to branch 'dev'

In the above example we are switching to the dev branch.

Now, if we commit changes using the git add and git commit commands then it will be saved in the dev branch as we are currently on it.

More on git checkout later.

Delete a branch

To delete a branch we use the git branch -d [name] command.

$ git branch
  dev
  dummy
* master

$ git branch -d dummy
Deleted branch dummy (was f066f07).

$ git branch
  dev
* master

In the above output we can see that there are 3 branches. Then we are deleting the dummy branch.

The git branch -d [name] command is a "safe" command. Git will not allow us to delete a branch if it has unmerged changes.

Force delete a branch

To forcefully delete a branch we use the git branch -D [name] command. This will permanently delete all the unmerged changes that were made to the branch.

$ git branch -d dummy
error: The branch 'dummy' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dummy'.

$ git branch -D dummy
Deleted branch dummy (was 9be9a2f).

In the above example we have a dummy branch which has some unmerged changes and so we are not allowed to delete it using the git branch -d dummy command. So, we use the git branch -D dummy to delete it.

Rename the current branch

To rename the current branch we use the git branch -m [name] command.

In the given example we have two branches dev and master. Then we are creating a new branch hello. Then we are switching to the hello branch. Then using the git branch -m helloworld command we are renaming the hello branch to helloworld.

$ git branch
  dev
* master

$ git branch hello

$ git branch
  dev
  hello
* master

$ git checkout hello
Switched to branch 'hello'

$ git branch
  dev
* hello
  master

$ git branch -m helloworld

$ git branch
  dev
* helloworld
  master

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT