File history
- Git course
- Beginner
- Lesson 46/80
- 4 min
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.txtPlacing 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.txtThis allows you to review the full journey of a file even if it was moved or renamed multiple times.
Practice List commit history of a file
List the commit subjects touching ayar.json using the command git log --format="%s" -- ayar.json.
Starter code
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"
# ayar.json gecmisi icin komutu buraya yazSample 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.jsonExpected output
Ayar dosyasi guncellendi Ayar dosyasi olusturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
- git show --all rapor.txt
Explanation: When --oneline and a file path are provided to git log, only commits touching that file are listed in one line each.
2 Follow file renames
git log ____ -- eski_dosya.txt
git log --follow -- eski_dosya.txt
Explanation: The --follow flag continues tracking the previous history even if the file was renamed.
3 File history options
| Term | Meaning |
|---|---|
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 |