# Git Advanced-1

## Git Branching

### What is Git Branch?

Git Branch is an unavoidable concept when we are studying Git. Branching is a part of the daily development process. Git Branches are effectively a pointer to a snapshot of your incremental changes. It helps to encapsulate and keeps the changes made by you up to you only. It saves the main code base from any unstable code and gives you a chance to clean up any mess that is made by experimenting.

Imagine there are 5 developers in your team and you people are writing incremental pieces of code daily so, you can not just write the code on the central repository because if there are any issues in your code it can cause a huge problem. So here comes the concept **Branch,** consider it **as a Rough Page** for you which is limited to you and you can do whatever you want like adding a new feature, debugging or no matter how big or small changes you made this will not affect the main code that is already approved and in there in the main code base.

### How does it work?

When you create a branch, it comes out as a separate line from the **main** branch bringing all the files or codes that are available in the main branch until that time.

Then it enables you to build your feature while keeping the previous code with yourself without impacting the main code base. You can create branches from any point and start coding your feature. When you feel that your feature is stable and it gets approval to be a part of the main code base, you can simply merge the piece of code to the main code base.

![Source: atlassian.com](https://cdn.hashnode.com/res/hashnode/image/upload/v1673404779428/6eb0774d-4ed0-4928-a033-fd5062d83444.png align="center")

### Command For Branching

**1- Listing the branches that are available in the repository**

`$ git branch` : Helps to know the branches present in your local repository.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673405663508/9e2fa981-fddf-4683-a32c-f77d9d4a02ee.png align="left")

`$ git branch --list` : It is an alternative to ***$ git branch*** which also lists all the branches available.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673405779088/99303aa4-ae14-41df-bf28-fcd42cc8d8f8.png align="left")

`$ git branch -a` : It shows the list of the remote branch. In the below picture, **origin** is a variable that points to the remote repository and it contains a branch called **master** as well.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673406549098/84416216-4268-4d69-8d49-8b9b16414f7d.png align="left")

**Note:** Currently the above pictures, show the master branch which is created by default when Git is initialized. It is the base branch of any repository. As there are no further branches created hence, it lists only the **master** branch.

**2- Creating Branches and Operating inside them**

`$ git branch <branchName>` : It is used to create a new branch but it won't switch to the newly created branch automatically. Now when you command **git branch** or **git branch --list** it will show the list of branches and the current branch you are in by making it green.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673406901114/e8bcb127-ae88-4baf-b77a-7bac03578598.png align="left")

`$ git checkout <branchName>` : It switches us into the specified branch name

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673407314676/cbfed67b-81b6-483b-81f0-8c4fbd1cd63c.png align="left")

`$ git checkout -b branchName` : It creates the branch and switches to it at the same time.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673428896214/9572abf5-f799-4254-8e2f-3f9548ecbe63.png align="left")

**3- Deleting Branches & Renaming Branches**

`$ git branch -d Dev` : It is a safe way of deleting branches. This command prevents the deletion of the branch if it has any unmerged changes and the Dev branch has one unmerged file named branch.py

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673415268248/801028e8-db92-418e-be3f-3041a4d3e202.png align="left")

You can see the log below where the **Head** is pointing to the **Dev** branch and it has an unmerged file:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673411880552/009fa49f-1a56-4e84-995d-c1717f80203f.png align="center")

`$ git branch -D Dev` : It is used to **forcefully delete** the branch even if it contains unmerged files.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673412196370/de4658f0-3771-47a8-96e2-b68995176a77.png align="left")

`$ git branch -m DevRenamed` : This renames the branch name.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673412594651/df12680e-1139-47c9-9449-d4313db80883.png align="left")

### **Creating a branch from another sub-branch**

As you already know we have a **master** branch from which we created a branch called **Dev.** Now, have you ever thought about what happens when we create another branch from **Dev**? Let's see.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673417562461/dc845dbd-0bd3-422d-9b79-1694162e2861.png align="left")

Here as you can see the **HEAD-&gt; dev\_enhanced**, **Dev** which means **dev\_enhanced** is currently in sync with **Dev** as it is coming out of **Dev itsel**f and the HEAD is pointing at dev\_enhanced.

When we are creating a branch from another sub-branch, it brings all the commits from the master as well as from the branch it is created from because it's parent has all the files that are present in the master.

### Remote Branching

So far we have been trying all the commands on the Local system. We can also do the same on remote repositories as well.

`$ git remote add new-remote-repo` [`https://github.com/rkn1999/devops-batch2-zero-to-here.git`](https://github.com/rkn1999/devops-batch2-zero-to-here.git) : This is used to add a remote repository however, make sure a remote repo must first be configured and added to the local repo config.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673414291647/162fa277-1b1b-4dc5-9b17-f4f6aef3fb73.png align="left")

`$ git push new-remote-repo` : Now we pushed the changes to the remote repo.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673414350622/66710e71-e792-4b07-8b21-15c83a47cf17.png align="left")
