TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Git Branch

Git Branch with Git Tutorial, Git Introduction, Git, What is Git, GitHub, What is GitHub, Git vs GitHub, Git Mercurial, Installation of Git for Windows, Installation Git for Ubuntu, Git Environment Setup, Git Command Line Tools, Git Tools, etc.

<< Back to GIT

Git Branch

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.

Git Branch

Git Master Branch

The master branch is a default branch in Git. It is instantiated when first commit made on the project. When you make the first commit, you're given a master branch to the starting commit point. When you start making a commit, then master branch pointer automatically moves forward. A repository can have only one master branch.

Master branch is the branch in which all the changes eventually get merged back. It can be called as an official working version of your project.

Operations on Branches

We can perform various operations on Git branches. The git branch command allows you to create, list, rename and delete branches. Many operations on branches are applied by git checkout and git merge command. So, the git branch is tightly integrated with the git checkout and git merge commands.

The Operations that can be performed on a branch:

Create Branch

You can create a new branch with the help of the git branch command. This command will be used as:

Syntax:

$ git branch  <branch name>

Output:

Git Branch

This command will create the branch B1 locally in Git directory.

List Branch

You can List all of the available branches in your repository by using the following command.

Either we can use git branch - list or git branch command to list the available branches in the repository.

Syntax:

$ git branch --list

or

$ git branch

Output:

Git Branch

Here, both commands are listing the available branches in the repository. The symbol * is representing currently active branch.

Delete Branch

You can delete the specified branch. It is a safe operation. In this command, Git prevents you from deleting the branch if it has unmerged changes. Below is the command to do this.

Syntax:

$ git branch -d<branch name>

Output:

Git Branch

This command will delete the existing branch B1 from the repository.

The git branch d command can be used in two formats. Another format of this command is git branch D. The 'git branch D' command is used to delete the specified branch.

$ git branch -D <branch name>

Delete a Remote Branch

You can delete a remote branch from Git desktop application. Below command is used to delete a remote branch:

Syntax:

$ git push origin -delete <branch name>

Output:

Git Branch

As you can see in the above output, the remote branch named branch2 from my GitHub account is deleted.

Switch Branch

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command. To switch between the branches, below command is used:

$ git checkout<branch name>

Switch from master Branch

You can switch from master to any other branch available on your repository without making any commit.

Syntax:

$ git checkout <branch name>

Output:

Git Branch

As you can see in the output, branches are switched from master to branch4 without making any commit.

Switch to master branch

You can switch to the master branch from any other branch with the help of below command.

Syntax:

$ git branch -m master

Output:

Git Branch

As you can see in the above output, branches are switched from branch1 to master without making any commit.

Rename Branch

We can rename the branch with the help of the git branch command. To rename a branch, use the below command:

Syntax:

$ git branch -m <old branch name><new branch name>

Output:

Git Branch

As you can see in the above output, branch4 renamed as renamedB1.

Merge Branch

Git allows you to merge the other branch with the currently active branch. You can merge two branches with the help of git merge command. Below command is used to merge the branches:

Syntax:

$ git merge <branch name>

Output:

Git Branch

From the above output, you can see that the master branch merged with renamedB1. Since I have made no-commit before merging, so the output is showing as already up to date.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf