Basic Git commands every developer should know
Surender Vikram Singh
Founder at VT Netzwelt Pvt. Ltd. | Entrepreneur | Powering Next Generation Products With Innovation
If you are a developer working in a team, then you must realize how difficult and frustrating it becomes when it comes to file sharing.
No matter how hard you try for effective collaboration within the team but the fact is that the things become chaotic when there is no version control system.
Common issues faced by developers include overwriting files, lost files, etc. Even though if you are having a common repo, if one team member forgets to get the latest files, he can destroy things with his/her latest additions.
To avoid all these scenarios, we need an excellent version control system setup.
A Quick Intro To Version Control
Version Control, also known as Source Control Management is an extraordinary method to take care of the file-sharing issue.
The main aim of the version control system is to keep a primary repo for all the files. Any member of the team can check out files, roll out improvements and afterward commit them back. The Version Control System keeps a record of as who has changed the files, when they were changed, and what is different about them from the previous state of the repo.
It additionally requests that you compose a little note about the change you have made in the file so that everybody knows what you did and why. You can also rollback to any previous version of the file.
On the off chance, if you and someone else is working locally on the same file at the same time, the main repository will create a new up-to-date file by merging both sets of changes. The Version Control System also highlights any conflicts that may arise during the merge process.
Benefits of Using a Version Control System
- No chance of files being overwritten
- A common repository to hold all the latest files
- Rollback to a previous version of the file if required
- Different people can work on the same file simultaneously
Git – Open-Source Distributed Version Control System
As now you have an idea about Version Control, the next step is to choose a version control system. There are plenty of options available in the market like SVN, Mercurial, Git, Bazaar.
Any of these systems can be a good solution for you depending on your needs but here we will be discussing Git. Git has gained a lot of popularity thanks to GitHub and a strong Linux community.
We are ourselves using Git in our organization for quite a long time now and have really benefitted from it when it comes to the time taken for the delivery and the developer’s satisfaction.
Git is an open-source distributed Version Control System. Distributed means that every user have a complete copy of the main repository in his local machine.
Git was created by Linus Torvalds for Linux Kernel Development.
With Git –
- You can work offline
- Faster Processes
- No single point of failure
If you are looking to start with Git in your projects, then you need to know a handful of Git commands.
These Git commands are most frequently used by developers. These commands need to be run on a command-line interface (CLI) tool. However, Git also has several GUI clients that you can use.
Most Commonly Used Git Commands
1. Configuring Git
The first step after installing Git is to configure it. For the basic configuration, there are only 2 commands used. One command is used to register the user’s name and the second command for his email.
git config --global user.name "Peter Galle" git config --global user.email "[email protected]"
2. Initializing the Main Git Repository
After Git configuration, the next step is to initialize a Git Repository or repo. To initialize a git repository for new or an existing project, navigate to the folder which you want to use as Git project folder and run the following command –
git init
After running this command, Git will start tracking all the changes you make in that particular folder. The command has actually created “.git” hidden folder in our directory to maintain and handle any type of changes.
3. Cloning a Remote Repo
You can create a copy of a remote repo on your local machine by cloning it. First, navigate to the folder where you want to clone the repo and run the following command –
git clone https://www.example.com/your-online-repo
It will download the repo with the folder named “your-online-repo”.
4. Checking the Status of the Repo
There are three states in which a file can reside in Git –
- untracked – It is the state where you have added files which are new in your working directory and were not present in your last snapshot, in short, untracked files are the ones which git don’t know about.
- modified – It is the state where you have made some changes but not yet committed.
- staged – It is the state where files are stored which you want to commit.
- committed – It is the state where a current snapshot of the file is stored in the local database in .git folder.
Check the status of the current repository with the following command –
git status
5. Staging Files
Type the following command to push your files to the staging area –
git add index.html
The command will add index.html file to the staging environment.
You can also use –
git add .
It will move the whole working directory to the staging environment.
6. Committing Stages Files
You can save the current snapshot of the staging area by committing it using the following command –
Complete article available here -