Git

Git Rename Branch Example

In this example we shall learn how to rename a Git branch.

1. Pre-requisites

To see this example in action, Git should be installed. It can be downloaded from here.

2. Git Branch

Git branches aren’t like SVN branches which most of us use. The main purpose of SVN branches is to capture large-scale development effort which is again very occasional. On the other hand, Git branches are an integral part of one’s everyday workflow.

A Git branch represents an independent line of development and this serve as an abstraction for the edit/stage/commit process. One can think of them as a way to request a brand new working directory, staging area, and project history. And all the new commits are recorded in the history for the current branch.

In lehman’s terms, purpose of creating is to keep master branch clean, without any changes. Each time, that a feature is to be developed, a separate branch shall be created for it, which will be later copied to master branch.

3. Renaming Git Branch

To understand this example, we shall first create a Git repository, then will create its branch, and finally will rename it.

First step, we shall be creating a directory for our master branch.

Create directory for Master Branch
Create directory for Master Branch

Once the directory has been created we shall be initializing our branch with git init command.

Initialize Git Branch
Initialize Git Branch

Post initialization of master branch, we shall add some files and shall commit the same using git commit command.

Add files to Master Branch and Commit
Add files to Master Branch and Commit

Now that our master branch is ready, we shall create a directory where we shall be creating a new branch.

Create directory where new Branch shall be created
Create directory where new Branch shall be created

And here we shall be cloning the original master branch using git clone command.

Clone Master into newly created branch directory
Clone Master into newly created branch directory

And now we shall be creating a new branch using command git branch <branch_name>. Available branches can be seen using command git branch.

Create Branch
Create Branch

Till now we have successfully created our new branch, it’s time to switch to it. We shall use command git checkout <branch_name> to switch to it. Information on current branch can be seen using command git status.

Switch to new Branch
Switch to new Branch

And then a thought came that we should rename our branch to a more appropriate name. For this we shall be using command git branch -m <new_branch_name> to rename existing branch.

Rename Git Branch
Rename Git Branch

4. Conclusion

In this example, we learnt basics of git branch command and how to rename existing git branches.

Saurabh Arora

Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button