First, what is Git and what is it used for?
This is a version control system that is commonly used among all. It remembers every change made to a file, config, or script so that there is a trail of the changes and you can go back to specific versions if needed. Moreover, it also makes collaboration easy, allowing changes by different users to get merged in just one source.
It runs locally and saves all your files with their histories on the system. Online hosts like GitHub or Bitbucket can also be used to store file copy and their revision history. When you have a centrally located place from where you can upload and download changes from others allows you to collaborate with developers more easily. It can merge changes automatically so that two people can work on different parts of the very same file and then merge the changes without losing each other’s work.
What are branches in Git and how do they work?
This is just like tree branches, to explain analogically tree branch is attached to the central part of the tree which is known as the trunk. As branches are able to generate and fall, the trunk however remains compact and is seemed to be the one and only part through which can say that it is alive and standing.
In the same way branch in Git in a way in which we keep developing and coding new features or modifications to the software without affecting project’s main part. The branches create another development line in the project. The default branch which is also known as the primary branch is the master branch. As soon the repository is created so does the main branch.
It is light in weight but in what terms? Well, in terms of data that is carried and then less which is created doing that, in other version control systems like SVN, creating branches is a cumbersome process. What happens is one branch creates the whole main code from the main branches and gets copied to the branch which has been newly created. In Git, the code gets separated from the point of creation of the branch only. Once it is created, we can switch it to another branch and begin with the development process.
The benefit and need of using a Git Branch
As you decide you create a project and an issue is there then Git branches come to the rescue. They as mentioned above create another line of development that is completely different or even isolated from the main master branch. Doing this has many advantages, assume that you are developing any project with help of your team and you finish a feature. You make a call to the client to request them to see the feature but they are busy so what you do is send them a link to have a look at the project.
With branches, we can work independently and with different modules and then merge them once we are finished with their development. It might find a cumbersome process but these branches are swift to be both created and destroyed. With a simple command, you can perform such processes plus they are easy on the pocket as well especially keeping their size in mind.
Git branches help the team which is present in various parts of the world, work independently on independent features that would combine to produce a greater project. The branches however are very flexible. Now use of branches does not mean that you are using them for different features.
For any reason, we want we can use the branch in Git. Branches are created for different teams working on the project. Moreover, you can create them for any other feature which you are creating in the project. For different lines of development, we can create a branch and for other reasons as well. It all depends upon you.
4 Ways To Do Git Branch – Master Git Branches
1: Switch your branch to a specific commit
Now at times, you might need to switch to a new branch but jump to a commit you already know about, right?
To check a new branch from a certain starting point what you need to do is execute the “git checkout” command and then specify “-B” option as well as the branch and its start point.
[email protected]:~# git checkout –B
To check specific start points, you need to list all the commits which you need to do with the help of “git log” command.
[email protected]:~# git log --online --graph
Now to switch towards the master branch, on this certain commit, we are going to be using the “git checkout” command so we can tell it which one is the “master” branch along with the commit.
[email protected]:~# git checkout –B master
To check if you are on the specific commit or not you are going to use “git log” command once again.
[email protected]:~# git log –graph
2: Use the Git Checkout method
With this command, you can navigate between the branches which have been created by the git branch. It updates files that are working in the directory to match the version stored in that branch and orders Git to record all the new commits on that branch.
This is at times confused with git clone; clone works to fetch the code from a remote repository, as alternate checkout words to switch between different versions of code present on the local system already.
Now, one of the easy ways to do this is using the “git checkout” command and specifying the branch name to which you want to switch to.
If there is no destination branch then you need to append the “-b|” option else you shall not be able to switch to that branch.
[email protected]:~#git checkout –b
If you want to switch to the master branch to another branch called a feature in the repository then you need to be sure that the target branch exists by running the “git branch” command.
[email protected]:~#git branch
When you are sure that branches exist then you can switch from master to feature branch by executing the “git checkout” command:
[email protected]:~#git checkout ninja
3: Change branch with the help of Git Switch
Simply use the “git switch” command to switch branches on Git quickly and specify the branch name you want to switch.
If there is no destination branch then you need to specify the “-c” option else you shall receive an error message when switching to that branch.
[email protected]:~# git switch –c
Now let’s say that you intend to switch to the feature branch from the master branch then you need to use the “git switch” command and then specify the destination branch.
[email protected]:~# git switch ninja
If you want to switch to a non-existing branch then you shall receive the following error message:
[email protected]:~#git switch
Now, to solve this error you need to append “-c” option to “git switch” command. This tells git that you are intending now to switch the branch.
[email protected]:~#git switch –c
4: Checkout the Branch Remotely with Git
So you can start using a remote branch be sure to catch it with with the command “git fetch” at first.
After this switch to it by executing “git checkout” with the “-t” option and branch name.
[email protected]:~#git fetch
[email protected]:~#git checkout –t
The “-t” option stands for the track which is used to create your branch, this helps to setup the upstream branch by itself.
Let’s assume that you have a branch by the name “remote-branch” on the origin remote.
To check this out you shall need to execute the checkout command and specify the information which has been specified above.
In this you can see that information related to remote tracking was automatic: set as consequence, if you commit any kind of modification you need to push them to the upstream branch automatically.
Frequently Asked Questions
How to change my branch to the main branch?
This is a question asked A LOT by users who have just started working with Git. Changing the branch you are currently working on or even a new branch to the main one can be done by renaming your local one: Step 1: Move the master branch with Git branch –move Step 2: Now set the main upstream branch to the main one with the (–set-upstream). Step 3: Now push the changes you made to the origin and delete the original master.
Will I lose any data if I change branches?
Yes, unless you shelve your changes. You can do this by using the below commands: Step 1: Run (git stash) Step 2: Now checkout your branch Step 3: Now apply it to your stash with “apply“.
Knowing how to change a branch in Git is very useful when you are working in an enterprise that manages multiple branches. This could also simply be one project that has a ton of branches! Knowing how to switch between branches in Git is vital so you are efficient. Let us know in the comments which method you use our personal favorite is using Git Checkout!