Ever felt annoyed when you have to fill up a long form providing all of your information? I don’t know why websites are still following the old thumb rule of using the Registration and Login forms when you can easily login with Facebook PHP API SDK v5. Internet has not only made our lives easier… [Read More]
In my previous article list of some basic git commands, we learned to install and configure git with basic git commands. Today, we will talk about the advanced git commands list. The key points for this article are as follows;
- What is Git Branching and why we use it?
- Create a new branch
- Merge branches
- Resolve conflicts in merging the branches
1. What is Git Branching and why we use it?
Almost, all the VCS has branching support. Branches allow the developers to work without messing the code with the main development line. It often becomes expensive. The reason for expensive is that every single developer want to create a new and separate copy of source code directory. Same is with the git and we create branches there.
In git, branches are often said “killer feature”. But it is still so special that we can’t miss it. Git branching is extremely lightweight and nearly instantaneous. The process of switching back and forth between branches is incredibly fast. Git always encourages merging and branching workflows even many times in a day. Having the command on this feature will give you a unique and powerful tool and will change your development way.
Branches in a Nutshell
To understand the flow of git branches, we have to examine the way of storing data in it.
We know that git stores data in series of snapshots. It doesn’t store data in series of change-sets or differences. The storing process is explained here below in image.
To understand it visual, let’s suppose that we have a directory having four files, and we commit after staging them all. Git will stage these files checksum each and store that copy of the file in the Git repository. Git will add that checksum to the staging area:
$ git add README example.txt LICENSE$ git commit -m 'This is my initial commit of my project'
As you run the “git commit” command, git checks the each subdirectories and stores the tree objects in git repository. After this, git create a commit object. That object has the metadata about the commit and pointer to the tree of root project. This action will re-create that snapshot when it will be needed.
Now, our git repository has five objects
- Blob for the contents of each of your three file
- Tree that lists the contents of the directory and specifies which file names are stored as which blob
- Commit with the pointer to that root tree and all the commit metadata.
If you want to push some more changes, you will commit again. Now, the next commit will store a pointer to the commit that came instantly before it.
Branch in Git:
In Git, branch will be treated as a pointer that will move between these commits. Here in git, ‘master’ is the default branch name. As we start to make commits we’re given a master branch. The pointer in this ‘master’ branch will move to the last commit we made. In our each commit, pointer moves forward automatically.
Master Branch in Detail
It is not a special branch but just like other branches. The reason is that the ‘git init’ command creates it by default and most of users doesn’t change it.
2. Creating a New Branch
For example, you want to create a new branch named ‘testing’. Simply, use this command:
$ git branch testing
This will create a new pointer for you to move around to the same commit you’re currently on.
What branch you’re currently on?
Git has a special pointer called HEAD. This is mostly a different than that of HEAD in other VCSs. In Git, this points to the local branch you’re currently on. So, here, you are still on master. The git branch command only created a new branch – it didn’t switch to that branch.
It can be seen easily by running a command git log. It shows that where the branch pointers are pointing. This option is called –decorate.
$ git log --oneline –decorate
f30ab (HEAD -> master, testing) add feature #32 - ability to add new formats to the central interface
34ac2 Fixed bug #1328 - stack overflow under certain conditions
98ca9 This is my initial commit of my project
Here, the “master” and “testing” branches that are right there next to the f30ab commit.
How to switch between branches?
To switch to your branch, you will use the following command. This will move the HEAD to point to the desired branch.
$ git checkout testing