View the change history of a file using Git versioning
How can I view the change history of an individual file in Git, complete details with what has changed?
I have got as far as:
git log -- [filename]
which shows me the commit history of the file, but how do I get at the content of each of the file changes?
I'm trying to make the transition from MS SourceSafe and that used to be a simple
For this I'd use:
or to follow filename past renames
gitk --follow [filename]
You can use
git log -p filename
to let git generate the patches for each log entry.
git help log
for more options - it can actually do a lot of nice things :) To get just the diff for a specific commit you can
git show HEAD
or any other revision by identifier. Or use
to browse the changes visually.
Read more... Read less...
git log --follow -p -- path-to-file
This will show the entire history of the file (including history beyond renames and with diffs for each change).
In other words, if the file named
bar was once named
git log -p bar (without the
--follow option) will only show the file's history up to the point where it was renamed -- it won't show the file's history when it was known as
git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as
-p option ensures that diffs are included for each change.
If you prefer to stay text-based, you may want to use tig.
# apt-get install tig
- Homebrew (OS X):
$ brew install tig
Use it to view history on a single file:
Or browse detailed repo history:
gitk but text based. Supports colors in terminal!
You can also see when a specific line of code inside a file was changed with
git blame filename. This will print out a short commit id, the author, timestamp, and complete line of code for every line in the file.
This is very useful after you've found a bug and you want to know when it was introduced (or who's fault it was).
If you use SourceTree to visualize your repository (it's free and quite good) you can right click a file and select Log Selected
The display (below) is much friendlier than gitk and most the other options listed. Unfortunately (at this time) there is no easy way to launch this view from the command line — SourceTree's CLI currently just opens repos.
To show what revision and author last modified each line of a file:
git blame filename
or if you want to use the powerful blame GUI:
git gui blame filename