5 Basic Git Commands Every Developer Must Know

πŸ“† Β· ⏳ 5 min read Β· Β· πŸ‘€

Introduction

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.

πŸ’‘

Spoiler Alert!

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.

1. git init

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.

Terminal window
git init

You would see output something like this

Terminal window
Initialized empty Git repository in /path/to/your/folder/.git/

2. git clone

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.

Terminal window
git clone <:remote-url:>

Example

Terminal window
git clone https://github.com/nodejs/node.git

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

Terminal window
git clone https://github.com/nodejs/node.git myfoldername

3. git add

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

β”œβ”€β”€ main.js
β”œβ”€β”€ styles.css
β”œβ”€β”€ index.html
β”œβ”€β”€ about.html
└── contact.html

You have made changes in index.html & about.html and now you want to add these changes to git.

You can do something like this to add these files.

Terminal window
git add index.html about.html

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

Terminal window
git add .

The . basically means add all the files in which I have made changes.

4. git commit

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

Terminal window
git commit -m "index.html updated and about.html added"

Now we have successfully committed our changes to the git system.

5. git push/pull

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

Terminal window
git push <:remote:><:branch:>

Example

Terminal window
git push origin master

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.

Terminal window
git pull origin master

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

Terminal window
git remote -v

It will display the output of all the aliases you have and where do those aliases point to. (Yes, you can have multiple aliases)

Terminal window
origin [email protected]:AkashRajpurohit/Blogging-Website.git (fetch)
origin [email protected]:AkashRajpurohit/Blogging-Website.git (push)

πŸ”₯Bonus

These are a few other basic commands that are can be helpful.

6. git config

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.

Terminal window
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

See the changes by typing,

Terminal window
git config --global --list

7. git status

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.

Terminal window
git status
Terminal window
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: node.txt
no changes added to commit (use "git add" and/or "git commit -a")

8. git log

git log is used to check the logs/history of all the commits you have made

Terminal window
git log

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

Terminal window
git log --oneline --all --decorate --graph
Output of the above command
Output of the above command

TL;DR

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.

You may also like

  • # git# engineering

    Selecting the Right Git Merging Strategy: Merge Commit, Squash and Merge, or Rebase and Merge

    Uncover the intricacies of Git merging strategies – merge commit, squash and merge, and rebase and merge. Discover the pros and cons of each approach and learn how to navigate the decision-making process based on your project's dynamics and team preferences.

  • # git# engineering

    Git Beyond Basics: 7 Uncommon Git Commands Every Developer Should Know

    Git is one of the most popular version control systems used by developers worldwide. However, most developers only use a handful of Git commands in their daily work. In this article, we will explore some uncommon Git commands that can help improve your workflow and productivity. These commands will help you to work more efficiently, troubleshoot issues, and collaborate more effectively with your team.

  • # projects# engineering

    I built my own in-house Newsletter system

    Discover how I transformed the need for a newsletter system, sparked by Revue's shutdown, into a fulfilling side project. Dive into the my journey of conceptualizing, breaking down, and building a custom newsletter system that seamlessly integrates with my website's content workflows.