Branches in Git

testYourselfGit Git827 1 0

A branch in Git is simply a lightweight movable pointer to a commit and you can create the unlimited number of branches you want.

The most recognized branch is the master. When you create the repository by using git init or git clone, Git creates the default branch named master.

Create a branch

To create a new branch in Git we use the git branch command. The git branch create a new branch on your local machine:

git branch testing

Switch between branches

To switch from one branch to another use git checkout:

git checkout prod-fix

We can also use git checkout -b that is a shorthand for git branch followed by git checkout. Executing the git checkout -b causes that a new branch will be created and then checked out. 

Rename a branch

To change a branch's name, use the git branch -m command in the directory your repository is stored at.

The command takes two arguments: the current name and the new name.

git branch -m old-branch-name new-branch-name

Remove a branch

To remove the local branch, use the git branch -d command in the directory your repository is stored at.

The command takes one argument: the current name of the branch you want to delete.

git branch quick-fix

Join two branches

When we finally finished working on a new feature and want to merge that changes with the main branch we can use git merge command. The git merge joins two or more development histories together.