Git Branching and Merging for beginners.
Tags: git, beginners, tutorial
Published: 2020-11-03 05:47:26

As my previous blog on [Basic Git knowledge for beginners](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p) focused on basic git commands and pushing your code to github, this tutorial will focus on creating multiple branches and merging them together.


After creating the main branch and adding some code in it, we want to change some part of the code in the main branch. So what we do is we create a new branch and make changes in them and <strong>merge</strong> it with the main branch. 

These steps may seem trivial at start, but as our application becomes huge in size, this process is a life saver.

#### <u>Branching for a group of developers:</u>

Branching is also a way for a team of developers to work together in a single project.

When a team member raises an issue in the main branch and wants to solve it, he can simply create a new branch and solve the issue and merge it with the main branch with three lines of git code in terminal. 

After doing all the steps in my [previous blog](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p), hop on to terminal and create a new branch.


<code>git branch branchname</code>

Check existing branches with, 

<code>git branch</code>

After creating a new branch, we have to change to that branch. 

We can achieve that with <strong>checkout</strong>.

In terminal,

<code>git checkout branchname</code>

Now we have changed to that branchname.

We can do these two steps of creating a branch and switching to that branch in a single way by using the <code>-b</code> flag.

<code>git checkout -b branchname</code>


Now you hope on to vscode/atom and change some code you had thought of. After that you add and commit the files with,

<code>git add . </code>

<code>git commit -m 'Changed'</code>

We can do this in single line with -a flag which means adding the files for staging area in that branch and then committing it with git commit command,

<code>git commit -a -m 'Changed'</code>

Now we change our branch to master with,

<code>git checkout master</code>

After these steps we would like to <strong>merge</strong> the other branch with the master one.

<code>git merge branchname</code>

After merging we can delete that branch with,

<code>git branch -d branchname</code>

And thats how you create a new branch, make some changes and merge them with the main branch.

Now lets push to github repo, 

<code>git push -u origin main</code>

Now you can go to your github repo and see what changes have happened.


I have to say that, this article just scratches the surface of git branching and merging. For a beginner these above discussed stuff of branching and merging is more than enough to play around with it and understand the crux of them.


There are stuffs like,

- ####  Merge conflicts when multiple branches are made.

- ####  Stashing and cleaning the branches to avoid conflicts.

And some other stuffs.

This article gives you insight on how to create a single branch and merge it with main branch. 

In upcoming articles, I will talk about the above mentioned merge conflicts while dealing with multiple branches and other advanced git stuff.

#### Make sure you are following me for those articles!


### <u>My Personal Experience:</u>

Git Branching is the most important aspect of git and I use it everyday if I want to change my code in repo without breaking anything. Make sure you master them by practicing them repeatedly.


### <u>Summary of article:</u>


<code>git checkout -b branchname</code>

<code> git commit -a -m "committed"</code>

<code>git checkout main </code>

<code>git merge branchname</code>

<code>git branch -d branchname</code>


### Thank you for reading!

If you like this article, Unicorn this one! Heart/Like this one and save it for reading it later.

### <u>My Other Articles: </u>

- [Git for beginners](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p)

- [Universal CSS properties everyone must know](https://dev.to/gautham495/universal-css-properties-everyone-must-use-4kie)

- [Create-react-app](https://dev.to/gautham495/creating-a-new-react-app-with-create-react-app-4e9m)


- [Change headers in react with react-helmet](https://dev.to/gautham495/changing-headers-in-react-with-react-helmet-d0d)
- [Know How to apply box-shadow on all four sides.](https://dev.to/gautham495/know-how-to-apply-box-shadow-on-all-four-sides-43pn)