Difference between untracked, unstaged and staged changes

In a nutshell: Untracked changes are not in Git. Unstaged changes are in Git but not marked for commit. Staged changes are in Git and marked for commit.

Untracked vs. tracked files

Each file in your Git folder can be sorted into one of two categories.

  • Untracked - This file exists locally, but isn’t a part of the Git repository. The file’s change history will not be recorded and it will not be pushed to remote copies of your repository when you run git push. Unless they’ve been added to an ignore list, untracked files will show up when viewing your Git status.

    Here’s an example where we created untracked_file in the main folder without adding it:

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	untracked_file

nothing added to commit but untracked files present (use "git add" to track)
  • Tracked - Git tracks the file’s change history and it will be pushed to remote copies when running git push. These files will show up in your Git status report if there are differences between the version on your hard drive and the last committed version.

An untracked file becomes a tracked file when it is added using git add [file]. A tracked file becomes untracked when running git rm --cached [file]. This will remove the file from Git while preserving your local copy.

What’s the difference between all the different types of changes?

Untracked changes are changes to untracked files. Git does not keep a record of them. Changes to tracked files can be sorted into two categories.

  • Unstaged changes exist in your working directory, but Git hasn’t recorded them into its version history yet. You’ll usually want to stage them (mark them to become part of your next commit) or discard them by restoring the last committed version of the file.

    An unstaged change looks like this when viewing your Git status:

    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
      
    	modified:   notes
      
    no changes added to commit (use "git add" and/or "git commit -a")
    
  • Staged changes are a lot like unstaged changes, except that they’ve been marked to be committed the next time you run git commit. Upon your next commit, your staged changes become part of your Git history. git status will no longer list them as changes since they’re part of your last commit now.

    If you decide you don’t want to proceed with a staged change after all, you can do two things. Sometimes the easiest way to stage a change that cancels out your previous change. Example: if you staged a few new lines but you decide you don’t want to keep them, you can delete the lines, save your file and stage that change. You can also use this trick to unstage specific bits of text. This is what a staged change looks like:

    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
      
    	modified:   notes
    

Note that a file can contain both unstaged and staged changes at the same time. Here’s what that looks like:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   notes

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   notes

Can I have a last look at all staged changes before I commit?

Yes, you can run git diff --staged whenever you want to see them. Here’s an example:

diff --git a/notes b/notes
index cb799d4..605ca3c 100644
--- a/notes
+++ b/notes
@@ -1 +1,3 @@
 These are my notes.
+
+This is a staged change.

Special types of changes

  • Creation of a new tracked file. When you git add an untracked file so that Git will start tracking it, the file will appear as new file under “Changes to be committed” in your Git status. You can unstage the addition of this file just like you would unstage any other change. Again, there’s a trick for that.
  • Deletion of a tracked file. When you git rm a tracked file to remove it from your repository and delete your local copy, the file will appear as deleted under “Changes to be committed”. Like above, you can unstage this deletion in the usual ways. The file will reappear after unstaging the change.