Git
File history
Learn to list the history of a specific file.
History of a specific file
The git log command normally lists the entire commit history in the repository. To see only the commits related to a specific file, add the file path to the end of the command. This filtering lets you quickly find who made changes to that file and when.
git log dosya.txt
git log --oneline -- dosya.txt
Placing a double dash (--) before the path prevents the filename from being confused with a branch name.
Renamed files and --follow
When a file is renamed, a standard git log lookup might lose the history prior to the rename. The --follow flag tells Git to follow file renames and provide the complete history. You can also add -p to inspect the patch diffs.
git log --follow yeni_ad.txt
git log -p --follow yeni_ad.txt
This allows you to review the full journey of a file even if it was moved or renamed multiple times.
Exercise List commit history of a file
List the commit subjects touching ayar.json using the command git log --format="%s" -- ayar.json.
Show example solution
git init -q
git config user.name "Zeynep"
git config user.email "zeynep@example.com"
echo "v1" > ayar.json
git add ayar.json
git commit -qm "Ayar dosyasi olusturuldu"
echo "v2" > ayar.json
git commit -am -q "Ayar dosyasi guncellendi"
echo "readme" > README.md
git add README.md
git commit -qm "Benioku eklendi"
git log --format="%s" -- ayar.json
Test yourself
-
Which command is used to see the one-line commit history of a specific file?
- git status --oneline rapor.txt
- git diff --oneline rapor.txt
- git log --oneline -- rapor.txt
- git show --all rapor.txt
Show answer
git log --oneline -- rapor.txt — When --oneline and a file path are provided to git log, only commits touching that file are listed in one line each.
-
Follow file renames
git log ____ -- eski_dosya.txt
Show answer
git log --follow -- eski_dosya.txt
-
File history options
- git log <dosya>
- Commits modifying the file
- git log --follow
- Follows file renames
- git log -p
- Also lists commit diffs
- git log --oneline
- Summarizes history in one line
Next lesson: Author and date