Git & GitHub Tutorial - Part 6 - git branch, checkout or switch branch

Branch

When multiple people working on the same project or multiple tasks doing in the same project same time it is necessary to use branches for different features. When a feature or task is complete it could be merge to the main branch. Otherwise, it would be very difficult to work on the same branch for different features or different users and there has always chances to mess up the project by committing randomly. So it is good to create branches for different features and merge themes after ready. It also ensures that when a feature is ready you don't need to wait for other pending features to complete you can merge the complete service or feature. Like in an eCommerce project I need to do the following features

  1. Shopping cart design change
  2. Implementing payment gateway
  3. Tracking delivery

So I can create three separate branches to complete these three features and assign different developers. So when the shopping cart branch is done I can merge it without waiting for the other two features or branches.


We are already in a branch called main. To see your default branch run command git status. When we create a git repository it creates a default branch called main or master as your settings.


Current branch state

If we run the command git log we can see some history of our branches. At the top of the log we can see something like this 

commit 9ba283706c971748786de4fefd59f26e5b2ffda0 (HEAD -> main)

Here the hash (9ba283706c971748786de4fefd59f26e5b2ffda0) is the unique identifier generated by the git and the (HEAD -> main) representing the current state of the branch. That means our current branch is at this situation


To see all created or existing branch we have to run command 

git branch

This will show all available branch and you can see * sign before your current branch, means the branch you are working in.

To create a new branch we have to run the command git branch nameOfTheBranch. Suppose if I want to create a branch named develop I have to run the following command

git branch develop

If we run the command git branch again we can see the branch develop. And this branch is created from the head of our main branch. It is said before that the head means the current or last committed state of our branch. To see it if you run git log this will show something like this 

commit 9ba283706c971748786de4fefd59f26e5b2ffda0 (HEAD -> main, develop)

That means now two branches main and develop referencing to the same point

To change branch we have to run the git switch command with the branch name we have to change like if want to switch develop branch I have to run 

git switch develop

Now if I change something (I just added h3 on line 12) and commit

Then if I run the git log command we can see the most recent commit is in the develop branch and our main branch is one commit behind. That means our develop branch is updated from the previous head. 


So if now I change the branch from develop to the main I can see that the code is in the same situation I left. 


To switch branches you can also use the checkout command like this.

git checkout branchName

If you want to create and switch the branch at a time you can use the -c flag with the git switch command. It will create a new branch and switch to it. Like

git switch -c branchName

If you want to checkout command to create and switch branch at a time you have to use -b flag. Like

git checkout -b branchName


If you miss the previous content you can read it from here

Read: 13
Login to comment

Recent Comments