Run git log --follow -- File
In this step, we will explore how to track the history of a specific file, even if it has been renamed. This is where the git log --follow
command comes in handy.
First, let's make sure we are in our project directory.
cd ~/project/my-time-machine
Now, let's create a new file and add some content.
echo "This is the original content." > original_file.txt
Add and commit this file.
git add original_file.txt
git commit -m "Add original file"
You should see output similar to this:
[master 1a2b3c4] Add original file
1 file changed, 1 insertion(+)
create mode 100644 original_file.txt
Now, let's rename the file.
git mv original_file.txt renamed_file.txt
Check the status to see the change.
git status
You should see something like this:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: original_file.txt -> renamed_file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
message.txt
Commit the rename.
git commit -m "Rename original file"
You should see output similar to this:
[master 5d6e7f8] Rename original file
1 file changed, 0 insertions(+), 0 deletions(-)
rename original_file.txt -> renamed_file.txt (100%)
Now, let's use git log
to see the history of the renamed file.
git log renamed_file.txt
This will only show the commit where the file was renamed. To see the history before the rename, we need to use the --follow
option.
git log --follow renamed_file.txt
This command will show you the history of the file, following its name changes. You should see both the "Rename original file" commit and the "Add original file" commit.
The git log --follow
command is essential when you need to understand the complete history of a file that has been moved or renamed within your repository. It helps you trace the evolution of the file across different commits, regardless of its current name.