Understanding version control and mastering git - Branches and more...!!
Tags: git, dev, version
Published: 2022-06-19 16:18:50

---
title: Understanding version control and mastering git - Branches and more...!!
published: true
date: 2022-06-10 22:32:33 UTC
tags: git, dev, version
canonical_url: https://blog.nandan.dev/understanding-version-control-and-mastering-git-branches-and-more
---
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6a37i3x3elst3ywekql.png)
 
In my last blog, We learned about version control and its use in software development. We also understood some of the basic git commands like config, clone, commit, and push. As a beginner both in git and a project, these commands come handy and get us going.

As we grow and our project grows, so does the team. With all the growth, it becomes tough for developers to manage code and keep track of all the contributions. This is where branching comes into the picture.

## What is a branch

In Git, a branch is a new/separate version of the main repository. It represents an independent line of development. It also serves as an abstraction for the edit/stage/commit process which means that whatever changes you make in a branch, do not affect your master/main **_[T&C Apply]_**.

### Some Branching commands

- Create a new branch, But do not switch to it
```
git branch <branchname>

```
- Switch to an already created branch
```
git checkout <branchname>

```
- Create a new branch and switch to the branch
```
git checkout -b <branchname>

```
- List all the branches in your repo, and it also shows what branch you're currently in
```
git branch

```
- Push the branch to your remote repository, so others can use it
```
git push origin <branchname>

```
- Push all branches to your remote repository
```
git push --all origin

```
- Delete a specific branch
```
git branch -d <branchname>

```
- Delete a branch on your remote repository
```
git push origin:<branchname>

```

Well... That was a lot of branching knowledge and contrary to the law of conservation of energy, a branch can be created or destroyed (deleted) and the only way to preserve it, is to finally merge it to **master** or to its **target**.

## What is a merge

Merge is the process of taking the contents of a source branch and integrating them with a target branch.

Remember the **_T&C_** I had put earlier? It's time we trace back.

Let's get into a scenario where you were given a feature to implement i.e. Heart Glasses for your octopus (_Here onwards we will call him Octie_). What will you do?

- You will create the heart\_glass branch from your master branch using one of the above branching commands.
- Once you are done with all your changes and know that the glass will look good on Octie;
- You go ahead and merge your heart\_glass branch to your master branch, and maybe then delete the heart\_glass branch and you will have all your changes in the master now.
- Octie is looking good there, and while you were creating glasses, someone put a hat on Octie. Ain't that nice?

The below image practically sums up the whole process - ![Git Branching - Imgur.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1654895087695/0bPgOydBn.png)

### Some merge git commands

- merge a different branch into your active branch
```
git merge <branchname>

```
- Fetch and merge changes on the remote server to your working directory
```
git pull

```
- View all the merge conflicts
```
git diff

```
- Preview changes, before merging
```
git diff <sourcebranch> <targetbranch>

```

In this blog post, we have deep-dived into branching and merging, and we are one step closer to becoming git masters.

In the next blog post, we will try to cover resetting a branch, tagging, and so much more. I also intend to cover some less-known git features in this series. Do you know you can clone only a subdirectory from a git repo? _More on this later!_

Stay tuned, follow me on social media channels, and subscribe to my newsletter [here](https://blog.nandan.dev/) to get updates on my upcoming posts.