Uncommitted changes in Git can be tracked through a couple of key commands such as git status
and git diff
.
The git status
command shows the status of your working directory and staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
For example:
$ git status
A sample output might look like:
On branch main
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: file1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file2.txt
no changes added to commit (use "git add" and/or "git commit -a")
This output indicates that file1.txt
has been modified but not yet staged, and file2.txt
is a new file that hasn’t been tracked yet.
The git diff
command shows the changes between different commits, or between the working directory and the index, etc. It is often used in conjunction with git status
to see the detailed changes.
To see the actual changes made to files that are not yet staged, you can use:
$ git diff
You might see output that looks like this:
diff --git a/example.txt b/example.txt
index 557db03..718cd20 100644
--- a/example.txt
+++ b/example.txt
@@ -1,3 +1,3 @@
This is an example file.
-Here is some original content.
+Here is some new content.
The lines that begin with -
represent the original state, and the lines that begin with +
represent the new state. So, in this example, the line “Here is some original content.” has been changed to “Here is some new content.”
And to see the changes that are staged but not yet committed, you can use:
$ git diff --staged
Now, if you stage the changes (using git add example.txt
) and then use git diff --staged
, you would see the same changes but this time they reflect the difference between your staged changes and your last commit.
Remember, git diff
without any parameters shows the difference between your working directory and the staging area (i.e., changes that are not yet staged). On the other hand, git diff --staged
(or git diff --cached
) shows the difference between staged changes and the last commit.