What Are Branches?
Branches are a way of diverging from the main development line and keep working on the project without affecting the main development line ("main development line" refers to mainly master
or main
branch). That means every branch has its own commits which can be different from other branches and they do not meddle in each others stream unless we manually perform operations like merging, rebasing or cherry-picking.
What Are Branches Used For?
As branches allow us to work without messing up main development line, branches are used for adding new features, solving bugs, hot fixing a critical bug etc. Basically everything that needs to be isolated at the time of development, uses branching.
We can have a new branch for every ongoing task, commit changes in that branch and finally when the work is done, we can merge that branch into main development line(in our case master
branch).
In the previous posts in this series, you might've noticed that there is master
written in the terminal screenshots. That stands for which branch we're currently working on.
Now that we've discussed what Git Branches are and what they're used for, we can move on to how to use them. There are two commands that we generally use to create and navigate through the branches which are git branch
and git checkout
. The latter one can also be used with commits, but for now, we'll use it with branches.
Command: git branch
Running git branch
gives you a list of existing branches in the project and the current branch is shown with a *
at the start of its name in the list. Here is the repository that I am working on for this series.
Running git branch <name-of-branch>
will create a new branch.
Command: git checkout <name-of-branch>
Running git checkout <name-of-branch>
allows us to navigate to the branch name that we provided.
We see that running git checkout new-list
switched the branch and when we ran git branch | cat
, the *
is now with new-list
which means that new-list
has become our current branch.
Now let's add a new list of friends in this branch. Since blog posts and books and are our biggest friends, we're gonna add them before everything.
After creating the file and writing to it, we can finally commit that file and push it.
We see that there was a fatal error when we ran git push
. The error put in simple words means that the new branch named new-list
doesn't exist in GitHub(remote). And it shows us a suggestion at the end to run to create this branch in GitHub, which is git push --set-upstream origin new-list
.
The command git push --set-upstream origin new-list
is equivalent to creating a new branch in remote named origin
and pushing our new local commit to that remote branch.
The thing to keep in mind is that whenever we create a branch with
git branch
, it is only on our machine, we need to push it to remote manually after creating it.
Shortcut: git checkout -b <name-of-new-branch>
Running git checkout -b <name-of-new-branch>
will not only create a new branch but also navigate to that new branch so you don't have to run git branch <name-of-new-branch>
and then git checkout <name-of-new-branch>
.
Yay ๐งจ๐งจ๐งจ, now you know how to create branches and switch between them, and a shortcut that does both by default!
This post is originally written on my blog.
Top comments (4)
hi, i wanted to know how to convert my code to another code that i commited. is there is something similar to branch's checkout for that?
It'd help if you could make the scenario a bit clearer.
In general, if we want to roll back to older commits, we can use git revert. We can also delete commits with
git reset --hard HEAD~x
where 'x' is the number of last commits that you'd like to delete. But revert is always a better option as that way our code always stays in git.thank you very much. and i'm sorry that my english isn't good
glad i could help