What is Git ??
Git is the most commonly used version control system. Git helps to track changes you make to the files. Therefore it will provide us a record of what we’re doing, and it helps us to revert to specific versions if needed to do so. Many organizations use Git because it makes collaboration easy by allowing changes of multiple people to be merged into one source. Therefore, Git is useful for track and version your code either you work only by yourself or work as a part of a team.
Git is software that runs locally. Your files and their history are stored on your computer. We can use remote online hosts like GitHub, GitLab, or Bitbucket which, are wrappers on top of Git to store our files and their revisions. Using a remote repository as mention above will give you a centrally located place where you can upload your changes and download changes from others, letting you collaborate easily with other teams of developers. After we set up a remote repository, we can upload (push) your files and revision history to it. Also, we can download (pull) their changes into your local repository; once someone makes changes to the remote repository. This is why everyone calls Git a Distributed Version Control System because it has a remote repository that is stored in a server and a local repository that is stored in the computer of each developer.
1. How to install Git
You can download Git using the following link, and it has details on how to install Git in multiple operating systems. https://git-scm.com/book/en/v2/Getting-Started-Installing-Git. After that, you can verify the git version using the following command.
2. How to Create a Local Repository
You can create a folder name called git-sample, then go inside the folder and create a local repository using the following command. After that, you can create a file called .gitignore and add the file names that you don’t need to be version controlled by git.
3. Staging, and Commit Code
The process of adding the code to the local repository is known as Committing. Before committing the code, it has to be in the Staging Area. The staging area is there to keep track of all the files which are to be committed. If any files are not added to the staging area, they will not be committed. So that developer can decide which files are needed to be committed.
4. Git Status and Log
Git status command helps to find out the files in the staging area, the files that have been modified, and the files we are going to ignoring. Git log shows all the commits that have been done until now. It displays the date of the commit and the commit message.
5. Point to the Remote Repository
Until now, we have been working only in the local repository. Each developer will work in their local repository, but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code. GitHub is a famous remote repository that most developers use. First, create an account on GitHub, then create a repository by clicking “Create Repository”. Give it any name you desire, then copy the remote repository URL as shown in Figure 2. In order to point your local repository to the remote repository, use the following command.
6. Git Pull
Git pull is used to pull the latest changes from the remote repository into the local repository. The remote repository code is updating by the various developers if you work in a team. Therefore it is recommended that you perform a pull every time before you make a push.
7. Git Push
To push our code from the local repository into the remote repository, can use the following command. It will push our code from the local main(master) branch to the remote main(master) branch.
8. Git Clone
As an alternative to git init, you can first create the remote repository and then clone it to your local machine(local repository) using the below command and follow the other procedure mentioned above.
9. Git Branches
Git branches are used to track the feature development by the developer on a new feature branch without interfering with the stable main(master) branch. Changes made on the created feature branches do not affect the local main branch or remote main branch. You can create a local branch using the following command.
Although we created a new branch, we are still in the main branch context. Therefore to switch to the ranmal-feature-1 branch, use the following command.
At the moment, the created branch details are in the local repository only. To push our branch details to the remote repository, use the following command. After that, you can perform staging, commit, status checking, pushing, and pulling operations between your local and remote branches to track your feature developments.
10. Merging Branches
Let’s say our ranmal-feature-1 branch is 2 or 3 commits ahead of the main branch. Now we want all of the code in the feature branch to be merged with our main branch. For this, we can use the Git merge. First, perform a pull from the remote branch as below. Then switch from feature branch context to main branch context. After that, perform a pull request from the main branch (it is always recommended to do a pull request before modifying any branches in the remote repository). After that use, the git merge command as shown below to merge the local feature and main branches.
After running the above commands, the merging of the two branches will be successful. Therefore to push the updated main branch again to the remote repository use the below command.
Important Point
🔸 Although we consider the merging is successful here, in real projects, there will be conflicts when merging two branches. A merge conflict arises when two separate branches have edited the same line in a file, or when a file has been deleted in one branch but edited in the other.
There are more topics we can cover under Git like Stashing, Rebase, and Tags we will cover those topics in a separate blog article. Now you know the basics of how to use Git, so go ahead and explore more.
References
For further more clarification check these resources;