Git FAQ and Troubleshooting

Jan 26, 2024  β”‚  m. Jan 26, 2024 by Charlotte Curtis

Git is a powerful, versatile, and sometimes infuriating version control tool. This page describes some problems, solutions, and ways that git can be useful.

Recovering deleted files

Sometimes, you might accidentally delete or overwrite a file. Git is here to help! First, check to see if you’ve committed the deletion yet by running git status. Say I deleted my makefile, for example. If you haven’t committed, you’ll see something like:

$ git status
On branch main
Your branch is ahead of 'teacher/main' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    makefile

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

In this case, we can recover it easily with the following command:

$ git restore makefile

If the change has been committed, you won’t see it in git status anymore. In this case, you’ll need to find the commit before the deletion/overwrite happened. Run the command:

$ git log --stat -- <filename>

where <filename> is the file that you want to recover (in my case, makefile). This will show you a list of commits that affected the file, along with the changes that were made. Find the commit before the deletion/overwrite happened, and copy the commit hash (the long string of letters and numbers at the top of the commit message). Then, run the command:

$ git checkout <commit hash> <filename>

where <commit hash> is the hash you copied, and <filename> is the file you want to recover. This will restore the file to the state it was in at that commit.

Checking what changed

Your code was working yesterday, but then you made some changes and now it isn’t! What changed??

Case 1: You have not yet committed

If you haven’t committed, you can use git diff without any arguments to see all the changes since your last commit. For example:

$ git diff
diff --git a/functions/functions.cpp b/functions/functions.cpp
index 40ba830..e8372bf 100644
--- a/functions/functions.cpp
+++ b/functions/functions.cpp
@@ -1,4 +1,4 @@
-#include "functions.h"
+include "functions.h"

Okay, some obscure symbols here, but ultimately this is telling me that I deleted the line #include "functions.h" and added the line include "functions.h". Looks like I deleted the # by mistake, so that’s probably the cause of my problem!

You can also add a filename to git diff to see the changes to that file only. For example:

$ git diff functions/functions.cpp

would produce the same output in this case.

Case 2: You have committed

If you have committed, you’ll need to tell git which commit you want to compare to. You can do this with the git diff <commit hash> command. For example:

$ git log
commit 8cab5cf617a797483cf025461c49247bd6dd81aa (HEAD -> main)
Author: Charlotte Curtis <ccurtis@mtroyal.ca>
Date:   Fri Jan 26 17:10:51 2024 -0700

    Adding change to functions.cpp

commit 9fc744543511520190fe0e6d8dafab4012625b85
Author: Charlotte Curtis <ccurtis@mtroyal.ca>
Date:   Fri Jan 26 17:08:15 2024 -0700

    Restoring makefile
... etc

Let’s say I want to compare to the second most recent commit. I can copy the hash 9fc744543511520190fe0e6d8dafab4012625b85 and run the command:

$ git diff 9fc744543511520190fe0e6d8dafab4012625b85

This will show me all the changes since that commit.

Alternatively, you can use git diff HEAD~1 to compare to the previous commit, or git diff HEAD~2 to compare to the commit before that, etc.



Previous: Coding Style and Documentation
Next: Fixing Git Labs