Git branches are one of the most important feature that git provides. In a single git repository we can make as many branches as we want.

Suppose you are working in master branch of a git repository and an urgent requirement comes to make a feature but you are not sure that this feature will be used in the product or not. so if your write the code for that feature in master branch itself then it could

  • Affect other functionalities as other developers may be working on master branch itself and you are not sure that what they are going to commit in master branch
  • if in future you want to remove the code related to this feature then it would be difficult to do so because so many commits would be there in the master branch

In this case the best approach is to create a new branch from master branch and do the work related to new feature in that branch.

In this article you will learn some important commands related to git branch

Viewing all the branches

Viewing all the local branches of git repository.


    git branch

Viewing all the remote branches of git repository.


    git branch -r

Creating new branches in git

Suppose the current branch is master and the new branch you want to create is develop

If you want to create develop branch and move to that branch automatically


    git checkout -b develop

Switching between branches or changing branches

Suppose the current branch is master and the branch you want to move to is develop


    git checkout develop

Deleting a branch

Suppose you want to delete develop branch

If you have done some work in develop branch and you have merged those work to master branch then you can delete develop branch using


    git branch -d develop

But in case you have not merged your changes to master branch then if you use above command then git will throw an error that the current work in the branch will get lost.

so you need to delete develop branch forcibly using


    git branch -D develop
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x