Git Status: A Beginner's Guide to Tracking Your Changes 🎯

beginner
18 min

Git Status: A Beginner's Guide to Tracking Your Changes 🎯

Welcome to our comprehensive guide on using git status! In this tutorial, we'll explore how to track changes in your projects using Git, a powerful version control system. By the end of this lesson, you'll be able to confidently navigate your way through the command line and understand the status of your project at any given moment. 📝

What is Git? 📝

Git is an open-source distributed version control system used for tracking changes in projects. It allows multiple people to work on the same project without overwriting each other's changes, and it makes it easy to collaborate, fix bugs, and manage multiple versions of a project.

Why Use Git? 💡

Using Git is essential for any developer, as it provides a way to manage and track changes to your codebase. It's an indispensable tool for collaborative projects and for maintaining the integrity of your codebase over time.

Installing Git 📝

Before we dive into git status, let's make sure you have Git installed on your machine. You can download Git from the official Git website (https://git-scm.com/) and follow the installation instructions for your operating system.

Getting Started with Git 📝

Once Git is installed, open a terminal or command prompt and navigate to your project directory. If you don't have a project yet, create a new one for this tutorial:

bash
mkdir git-tutorial cd git-tutorial touch readme.md

Now that we have a project and a file, let's initialize Git and create a new repository:

bash
git init

Introducing Git Status 📝

Now that we have a Git repository, we can start using the git status command. This command tells us the current status of our repository, including which files have been modified, which have been added, and which have been deleted.

The Anatomy of Git Status 📝

When you run git status, Git will display a message that looks something like this:

bash
On branch master No commits yet Nothing to commit (create/copy files and use "git add" to track)

Let's break down this message:

  • On branch master: This tells us which branch we're currently on (in this case, the master branch).
  • No commits yet: This indicates that we haven't committed any changes yet.
  • Nothing to commit (create/copy files and use "git add" to track): Git is letting us know that we can create or copy files, and then use git add to start tracking them.

Adding Files with Git 📝

Let's add our readme.md file to the Git repository using the git add command:

bash
git add readme.md

Now if we run git status, we'll see a different message:

bash
On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: readme.md

This message tells us that we have a new file in the repository (readme.md) that's ready to be committed.

Committing Changes with Git 📝

Now that we've added our file, let's commit our changes with the git commit command:

bash
git commit -m "Initial commit"

In this example, we're creating a commit with the message "Initial commit". This message describes the changes we've made in this commit.

After running this command, Git will display a message confirming the commit:

bash
[master (root-commit) 08a8e0d] Initial commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 readme.md

This message tells us that we've committed one file (readme.md) and that no lines were added or deleted.

Working with Git Status 📝

Now that we've committed our changes, let's make some changes to our readme.md file and see how git status responds:

bash
nano readme.md

(Edit the readme.md file and save it.)

If we run git status now, we'll see a message like this:

bash
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: readme.md No commits yet

This message tells us that we've modified our readme.md file, but we haven't yet added those changes to the Git repository.

Staging Changes with Git 📝

To stage our changes, we use the git add command again:

bash
git add readme.md

Now if we run git status, we'll see a message like this:

bash
On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: readme.md

This message tells us that our changes to readme.md are staged and ready to be committed.

Committing Staged Changes 📝

To commit our staged changes, we use the git commit command again:

bash
git commit -m "Modified readme.md"

After running this command, Git will display a message confirming the commit:

bash
[master 4973456] Modified readme.md 1 file changed, 1 insertion(+), 1 deletion(-)

This message tells us that we've committed one file (readme.md) and that one line was added and one line was deleted.

Git Status Cheat Sheet 📝

Here's a quick reference for some common Git status commands:

  • git status: Shows the current status of the repository
  • git add <file>...: Stages changes to be committed
  • git commit -m "<message>": Commits staged changes with a message
  • git restore <file>...: Discards changes in the working directory

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `git status` command show us about our Git repository?

That's it for our introductory lesson on git status! We've covered the basics of using Git to track changes in our projects, and we've seen how git status helps us keep track of our repository's current state. In future lessons, we'll explore more advanced Git concepts and learn how to collaborate with other developers using Git. Happy coding! 🎉