Git Tutorial - Learn Git Branching in Less Than 6 Minutes
Git Tutorial - Learn Git Branching in Less Than 6 Minutes
Educative
Git is one of the most widely used version control systems and is an important tool for
every developer to know. One of the greatest benefits of Git is its branching capabilities.
Git branching is a fundamental aspect of your version control workflow. Today, we’ll
discuss how to create, delete, merge, and rebase Git branches. Afterward, we’ll cover the
next steps you can take to further your Git knowledge.
We'll cover:
What is branching?
Creating branches
Deleting branches
Merging branches
Rebasing branches
Git concepts to learn next
What is branching?
https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p 1/6
What is branching?
9/18/21, 10:17 PM Git Tutorial: Learn Git Branching in 5 minutes - DEV Community 👩💻👨💻
Imagine you’re working on a project with your team, and you’re creating a new feature
that requires a lot of changes to the codebase. In the process, you discover a bug that
needs to be fixed. This bug is related to your new feature, and you don’t want to affect
your code. This situation could become complicated. Where will you store the code you’ve
been working on?
That’s where Git branches come in. Branching is a core concept of source control that
allows you to separate your work into different branches so you can work freely on
your source code without affecting anyone else’s work or the actual code in the
main branch.
With Git, you begin with a single primary branch called main. Git allows you to create as
many branches as you want.
Note: You don’t always have to create branches from the main branch. You can create a
new branch from any other branch.
In your separate branches, you’re able to experiment and test without directly affecting
your source code. Branching allows you and your team to switch between contexts
without worrying about affecting different branches. It’s a great tool for teams
because it makes collaboration easier, convenient, and flexible.
Note: Git branches are different from SVN branches. Git branches are useful in your
everyday workflow, whereas SVN branches are used in large-scale development efforts.
Creating branches
A local branch is only accessible to you on your physical device. Before you can push a
branch to a remote repository, you need to create a local branch first.
If you already have a local branch, you can use git checkout :
If you don’t already have the branch you need, you can create a new branch like this:
https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p 2/6
9/18/21, 10:17 PM Git Tutorial: Learn Git Branching in 5 minutes - DEV Community 👩💻👨💻
Now that we know how to create a new local branch, let’s take a look at how to create a
remote branch.
If you want to push your local branch to the remote repo, you can use git push :
Deleting branches
Now that we know how to create a local branch and a remote branch, let’s learn how to
delete them. We’ll start with deleting a local branch using the -d flag:
Sometimes, Git refuses to delete your local branch. This happens when your branch has
commits that haven’t been merged into other branches or pushed to remote repositories.
This refusal is meant to protect you from accidentally losing your data.
If you’re sure you want to delete your branch, you can use the -D flag. This flag forces
deletion.
Merging branches
Git merge is an essential function of branching. Merging allows you to join two or more
branches through a commit Only the target branch is changed In our case this would
https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p 3/6
9/18/21, 10:17 PM Git Tutorial: Learn Git Branching in 5 minutes - DEV Community 👩💻👨💻
branches through a commit. Only the target branch is changed. In our case, this would
be the main branch. The history of your feature branch is preserved.
Let’s think back to the feature you’ve been working on with your team. Your whole team
has access to the main branch that you uploaded to the remote repository. You’re working
on a small fix to the feature, and you don’t want your work to affect the main branch.
You can create your own branch to experiment and test your changes. Once it’s
completed and tested, you can merge that branch into the main so it’s accessible to
everyone.
In our scenario, let’s say you want to merge your experimental branch, called myfeature ,
into the main branch. Before merging, you should do a git fetch to gather up-to-date
information about the remote repository.
Then, go into the branch you want to merge your changes into and perform git checkout .
You can check your project’s commit history on your repository to verify the merge. If
your merge is successful and your work on myfeature is complete, you can delete that
experimental branch since all the changes are now integrated into the main branch.
Rebasing branches
Git rebase is an advanced concept in Git. It can seem a bit complicated, but we’ll go over
the basics. Rebasing and merging are pretty similar. Both options take commits from a
branch and put them onto another branch.
The major difference between rebasing and merging is that rebasing erases the history
of your feature branch after it transfers the work from the feature branch to the
main branch. Some developers prefer this method because it ensures a simple review
process.
https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p 4/6
9/18/21, 10:17 PM Git Tutorial: Learn Git Branching in 5 minutes - DEV Community 👩💻👨💻
Here’s how to use rebase:
Cherry-pick
Advanced git repository manipulation
Pull requests
Git commands
To get started with these concepts and more, check out Educative’s course Learn Git the
Hard Way. This course takes you through Git basics to more advanced concepts. By the
end, you’ll have a strong understanding of Git, which will serve you well throughout your
career.
Happy learning!
Discussion (0)
Code of Conduct
•
Report abuse
Educative
Try a preview
https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p 6/6