File statuses#

In git, files can take on different states with respect to the current commit. The following picture mention central idea.

Get status#

The git status command allows you to get some information about the current status of the git repository. This command is very common on this site, so you can find many different examples of how to use it.

Untracked files#

If git hasn’t seen a file before, it will correspond to this category. You can find files on this stage in Untracked files section of git status command.

In the following example, I am simply trying to add a some text file to the freshly created git repo, and when I call git status, I can find it in the untracked files section.

%%bash
mkdir status_test
cd status_test
git init &> /dev/null

echo "=====Empty repo====="
git status
echo "some text" > test_file
echo
echo "=====One file added====="
git status

cd ..
rm -r  status_test
=====Empty repo=====
On branch master

No commits yet

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

=====One file added=====
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test_file

nothing added to commit but untracked files present (use "git add" to track)

To make git track some file you should use command git add <filemane>.

Staged#

These are the files for the next commit.

In git status they’re shown in the Changes to be committed section.

So in the following example, I have created and added a file to be tracked in the git repo, and show the result of git status for such a repository.

%%bash
mkdir status_test
cd status_test
git init &> /dev/null

echo "some text" > test_file
git add test_file
echo
echo "=====File to be commited====="
git status

cd ..
rm -r  status_test
=====File to be commited=====
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   test_file

You can commit changes by using git commit command.

Commited files#

If you just commit all the files and check git status, you will always get the same message.

You can also check the list of commits using git log.

So in the following example, I will try git commit and git log on the repository with a single commit and no changes since that commit.

%%bash
mkdir status_test
cd status_test
git init &> /dev/null

echo "some text" > test_file
git add test_file
git commit -m "my message" &> /dev/null
echo
echo "=====git status====="
git status
echo "=====git log====="
git log


cd ..
rm -r  status_test
=====git status=====
On branch master
nothing to commit, working tree clean
=====git log=====
commit 6e0339d191892a011e66351179c1b7cb9b08b292
Author: Fedor Kobak <kobfedsur@gmail.com>
Date:   Wed Sep 6 15:02:05 2023 +0300

    my message

Changed#

Any file that has been changed since the last commit will appear in the Changes not staged to commit section, so if you try to commit in this momed, any changes made to these files won’t be committed.

To commit these changes you need to use git add or git commit -a.

The following example shows the difference:

  • The first print shows test_file as modified in the Changes not staged for commit block;

  • Second block corresponds to case if you’re trying to commit unstaged files. Interestingly, if you’re trying to commit without any changes, it won’t commit and will print as for git status;

  • Last block is same as previous, but before committing I did git add <filename> - so new commit was added, which is displayed in the git status and git log results.

%%bash
mkdir status_test
cd status_test
git init &> /dev/null

echo "some text" > test_file
git add test_file &> /dev/null
git commit -m "first commit" &> /dev/null
echo "some other text" > test_file

echo "=====Changed file====="
git status

echo
echo "=====Not added commit====="
echo "-----git commit-----"
git commit -m "unsuccessful commit"
echo "-----git log-----"
git log --oneline

echo
echo "=====Add file====="
git add test_file
echo "-----git commit-----"
git commit -m "second commit"
echo "-----git status-----"
git status
echo "-----git log-----"
git log --oneline

cd ..
rm -r  status_test
=====Changed file=====
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:   test_file

no changes added to commit (use "git add" and/or "git commit -a")

=====Not added commit=====
-----git commit-----
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:   test_file

no changes added to commit (use "git add" and/or "git commit -a")
-----git log-----
aaad9bc first commit

=====Add file=====
-----git commit-----
[master 4dba109] second commit
 1 file changed, 1 insertion(+), 1 deletion(-)
-----git status-----
On branch master
nothing to commit, working tree clean
-----git log-----
4dba109 second commit
aaad9bc first commit