Every version control system has a branching system. Branching is a powerful feature in which it does not disturb the main or actual pipeline. For instance, we can branch the development into main and test, in the main branch actual working content is developed, whereas in the test branch the features to be added can be developed and can be merged with the main branch. Git also supports this branching and merging feature.
Many version control systems make this feature in a heavy way by copying all content from one branch to another. This leads to a high load. But, git makes it in a lightweight process by snapshots(Already mentioned in the post – Git introduction please refer it). It snapshots the content using checksums. This makes git a lightweight and powerful version control system.
In git, a new branch is created using branch command. The default branch in all git repositories is main branch. The branch command simply creates a new branch only, do not navigate to the branch.
Here, we create a new branch called the ‘test’ branch by the following command.
$ git branch test
To view, the branches execute the log command that shows an arrow specified with the branch name. The log shows the name of the branch currently the pointer present and created.
$ git log
To navigate to the branch, use the checkout command. This command takes control of another branch by taking a snapshot of the’master’ branch.
$ git checkout test
Now check out the log, the arrow changes to the test branch.
Now, you can able to work on the test branch freely, without disturbing the main content altering. If checkout is an extra process, use -b option with branch command. It will automatically navigate to target when a new branch is created.
The above picture illustrates how branching is done. Here, the ‘s’ stands for a snapshot. When the branch command is given with the name ‘test’, it creates a branch called ‘test’. Here s3 is the snapshot of the master and pointer points to master snapshot. When checkout command with ‘test’ given, it navigate to test branch. When some alteration made in the ‘test’ branch, commit it. This creates a new snapshot called s4 in the ‘test’ branch.
Edit and commit until the feature is ready. If your features are ready, to make the ‘test’ developments to the ‘master’ branch we must do merging of the branches, this will be available in the next tutorial.
Click here for Git archives.
To view official git documentation please visit git docs.
639 total views, 1 views today