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 have 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 wants to create a new and separate copy of the source code directory. The 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 a series of snapshots. It doesn’t store data in a series of change-sets or differences. The storing process is explained here below in the 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 each subdirectory and stores the tree objects in a git repository. After this, git create a commit object. That object has the metadata about the commit and pointer to the tree of the 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
- A 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, a 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 every commit, the 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 the users don’t change it.
2. Creating a New Branch
For example, you want to create a new branch named ‘testing’. Simply, use this command:
1 $ 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 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 the 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