In this article, We are going to learn the basics of Git and Git Branching and Merging with Master with an example.
What is Git?
Git is a fast, scalable, distributed revision control system with a rich command set that provides both high-level operations and full access to internals.
Below are the some of the important terminology of Git.
- Pull/Pull Request
In this topic mainly we are concentrating on the Git Branching and Merging with Master.
What is git branching?
Branches can be a new version of a repository, experimental changes, or personal forks of a repository for users to alter and test changes. By default, Git has a master branch, which is same as the trunk in Subversion.
Most of the time branch is created for developing new features. Once the feature is completed, it is merged back with the master branch and we delete the branch.
Every branch is referenced by HEAD, which points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit.
Git Branching and Merging with Master
Create a new directory and initialize a Git repository. We are going to create a directory named “learning”. below is a command for initializing a Git repository and adding a file into the repository.
---> Initialized empty Git repository in D:/learning/.git/
Create a new file named “hello.txt” in the “learning” directory and commit.
git add .
git commit -m "First Commit"
---> [master (root-commit) 2344816] First Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
Right now we are in the master branch. If we push anything in here it always stored into the master repository.
Create a new Branch:
Create a new branch as “Test” by executing below command.
git branch test
to executing below command we can see a list of branch created and the active branch.
git branch --list
Executing the following will point HEAD to the tip of <branchname>. In Git terms, a “checkout” is the act of switching between different versions of a target entity. The git checkout command lets you navigate between the branches created by git branch.
git checkout <branchname>
git checkout test
Now you are on the “Test” branch, you can start adding commits to it.
At this point, We have two branches such as master and test branch and each branch contain different files. Below image shows the different types of branch and development of it.
Now the code is there in two different line of development. We have to integrate with a single branch using git merge.
As discussed above git merge command used to merge the two different branches. Git merge will combine multiple sequences of commits into one unified history.
How does git Merge Works?
In the above scenarios, git merge takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new “merge commit” that combines the changes of each queued merge commit sequence.
As we can see in the below image, the test branch is merged with the master branch and created a common base branch of it.
By executing below command we can verify only one file exists in the master branch.
To merge commits into the master branch, let’s now switch over to the master branch.
Use the merge command to merge branches. By running the command below, the specified commit will be merged to the currently active branch.
git merge <branch>
git merge test
Now run below command to verify the specified commit will be merged to the currently active branch.
In this article, we have discussed basic of git and important terminology of Git. In-depth we have explained about git branching and merging along with a simple example.
This document integrated and referenced other Git commands like: git branch, git add, git list, git checkout and git merge.
In the next article, We are going to discuss how to resolve git conflict and more about git merging.