Tracking Changes in Git

As you work on your project, your project files will always be changed, new files will be added. And other files will be deleted .Keeping track of these changes is an essential step to ensure that nothing important is left untracked or unstaged. The git status command is your go-to tool for monitoring and tracking the state of your working directory and staging area.

In this lesson, we’ll explain what is the git status command does, how to interpret its output, , how to use it effectively in your workflow and how to get most benefit from it.

What Does git status Command Do?

The git status command provides us with some essential information about:

  1. 1
    Untracked files: Tell us about files that are still not be tracked by Git.
  2. 2
    Changes to tracked files: What is the modifications that we do to our project files that Git is already tracking.
  3. 3
    Staged files: Files added to the staging area, ready to be committed.
  4. 4
    Branch information: The current branch and any changes that have not been pushed.

This command doesn’t have access to modify your repository code structure.it’s purely informational, making it safe to use anytime.

Using git status

To learn how to use git status command effectively and in a safe way, ensure that you follow these steps:

  • Open your terminal and navigate to your Git repository file. then run the git status command on your terminal:
bash
1
          git status
        
  • Refer to the following real-world example to see the expected output:

If you’ve just created a new file but haven’t staged or committed it yet, the output will look like this:

bash
1
2
3
4
5
          On branch main
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Index.html
        
  • Untracked files: These files which are new files that Git hasn’t started tracking yet.
  • Hint: Git provides a helpful tip on how to stage these files using git add.

Tracking New Files

To track the untracked files in your project, you use the following steps:

  • Stage the file:
bash
1
          git add index.html
        
  • Check the status again:
bash
1
          git status
        

Output will now show the file under "Changes to be committed":

bash
1
2
3
          Changes to be committed:
(use "git restore --staged <file>..." to unstage)
 	new file:   index.html
        

Tracking Changes

If you modify a file that Git is already tracking, git status will show it as a modified file:

bash
1
2
3
4
          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:   index.html
        

Steps to Track Changes

1. Stage the changes:

bash
1
          git add index.html
        

2. Verify the status:

bash
1
          git status
        

Output will now move the file to the staged section, ready for commit.

Cleaning Up with .gitignore

In some cases, you might see files that you don’t want Git to track, such as temporary files or log files. These can clutter the output of git status. To exclude such files, use a .gitignore file. ( This means to ignore this file )

1. Create a .gitignore file in your repository root using this command:

bash
1
          touch .gitignore
        

2. Add patterns to ignore, such as:

bash
1
2
          *.log
node_modules/
        

3. Lastly, check the status again:

bash
1
          git status
        

Unwanted files matching the patterns will no longer appear and will be ignored now.

Tips for Using git status Effectively

  • Run it often: Get into the habit of running git status before staging or committing changes.
  • Combine with other commands: Use it with git add and git commit commands to understand how your actions affect your repository.
  • Learn the hints: Pay attention to Git’s suggestions in the output, they often provide the exact command you need to proceed.

Conclusion

The git status command is an essential tool for keeping track of your project’s state. By understanding its output and using it frequently, you can ensure you will have a clean and organized repository. In the next lesson, we’ll explore how to stage and commit files to preserve your work in Git’s version history.