DEV Community

loading...
Cover image for Git Branching and Merging for beginners.

Git Branching and Merging for beginners.

gautham495 profile image Gautham Vijayan ・3 min read

As my previous blog on Basic Git knowledge for beginners 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 merge 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.

Branching for a group of developers:

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, hop on to terminal and create a new branch.

git branch branchname

Check existing branches with,

git branch

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

We can achieve that with checkout.

In terminal,

git checkout branchname

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 -b flag.

git checkout -b branchname

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

git add .

git commit -m 'Changed'

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,

git commit -a -m 'Changed'

Now we change our branch to master with,

git checkout master

After these steps we would like to merge the other branch with the master one.

git merge branchname

After merging we can delete that branch with,

git branch -d branchname

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

Now lets push to github repo,

git push -u origin main

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!

My Personal Experience:

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.

Summary of article:

git checkout -b branchname

git commit -a -m "committed"

git checkout main

git merge branchname

git branch -d branchname

Thank you for reading!

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

My Other Articles:

Discussion (0)

pic
Editor guide