To complete a lab assignment for grading, you want to make sure all your local changes are pushed to your GitHub repository. The git status command is a useful tool to help you determine if your local repository is up-to-date with the remote repository on GitHub. Run git status and compare the output to some of the sample snippets below. You can also use git log to see recent commits and compare the log to the history on GitHub.

Your branch is up to date

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working directory clean

This is the ideal git status message. Being "up to date with 'origin/main'." means there is nothing to push. "working directory clean" means all the files in the current directory are being managed by git (or are being intentionally ignored via .gitignore) and the most recent version of the file has been committed.

There is nothing to do in this case. Your local repository is up-to-date with the remote repository on GitHub, or your remote is slightly ahead of your local repository and you can run git pull to update your local repository.

Untracked files

If git status mentions "Untracked files:", you may need to add one or more untracked files.

[01]$ git status
On branch main
Your branch is up to date with 'origin/main'.

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

	README.txt
  lab1

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

In this case, you probably want to add README.txt, but not lab1. The file lab1 is a binary file built automatically by the compiler when you run make. A general rule is if the file goes away when you run make clean, you do not want to add the file to git. Another good rule is that if you edit the file directly with your editor, the file belongs in git. So in this case, let’s add, commit and push changes to the new file README.txt.

[01]$ git add README.txt
[01]$ git commit -m "instructions on how to run programs"
[01]$ git push
[01]$ git status
On branch main
Your branch is up to date with 'origin/main'.

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

	lab1

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

This status message is OK. We are up-to-date, there is nothing to commit, but there is still an untracked file. Since this file should not belongvim in git, this status shows a good git repo with all updates available to the instructor and the partner.

Changes Not Staged for Commit

[01]$ git status
On branch main
Your branch is up-to-date with 'origin/main'.

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:   lab1.c
	modified:   README.txt

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

In this situation, the file lab1.c was previously added to git and is maintained under version control. However, there have been some recent changes to the file you need to add and commit to git. For each of the files that are listed as modified:, use git add to add these files to be part of the next commit. Once you have added all the files, use git commit -m "sample message" to save your changes to git.

[01]$ git add README.txt lab1.c
[01]$ git commit -m "Documented results"
[main c0ab9a9] Documented results
 2 files changed, 2 insertions(+), 2 deletions(-)
[01]$ git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

Note that the message at the end of git status now says "nothing to commit, working directory clean", but the second line reads "Your branch is ahead of 'origin/main' by 2 commits." Fix this with git push.

Changes to be Committed:

If you see the message "Changes to be committed: …​", it means you have previously run git add, but you have not run git commit.

[01]$ git status
On branch main
Your branch is up-to-date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

        modified:   lab1.c

Fix this by running git commit with a short message saying what you changed in the files.

[01]$ git commit -m "fixed bug in read_input(...)"

Your branch is ahead…​

If you get this message, you need to run git push to submit your changes to your instructor or your partner.

[01]$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

flour[01]$ git push
Counting objects: 5, done.
Delta compression using up to 6 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To /home/adas/share/cs31_f14/labs/01
   c0ab9a9..5809244  main -> main

flour[01]$ git status
On branch main
Your branch is up-to-date with 'origin/main'.

nothing to commit, working directory clean

Unmerged paths…​ both modified

When working with partners, you will eventually encounter a merge conflict. The output of git status will usually say something about unmerged paths, or say that one or more files are both modified. This is the trickiest case of git status to handle and you need to proceed a bit more carefully. A good place to start is to read the tutorial on Handling Merge Conflicts in git.

in the sample input below, git is giving some terrible advice. It is suggesting you run git add on the conflicted file and run git commit. You will need to run these steps eventually, but only after resolving the conflict. You will likely need to manually edit the conflicted files before adding and committing the fixes. Good planning and good communication with your partner can reduce merge conflicts, but it is good to know how to manage them when they do happen.
Don’t run git add immediately in this case.
Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

	both modified:      test1.c

Fatal: Not a git Repository

$ git status
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)

If you get this error, you are not in a directory that has a git repository. Use cd or refer to the Git setup docs regarding setting up your initial repo.

References