Top 20 Git Commands With Examples
What is GIT?
We all are aware that “Necessity is the mother of all inventions”, similarly Git was invented to overcome the problems that the developers faced before Git.
Git was created by Linus Torvalds in 2005 and is specially designed to handle small as well as large projects with much efficiency and speed. It is a free and open-source distributed version control system that is very easy to learn. Git tracks all the changes that are made by the user in the source code so that the user has a record of what has been done and can revert to specific versions if ever needed. With the help of Git, multiple people are allowed to make changes and can be merged into one source, so it also makes collaboration easier.
Let’s understand a scenario which is before and after Git invention-
Before Git was invented-
- Before the invention of this tool developers used to submit their codes to a central server and there is no copy of their own.
- Any changes which are made to the source code or file were unknown to the other developers and there is always a lack of communication between developers.
After the invention of Git-
- Invention of Git allows every developer to have a copy of the code on their local systems.
- Any changes which are made to the source code can be tracked by other developers which allows regular communication between multiple developers.
Why are we using Git?
Today it is very important to switch from a centralized version control system to Git as it changes the way your development team creates software. Users preferred using Git because of the following features-
- Free and open source- Git is released under General Public License which means it is open source and you don’t need to buy it. It is totally free and since it is open-source, users can modify the source code as per their requirements.
- Speed- Since there is no network dependency for performing operations, it completes all the tasks very fast. Fetching version history from a local repository can be a hundred times faster than fetching it from a remote or central server.
- Scalable- In the future if the number of collaborators increases Git can easily handle it due to its scalable feature.
- Reliable- Since every developer has its own local repository, in case the system crashes the lost data can be easily recovered.
- Secure- Git uses SHA1 i.e., Secure Hash Function to name and identify objects. Every file and commit are check-summed and recovered only by its checksum.
- Easy Branching- Branch management with Git is quite simple as it takes only a few seconds to delete, create and merge branches.
Key Benefits of using Git-
- Historical Change Tracking- User can easily review a graph of all their changes, how they have changed, who changed them, and when they are changed. Users can also revert to a previous commit if in case needed. This history helps developers to identify and fix bugs.
- Work as a Team- User can easily share their code with other developers for review. Multiple users can work on the same file and resolve issues later.
- Availability & Redundancy- Git is a distributed VCS means there is no central place where everything is stored. There are multiple backups in case you need one. Users can also work offline and commit their changes once they are ready.
- Industry Standard- Due to its high demand, it is supported by many integrated development environments (IDEs) and by many popular developer tools.
Important Git Commands-
Let’s go through some important Git Commands list commonly used by the developers-
- git add- It is used to move all the changes from the working directory to the staging area. Before running the commit command, it is used to add any new or modified files.
- git branch- It is used to create isolated development environments i.e. branches within a single repository.
- git clean- As the name suggests, it is used to remove untracked files from the working directory.
- git clone- This command is used to create a copy of an existing Git repository. git init- This command initializes a new Git Repository.
- git merge- It is used to integrate the branches together.
- git checkout- This command is used to switch branches whenever the work is to be started on any different branch.
- git config- This is used to set the configuration options for your Git installation.
- git remote- is used to administer the emote connections.
- git reset- This Git command is used to remove changes that are not pushed to a public repository.
- git status- Using this command user can see the state of the working directory.
- git push- It is used to transfer or push the content from the local repository to the remote repository.