2) Standard Git Workflow Lesson

Git Status

14 min to complete · By CodingNomads Team

So far, you've heard a lot about how Git tracks changes to your files and folders. At this point, you might be wondering what that means and why you haven't seen any of that going on so far. In this lesson, you'll learn how to use one of the most useful Git commands: the git status command.

You'll practice running git status when working with your local and remote repositories.

What is Git Status?

The git status command prints the status of a repository, enabling you to check the state of your files and changes as they travel from your working directory to your local or remote Git repository. It provides you with a lot of useful information when working with Git-tracked projects, including:

  • Branches
    Which branch you are currently on
  • Commits
    How many commits you are ahead of or behind your remote repositories
  • Staged Files
    Whether your files are staged or not
  • Modified Files
    Exactly which files have been modified in the working directory but have not been added to the staging area
  • Guidance
    Hints and Tips about what you might like to do next. These are very valuable and usually right on point.

When interacting with a Git repository, most developers frequently use the git status command, making it an integral part of any Git workflow. But what does git status look like in action? First up, you'll see how to use it to track changes in your local Git repository.

Local Repositories

If you were to run git status in any non-versioned directory, which just means that you haven't initialized the directory with Git, you'll see the output below:

$ git status
fatal: not a git repository (or any of the parent directories): .git

Git will show you an error message reading that it's not a Git repository. This is expected. As you may remember, when you initialize a Git repo in a given directory, it creates a .git directory, which you can think of as the Git database. In a non-versioned directory, Git won't find a .git directory, which makes it vocally complain with a fatalistic tone. But don't worry; most other error messages that Git might throw at you will be much friendlier :)

To please your friend, Git, you simply need to initialize a Git repo in your directory and check back in. Do you remember your (empty) amazing project directory from the previous lesson, My_Amazing_Project? Navigate to it using the bash command, cd, like so:

$ cd ~/Documents/My_Amazing_Project
Colorful illustration of a light bulb

Note: If you're just tuning in and would like to follow along with this exercise, check out the previous lesson, Git Init, to learn how to initialize a Git repository.

What's the status of your repository? Go ahead and run your trusty new Git command, git status:

$ git status

You will see the following output:

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

This is great feedback. It means that you have successfully initialized an empty Git repository within the My_Amazing_Project directory. It's telling you that you are currently on a default "branch" that Git calls main and that you haven't made any "commits" yet.

Don't worry about what branches or commits are for now, as you'll explore them soon. Just know that at this point, you only have a main branch, which is GitHub's default branch, and that since you haven't changed any existing files or added any new files, you have nothing to "commit." Time to change that!

Start by creating a simple README.md file by running the following command in your terminal. For now, you can just write "# git practice" as the content of your new file.

echo "# git practice" >> README.md

What you see here are bash CLI commands that are not related to Git. This command will put the string # git practice into a new file called README.md. Now, let's run git status again:

$ git status

The output has changed. You will now see the following response from Git:

Git Status Readme

As you can see, the output from the git status command has changed. It now indicates that you have one untracked file, README.md, which is displayed in red. The output also gives you a good hint as to what you might want to do next.

When working with Git, always keep in mind that Git is a great communicator and that you will receive helpful tips at every step along the way if you keep reading the output that Git presents you with after running a command.

But what about that untracked file? In the next exercise, you'll explore this "untracked file" business further while working with a remote repository on GitHub.

Remote Repositories

Start by creating a new repository on GitHub titled temp_testing_repo and check the checkbox to add a README.md file. Next, you'll clone this repo to your local machine. But first, you need to make sure that you're not already in a Git repository. A simple way to do this is to cd to your Documents directory by running:

$ cd ~/Documents
Colorful illustration of a light bulb

Note: Never clone one Git repository into another one. You never want to have repositories within repositories since this messes up Git's way of keeping track of changes. If you're new to cloning or would like some review, check out the exercise on how to clone a GitHub repository.

Now you're ready to clone temp_testing_repo to your local machine. This will create a new folder with the name of the repository for you.

After cloning the new repo, cd into the project you just downloaded:

$ cd temp_testing_repo

Then run the git status command again:

$ git status

on branch main
your branch is up to date with 'origin/main'.

nothing to commit, working tree is clean

Git will display that "your branch is up to date". This means the branch you are working on locally is in sync with the remote branch on GitHub. You can change that by adding a new file to our local working directory. Do this by creating a new file called NewFile1.txt and adding some text to it:

echo "# practice makes perfect" >> NewFile1.txt

If you run the git status command after doing this, you'll see a different output:

Git Status output before adding a new file Git Status Untracked File Remote

There's that untracked files display again. While you have created a new file in your folder structure, you have not yet told Git to keep track of its version history.

As you can see, Git is already aware that the file exists. But if you want it to do its job, you first need to tell Git to consider this file by using git add. You'll learn more about git add soon. In the meantime, you can follow Git's gentle suggestion and "add" this file to your Git repo using git add NewFile1.txt and then run git status again to see what has changed:

$ git add NewFile1.txt
$ git status

You'll get this output:

Git Status Output after adding a New File Git Status Tracked File

Notice how NewFile1.txt went from "untracked", which is displayed in red, to "changes to be committed", which is displayed in green. This means that NewFile1.txt is now in Git's "staging area" and will be included in the next commit. More on the "staging area" soon!

As you can see, git status enables you to keep track of the work you have done in your Git repository, and gives you helpful tips on how to proceed. As you work more with commits, branches, merges, etc., the git status command will become ever more useful. You'll be using git status continuously throughout this course. Use it anytime you're interacting with a Git repo from the CLI to understand the status of your Git repository.

Summary: Git Status

The git status command is used to print the status of a repository, enabling you to check the state of your files and changes as they travel from your working directory to your local or remote Git repository.

Great work! You practiced using git status to check the state of each of the following:

  • Local Repository
  • Remote Repository

Now that you've learned how to check the status of your repository, it's time to learn more about the different locations where your files and changes live as Git tracks them.