Categories
Linux

How to Change Branch in Git

A Git Branch is a separate line of development in a software project. User can create a branch, and keep on committing their changes to this branch without messing the original 'master' branch.

Advertisements

Usually, each developer working on a code makes his/her changes in a separate branch. Git offers features to merge the branch with the master branch after changes are done. The branches can also be named according to what kind of changes they contain. This allows proper transparency and decentralization in development teams, and hence these practices are now widely followed conventions in the field of Software Engineering.

In this article, we will see how to change the current branch in a Git project using the git checkout command.

First of all, to see all the existing branches in a Git project, go to the project directory and run:

git branch
Advertisements

As we can see, the branch we are currently in (master) is highlighted.

Before we switch to another branch, if there are any changes done under this branch, they must be committed. Otherwise, Git might prevent the branch change, if there is a conflict in the branches.

To commit the changes, run:

git commit -m "Minor Changes in Code"

Note that the string after the -m flag is a mandatory commit message to be specified with each commit, explaining the changes done during the commit.

Finally, to checkout / change to another branch, run:

git checkout <branchname>
Advertisements

Eg. to checkout to the branch ‘testing’:

Now we can carry out the required changes in our branch.