I changed the name of an old file I committed several days ago. Now I need the name of that original document for a board meeting presentation. If it makes any difference, I'm on iMac OSX using iTerm2.
I have already tried the following commands without any luck. I'm expecting to see the actual original, user-end filename. Example of expected result: "01_Introduction_Statistics.pages"
git log
git log --pretty=oneline --graph --name-status
git show --pretty=oneline --graph --name-status
git diff --name-only
git svn log -v
git show --[=pretty<format>]
git show --oneline
git log --raw
git show --name-only
**git blame -f, --show-name show original filename (Default: auto)
This last one appears to provide what I need, but I'm not sure how to use it. I keep getting "error: unknown option `-,'" I have no idea what that means.
I have already tried
git blame -f, --show-name <hash code>
It returns an error. I'm lost and confused.
If you have a file and you want to know when it was renamed...
git log --follow --summary --format= <filename>
--follow
will continue listing history through renames. --format=
suppresses the usual output. --summary
shows when the file was created, renamed, or its permissions changed.
$ git log --follow --summary -w --format= baz
rename bar => baz (100%)
rename foo => bar (100%)
create mode 100644 foo
The original name of this file was foo
. It was renamed to bar
with no content changes. Then renamed again to baz
with no content changes.
Note: Git does not track renames. Instead it uses heuristics to look for similar files. A simple git mv
like in the history above is easy for Git to follow. However, if you rename a file and alter it in one commit, Git might not be able to follow the rename. It depends on how much was altered and how hard you tell Git to look.
By default it looks for files with at least 50% similarity. You can affect this with -M
and -l
.
For example, here I git mv baz blat
and also added a line and made some whitespace changes.
commit 3c3240d16866a91b499fe17d478b82277c83aded
Author: Michael G. Schwern <schwern@pobox.com>
Date: Fri May 10 12:22:59 2024 -0700
mv baz blat, add this
diff --git a/baz b/blat
similarity index 37%
rename from baz
rename to blat
index dfa74aa..43b1a69 100644
--- a/baz
+++ b/blat
@@ -1,4 +1,7 @@
this
+that
+
other
-thing
+
+ thing
This resulted in a similarity threshold below 50%. I need to lower Git's standards for looking for renames to 30%.
$ git log --follow --summary --format= blat
create mode 100644 blat
$ git log --follow --summary --format= -M.3 blat
rename baz => blat (37%)
rename bar => baz (100%)
rename foo => bar (100%)
create mode 100644 foo
Because this is a heuristic, Git can get it wrong. You still need to verify.
For this reason, if you need to rename a file and then change a significant portion of the file (and if it's a very small file any change can be significant) it's good practice to rename in one commit and alter in another, even if this results in a broken commit.