If you are a part of a professional software development setting, you would know how things work when it comes to writing code. There are usually different teams that write code – the development environment could have a team of developers that are sitting in the same location working closely with a team that is collaborating with them remotely from another part of the globe.
Both these teams write their code and contribute to a central codebase. This is where version control systems or VCSs come into play. They manage codebase that collects code from different sources.
Table of Contents
What is a version control system?
A version control system is a single point of reference for the contribution and management of all the stages and releases of a product, and it does this job without the need for having multiple files or folders. VCSs take out the issues that usually arise during the development phase, and streamline the entire process by not making developers trade files or folders.
Developers instead have a single source that they need to communicate with. It is this source that sees all the changes in the code. All the files are saved in this location. We have two types of VCSs that developers make use of around the world depending on their needs. These VCSs are
Centralized version control systems: These VCSs make use of a centralized repository where the project or codebase exists. This centralized project consists of all the files that are contributed by the developers of a team. If any developer wants to make changes to the project or the files stored in it, they need access to enter this repository.
Distributed version control systems: In these VCSs, things are a lot more flexible than their centralized counterparts. Mercurial and Git are the most common examples of distributed VCSs. In this system, every developer has its own local repository that not only has the code written by them and the changes that they made to it over a period of time but also their releases as well as those of other developers.
What is git?
In simple words, git is a distributed version control system that is available for free. It is an open-source VCS that can handle both small and large projects with greater speed and efficiency than its counterparts. It is the most preferred distributed version control system out of all the available ones.
How does git work?
Git doesn’t work in the same way that most of the other version control systems do. Unlike other VCSs that calculate differences in files and sums these differences up to arrive at the saved version, git uses snapshots of your files system to do the job. Whenever you commit changes to the files or save the changed state of the project, you will have git taking a snapshot of the system and then saving it as a reference.
Files that don’t undergo any change will have their previous snapshot stored as a reference. Git provides developers a string of commands to help them perform different tasks. We will discuss top 30 git commands that are most commonly used by open source developers.
When you are using git, you will have your files in any of these three states – modified, staged, or committed. Let’s discuss what these states actually mean.
Your files are in the modified state when you are making changes to them in your local repository; however, these are still not stored. In the staged state, the changes you made to the files are saved. Finally, in the commit state, the modified and saved files are stored in your project or codebase.
Now, there is something very important about git that you need to thoroughly understand. There are two types of the repository that you can run with git. Your laptop or desktop that you are working on has your local repository, which is also referred to as the working directory. Then a remote repository exists on the cloud.
So the question is how do you use these two repository types with git? The local repository comes with all the necessary features but is only available to be used on your desktop or laptop. What do you need to do when you have to share the files with others in your team? Use Gitlab or GitHub to create your code’s remote repository.
Then you can simply use that repository to upload your files or code. Now everyone that has access to your repository can download the files to their local laptop or desktop and make their contributions.
All these commands that we will be discussing in this section have a big role to play in making the software development process a lot easier for developers. We will mention the most useful of those commands.
1. Configure details: This can be used to configure your profile. Use git config command for this purpose. You can set up your name and email ID using this command. You are allowed to configure these details on two basis – project or global. Use the below command for locl repository.
git config user.name “Bill Tench”
git config user.email email@example.com
Use global config command for global set up
git config — global user.name “Bill Tench”
git config — global user.email “firstname.lastname@example.org
2. Initialize repositories: A repository is a type of directory for your projects. It has a lot of information, including source files, tags, heads, sub-directories, and other things. Use the git init command to initialize a repository. This command is one of the most used commands out of all the others. After the initialization of a repository, you can start adding files to it and start modifying them.
3. Add files: Git makes adding files a very task. Use the git to add a command to add all your modified files or directories to the repository. Let’s see how it works.
git add file
This command will have all the files from the working directory and add it to the repository. You can add specific files using this command. Now if you want to add PHP files, you can use the command mentioned below.
git add *.php
These files will then be marked for staging.
4. Verify files that are added: Files that have been recently added can be verified with the git status command. You can view new files or the ones that have undergone modification using this command. You can run the git status command for viewing the details. Once you use this command, you will see all those files that are set for the staged state in the next commit.
5. Commit repository changes: As discussed earlier, every time you commit a change, s snapshot of the codebase is taken. This is how git tracks changes and delivers version control. The command used for this function is git commit.
As soon as you run this command, you will be asked to provide some information. It could be anything like adding a small detail. The default Linux editor that was set up during installation will be invoked. Use git commit –m “First Commit” command to stop his indiscretion from taking place.
6. Display logs: If you want to see what changes you have made to the repository, you need to check the logs. Make use of the git log command to get this done. This will display generalized information on the commits you have made. Use git log –file command sees what changes you have made to a particular file. You can use other options as well to check the logs for other details.
7. Verify git branches: A branch shows an independent phase of development in a project. To view the current branch, you can use the git branch command. With this command, you will be able to see the branch you are currently working on. The active branch will have an asterisk sign against it in the output to differentiate it from other branches.
8. Reset branches: It is very to reset the working directory or repository you are currently on to a different state. You can use the git reset command to perform this function. You can perform a soft or hard reset of the current branch using the below-mentioned commands:
git reset –soft
git reset –hard
9. Add new branches: If you want independently work on new features, you can add new branches to make the job easy. Use the git branch command for this. Add the branch name for identification.
git branch feature-work
To make sure that the branch was added successfully, use the git branch command again. If the addition was successful, it will display the new branch with the name of feature-work. You need to remember that you can add only one new branch with the same name. If you do so, it will result in a fatal error. So try using different names for adding new branches.
10. Switch between branches: You can use the git checkout command to switch between different branches with your project. This command is also used by developers quite commonly during software development. Here is an example
git checkout feature-work
With this command, you will be notified of a successful switch. To make sure that the switch really happened, use the git branch command that we discussed earlier.
11. Display version: Use the git –version command to view the git version installed on your machine.
12. Consult manual page: Use the man git and man git commit commands to see manuals for sub-commands.
13. Manage git configuration: You can use the git config command to set, replace, or query different configuration options.
14. Summarize log info: Use the git shortlog command for this purpose.
15. View modifications for each commit: You can use git what changed the command to see the changes that each commit brought to your project.
16. Validate object database: Use git fsck command to find bad or missing objects in the object database.
17. List unpacked objects: Use the count-objects command to display the total number of unpacked objects as well as the disk space they are taking.
18. Pack objects that aren’t already packed: You can use the git repack command for this purpose.
19. Manage working trees: Use the git worktree command to display a working tree, remove a working tree, add a working tree, and prune a working tree respectively.
20. Look for patterns: Use the git grep command to make development easy by looking out for specific pattern in your repositories.
21. Display remote repositories: Use the git remote command to perform this function
22. Push updates: You can use the git push command to add your updates to the remote repository.
23. Pull new updates: You can use the git pull command to update the local version of your project with the modifications made by other developers.
24. Clone repositories: Use the git clone <Git : URL> command to perform this function.
25. Stash working directory: You can use git stash command for this purpose.
26. Revert existing commits: Use the git revert command to revert commands that are no longer useful.
27. Merge two branches: Merge two development branches into a single unified branch with the git merge command.
28. Check differences between files, commits, and tress: You can use the git diff command to perform this function.
29. Add tags to projects: Mark important events in your project with the git tag command.
30. Fetch remote data: Use the git fetch origin command to perform this function.
Also Read: Top Full Stack Developer Tools
If you’re interested to learn more about Github commands, full stack software, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.