Using a version control system ↗️ is no more just a fancy skill that can be added on your resume but a great tool to help you manage your codebase for any project.
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Today we will learn 5 basic commands of Git that you must know as a developer.
I’ll be also adding bonus commands at the end of this blog so keep an eye for that.
Download & install git at https://git-scm.com/ ↗️ if you haven’t yet.
The very first thing you need to do is to initialize your project folder to use git.
We do this by using the
git init command.
You would see output something like this
While the previous command helps you to initialize a git repo,
git clone helps you to clone a repository from a remote location like Github, Gitlab, Bitbucket, etc.
This will clone the nodejs repository to your local machine.
Tip: By default, it will clone into the folder with the name of the repository i.e node in the above example. If you want to change the folder name then just add another argument of the folder name at the end of the URL
So now you either have initialized your repo or cloned from a remote repo. Now we will see how to add the changes you have made to the git system
We will see the use of
git add command.
Suppose you have 5 files in your project
You have made changes in
about.html and now you want to add these changes to git.
You can do something like this to add these files.
Now, this is perfectly fine, but often you will have lots of files to be added and you definitely don’t want to write the file names all again. To overcome this we will use the following way
. basically means add all the files in which I have made changes.
Up until now, we have just added the files, but in git, we have to also commit those files to mark them as a checkpoint in your project codebase.
To do this, we will use
git commit command.
This command takes a mandatory flag
-m which is for the message you want to give to this commit.
Why is it mandatory?
Git uses a hash value to keep track of the checkpoints you have made using the commit command. However, I’m pretty sure you don’t want to remember that hash
b39203bc42f means that you have changed index.html and about.html right?
So for simplicity, we add a nice message to our commit so that we as a human can understand what all things have been changed up until this point.
Check out the example
Now we have successfully committed our changes to the git system.
So now you are working on a project and making lots of commits to properly manage the history of your code.
But now you want to share this with other people around and want others to collaborate on your project?
For this we will see how to use
git push and
git pull command to host your codebase on various platforms like Github, Gitlab and Bitbucket.
To push your code from local machine to a remote repository, you have to use
will push to your origin on the master branch.
Similarly, now if you want to bring changes from your remote repository to your local machine, we will use the
git pull command.
Confused about what is
origin? Then read the following paragraph or you can simply skip to the Bonus section.
origin is an
alias on your system which defines where is your remote repository at.
Use the following command to check what the origin is aliased to
It will display the output of all the aliases you have and where do those aliases point to. (Yes, you can have multiple aliases)
These are a few other basic commands that are can be helpful.
This command helps you to make changes in the configuration file of git.
You can use
--global flag to list/update your global git config file.
For example, you can set your name and email to the global git config and this would add your name and email to all the actions you perform in any of your projects which is using git.
See the changes by typing,
Use this command to check the status of the current working tree.
Simply said what all files have been changed, are they added, are they committed, are they push to remote, etc all this info can be viewed using
git status command.
git log is used to check the logs/history of all the commits you have made
It has many useful flags you can use to view the logs in a better format. The set of flags I use daily with this command is
We discuss very basic and important git commands that you must know as a developer in 2020. We also see a few bonus commands which aren’t that much necessary to look into but always good to know.