Overview of Git Branch
Mainly in git structure, the programmer keeps their master branch clean in their git repository. Then they are creating a copy of the master branch where they can easily store their last updated code, fixes the bugs, commit it and do many more things. This process is nothing but branching in git structure. By using git Branch command one can generate, remove, do listing, can change the name of branches. Branching is one of the latest features of git used to maintaining version control of code. It indicates your recently updated code without affecting the master branch.
How to Create a Git Branch?
- Create a git branch is a concept used to work on the latest new updates without doing changes in existing code.
- Nowadays, in git and many other different Version controlling tools uses the concept of Branching for a more effective software development process.
- Before starting the process of creating any new branch in our code, we want to take a pull of code, so we have the latest updated code.
- Then one can see a list of all existing branches in git by calling command as:
- It will show all the branches present in the git repository.
- HEAD is used to show the status of on which branch, currently user is working.
- So, it is totally different than HEAD. This works as a pointer to your local branch on which you are currently working.
- git log is a simple command used to check on which branch currently programmer is working.
- If a programmer wants to move from one branch to another, then one can hit command like:
git checkout (branch-name)
- After all this process is done, then you want to push your code on Git repository so you can call a command like:
git push origin <newly-created-branch-name>
- While you want to commit your latest updated code, you must switch on the master branch first from any other branch and then only commit your latest code by using git checkout master.
How to Create a New Git Branch?
There are multiple ways to create a new branch using Git. We will see one by one as mentioned below:
`1) A most basic way to create a new branch using the following command:
git checkout –b <branch-name>
This is the most commonly used method for creating a branch to you from your recent branch and it will change to your branch by using a single command itself.
2) Another way to create a branch by specifying particular branch name in command as follows:
git checkout –b new branch <branch-name>switched to branch <new-branch-name>
In the above scenario optionally you are able to define different branches through which another branch will be created.
4.8 (3,356 ratings)
3) One of the simple ways to create a branch is as shown below:
git branch <branch-name>
By using this simplest command, one can easily create a new branch in git. In this case, the checkout will be done in a background process.
All from the above steps branch can’t automatically allow us to move on a newly created branch. Initially, it puts us on our main branch like :
- git branch
git branch Branch_1
If you want to update manually on the newly created branch then switch on this branch using checkout command.
git checkout Branch_1
Switched to branch Branch_1.
4) Creating a branch through a Commit:
This is one another way to create a branch in git by defining a commit with its hash:
$ git branch <branch-name><hash>
With the help of hash git, it specifies some of the characters among them.
- git branch
git branch commit-branch 635d5b3
This one command does both tasks of creating and checking out of branch.
5) Creating a branch from a Tag:
- As we know one create branch using commit same like than one can create branch using a tag .
- Its use for pointing something in the project’s code history.
- Here tags are working as an identifier.
Here is the syntax for creating a branch using the tag as follows:
git checkout –b <branch-name> <tax>
git branch tag-branch v0.2.23
Let’s consider we are working on some projects with updated commit and we are on the master branch. Suppose we want to do changes in our latest code as per requirements, so we will not directly do changes in the master branch.
We will create one new branch and switch directly to the newly created branch :
- git branch department
- git checkout department
You worked on code and save the file to the department branch
$ git commit –a –m ‘inserted header [department]’
- Later, save all the changes done in a file and check status:
$ git status
- All changes are saved successfully. Let’s commit the code:
$ git commit
- As shown above code we committed all the changes in our newly created branch department.
- Once it gets clear everything is fine, now its time to commit all the code on the master branch:
$ git commit –a –m ‘saved changes [master]’
- By executing the above command we can able to save all our latest code to the master branch of the git repository.
- Merging changes of branch code: It will show the latest updates.
Git merge [branch-name] = git merge Computer
- Suppose we have some unnecessary branches present in our project and we want to remove those branches from the git repository.
- So this can happen with the help of delete branch option as follows:
git branch –d [branch-name]
git branch –d Civil
- Branching in git is used to maintain code, before committing it on the master branch.
- With the help of it, one can verify a list of branches, able to create new branches, remove unnecessary branches and many more operations to do with this functionality.
- Here we have seen different ways to create branch as though checkout command, specifying the branch name, through a commit, by creating a tag, etc.
This is a guide to Git Branch. Here we discuss what is Git Branch? and How to create it with different Examples. You may also look at the following articles to learn more –