Skip to main content

Command Palette

Search for a command to run...

Basic Git Commands: Part 1

Updated
6 min read
Basic Git Commands: Part 1
B

I am Barun Tiwary, learning to scale software to millions of users.

Today, we will discuss the 6 most important Git commands you should know, whether you are a software developer, frontend engineer, backend developer, or even if you work in DevOps and cloud.

If you are new to Git then first read this internal of git article to have a basic understanding of Git. This article will be divided into three parts. In each part, we will discuss two categories of commands.

6 Categories of basic Git commands.

  1. Project Setup

    • git clone

    • git init

  2. Daily Workflow

    • git status

    • git add

    • git commit

  3. Branching & Navigating

    • git branch

    • git checkout

    • git merge

  4. Sharing & Updating

    • git pull

    • git push

    • git fetch

  5. Inspecting & Comparing

    • git log

    • git diff

  6. Handling Interruptions & Mistakes

    • git stash

    • git reset

    • git revert

Let’s dive deep into each of these commands.

Project Setup.

git init

If you do not have git installed in your computer, do it from this article.

This category provides help to set up your new Git project locally or clone any other project from GitHub.

First, learn about how to create a local git project.

To do so, we will use git init a command, but before that, first set up the project with a name git-commands

mkdir git-commands
cd git-commands

# Once folder is created and we are inside this folder we can initialize a git repository here.
git init

# Git is the command line tool and init is the flag which tells the git to initialize
# the new git project in the curent folder. This command is responsible for creating
# .git folder inside the current folder.

Once we are done with creating a new git project locally, we can run git status the command to see if it is actually done or not.

git status helps to check the current project status. We will look into this in this article.

git clone

If you want to clone another GitHub repository, there are a few more commands and steps to follow. However, that is not covered in this article. We might discuss it later, so feel free to continue. If you run into any issues, please let me know in the comments or reach out to me on Twitter. I'll be happy to help you.

So, how to clone a project?

First, grab the URL of the HTTPS URL of the GitHub project, and then we just need to use a simple git command as follows.

# git clone url_of_the_git_project_to_clone
git clone https://github.com/Barun355/chess-frontend

Once done, you will see the chess-frontend codebase is cloned into your current folder.

After cloning the project, you can go into the project, contribute to it, and then push the changes by raising PRs and all.

Daily Workflow

We explored the essential commands you should know before moving forward. Now, let's look at another simple example to understand the basic commands we will use about 60-70% of the time.

So, let's take an example from the last article.

Here we already have a project set up named git-internal where we have done some of the work.

git status

We have already seen this command. It helps us display the current state of our working directory and staging area. It also shows us the current tree inside the working directory.

It is primarily used for:

  • Identify changes: Show modified, new, or deleted files.

  • Check staging area: Determine which changesareready to be committed

  • Verify working tree: Confirm if your project is in a clean state (i.e., nothing to commit).

Common options

  • git status -s (Short format) : Provides a more compact, summarized output.

  • git status -b : Displays branch tracking information

  • git status -v : Shows verbose details, including the textual changes of uncommitted files

Technical Explanation

The status options show 3 different things in the output for a simple project:

  1. Difference between the index file (staging area) and the current HEAD Commit (Latest Commit).

  2. Difference between the working tree and the index file (staging area)

  3. And finally, files and folders that are not being tracked by git yet. This excludes all the files and folders that are listed in .gitignore file.

git add

This command allows you to move changes in files or newly created files that are not yet tracked by Git to the index (staging area). Here, all updates are stored before being included in the next commit.

To showcase the add option, we will first create a greet.js and loop.js file, and we will see the most basic way of using this option.

# create greet.js file
touch greet.js loop.js
# greet.js
const greet = `Hello Barun Tiwary`;
console.log(`${greet}`);
// loop.js
for (let i = 1; i <= 10; i++){
    console.log(`Current value of i is ${i}`);
}

Now, as we have created all the required files, I will demonstrate the most basic way of using add option.

# git add filename
git add greet.js
git add loop.js

After running the code above, we have moved our newly created untracked files into the index (staging area). Although we did this one by one, it's not practical if we have hundreds or thousands of files to track. In that case, we can use the dot (.) option.

# git add .
# (.) represents all the files exluding files matching regex in the .gitignore file
git add .

Note: Whenever we make changes to a file, create a new file, or delete an existing file in a Git-initialized project, we need to run the add command for all those changes to track them.

git commit

Now, let's explore the most important Git options that actually store the code's history. First, we will look at the basic commands, and then we will explore the optional flags.

# git commt -m "write a meaningful message to store with the commit history."
git commit -m "Added greet and loop js files"

  • commit : The options to commit the changes from the index (staging) area.

  • -m “…” : This allows us to write the commit message directly while executing the commit command.

If you want, you can skip the -m flag. Doing so will open the Vim editor by default, where you can add the commit message and then save the changes. We are using the -m option because it is convenient.

Okay, so far we have covered two categories of basic Git commands, and we will cover the rest in upcoming articles.

More from this blog