Git Branching
Git Branches​
Having branches makes it easy to work on different things at the same time; say you are working two days on a new feature and in the middle of it you find a bug that needs fixing, you can do that easily by using two branches.
This is where git branches shines. They are cheap and virtually free to create. With our of understanding git internals this will become much more clear throughout this section
Create a new git repo as local-git, and add our first commit in README.md with one line as A and commit message as A
Creating Branches​
Creating Branches is easy
git branch foo
this will create a new branch named foo.
Create a new branch named foo.
Well after running this command we can see, nothing happens visually. but we can use
git branch
to show all the branches present in our repo. * points to the repo which we are currently checked out.
Git branch will diverge from the branch from which you have ran git branch [BRANCH_NAME] command. This also they will have all commit history off the branch from it is diverged.
What Actually is Branch to Git​
Branches are just pointers to commits in tree.

thats why they are virtually free.
HEAD is a special pointer which always points to state of repo. Basically HEAD shows us where we are in our repo.
Switching Branches​
You can switch branches easily in two ways
git switch <branch name>
git checkout <branch name>
checkout is a more versatile operation and i am personally just in the habit of using it and it is slightly more useful than switch.
switch is a specialized case of checkout which only helps in creating and switching branches.
So to remember this command people generally say checkout X branch instead to switch to X branch.
all the commands which accepts <branch_name> as a parameter, is actually accepts an commit SHA. (because branches are just pointers to commits)
To make a new branch and checkout to it, you can use
git checkout -b <branch_name>
Checkout to new branch named foo.
Now we are on foo lets create another commit with message B and a new line added to second.md of B. Then do it again, except replace B with C at the end we want something like this
2 things to notice:
- Notice our
HEADpoints tofoonow, current location of git points tofoo. masterstill points toA
You can also delete branch from
git branch -d <branch_name>
It will delete commits recursively until no commit or a commit which is present in another branch is encountered.
Ok now we have commits locked in, what to do now ?