Basic Git Commands Every Developer Should Know

Basic Git Commands Every Developer Should Know

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 repo.

Basic Git Commands Every Developer Should Know

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
  • Roll back 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 "peter@example.com"

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 –

git commit -m "First Commit"

Note – It is required to add a commit message that describes the changes you have made.

7. Connecting with a Remote Repo

To push all your local changes to a remote repo, first, you need to create a connection with that repo. For example – if you are creating a connection with an online server such as GitHub, you can use the following command –

git remote add origin https://www.github.com/your-repo

8. Pushing Local Changes to Remote Repo

After creating the connection, use the following command to push local files to the remote server –

git push origin master

The word origin stands for remote repo and master is your local repo.

push – push your changes to the remote server
origin – remote server origin
master – master branch

9. Pulling Changes From Remote Repo to Local Repo

Suppose you want to see the code of other developers in your local repo. To do so, we use the following command –

git pull

The command will pull all the changes from the remote repo to your local repo. Running this command only is enough for updating the local repo.

10. Fetching Changes from Remote Repo

If someone else is also working on the same project and you want to bring the changes others have made in the project, then you don’t need to clone the repo again, just type the following command –

git fetch origin

The command gathers any commits from the target branch to your local repo. However, the command does not merge them with your local repo.

11. Merging The Fetched Changes

To merge the changes from the remote repo to your local repo, type the following command –

git merge origin/master

12. Checking Current Branch

Use the following command to list down all the branches

git branch

Conclusion

Working with Git definitely requires a learning curve but it has become mandatory as modern development is impossible without a version control system.

There are more commands which can be used so if you feel we have missed some commands do let us know by commenting below.

Sharing is Caring

Have a Project in mind

Request a Call Back from our Industry Experts
Schedule a Call
Author:
VT Netzwelt is an internationally-recognized brand for the development of sophisticated web and mobile applications who is working for premium segment clients worldwide. Our team possesses valuable technical certifications in Project Management, Magento, and Quality Assurance.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please fill in the details and our representative will be in touch with you shortly.