Git blame -- prior commits?

113,844

Solution 1

git blame -L 10,+1 fe25b6d^ -- src/options.cpp

You can specify a revision for git blame to look back starting from (instead of the default of HEAD); fe25b6d^ is the parent of fe25b6d.


Edit: New to Git 2.23, we have the --ignore-rev option added to git blame:

git blame --ignore-rev fe25b6d

While this doesn't answer OP's question of giving the stack of commits (you'll use git log for that, as per the other answer), it is a better way of this solution, as you won't potentially misblame the other lines.

Solution 2

You can use git log -L to view the evolution of a range of lines.

For example :

git log -L 15,23:filename.txt

means "trace the evolution of lines 15 to 23 in the file named filename.txt".

Solution 3

Amber's answer is correct but I found it unclear; The syntax is:

git blame {commit_id} -- {path/to/file}

Note: the -- is used to separate the tree-ish sha1 from the relative file paths. 1

For example:

git blame master -- index.html

Full credit to Amber for knowing all the things! :)

Solution 4

You might want to check out:

git gui blame <filename>

Gives you a nice graphical display of changes like "git blame" but with clickable links per line, to move into earlier commits. Hover over the links to get a popup with commit details. Not my credits... found it here:

http://zsoltfabok.com/blog/2012/02/git-blame-line-history/

git gui is a graphical Tcl/Tc interface to git. Without any other params it starts a pretty simple but useful graphical app for committing files, hunks or even single lines and other similar commands like amend, revert, push... It's part of the git stock suite. On windows it is included in the installer. On debian - I don't know about other *nix systems - it has to be installed separately:

apt-get install git-gui

From the docs:

https://git-scm.com/docs/git-gui

DESCRIPTION

A Tcl/Tk based graphical user interface to Git. git gui focuses on allowing users to make changes to their repository by making new commits, amending existing ones, creating branches, performing local merges, and fetching/pushing to remote repositories.

Unlike gitk, git gui focuses on commit generation and single file annotation and does not show project history. It does however supply menu actions to start a gitk session from within git gui.

git gui is known to work on all popular UNIX systems, Mac OS X, and Windows (under both Cygwin and MSYS). To the extent possible OS specific user interface guidelines are followed, making git gui a fairly native interface for users.

COMMANDS

blame

Start a blame viewer on the specified file on the given version (or working directory if not specified).

browser

Start a tree browser showing all files in the specified commit. Files selected through the browser are opened in the blame viewer.

citool

Start git gui and arrange to make exactly one commit before exiting and returning to the shell. The interface is limited to only commit actions, slightly reducing the application’s startup time and simplifying the menubar.

version

Display the currently running version of git gui.

Solution 5

Building on the previous answer, this bash one-liner should give you what you're looking for. It displays the git blame history for a particular line of a particular file, through the last 5 revisions:

LINE=10 FILE=src/options.cpp REVS=5; for commit in $(git rev-list -n $REVS HEAD $FILE); do git blame -n -L$LINE,+1 $commit -- $FILE; done

In the output of this command, you might see the content of the line change, or the line number displayed might even change, for a particular commit.

This often indicates that the line was added for the first time, after that particular commit. It could also indicate the line was moved from another part of the file.

Share:
113,844
Sedate Alien
Author by

Sedate Alien

Updated on July 11, 2022

Comments

  • Sedate Alien
    Sedate Alien almost 2 years

    Is it possible to see who edited a specific line before the commit reported by git blame, like a history of commits for a given line?

    For example, I run the following (on the superb uncrustify project):

    $ git blame -L10,+1 src/options.cpp
    ^fe25b6d (Ben Gardner 2009-10-17 13:13:55 -0500 10) #include "prototypes.h"
    

    How can I find out who edited that line before commit fe25b6d? And who edited it before that commit?

  • Anders Zommarin
    Anders Zommarin over 13 years
    Can you get a complete history, without having to re-enter the command several times with different hashes?
  • Sedate Alien
    Sedate Alien over 13 years
    Superb! I knew it should have been simple. I had tried ^fe25b6d but not fe25b6d^, silly me. I second the question posed by @mrz; this is nice functionality, but a full history with one command would be wonderful.
  • Amber
    Amber over 13 years
    I don't believe Git has a built-in way of getting every blame that touched a line number (which kind of makes sense, since a given line may not have a consistent line number throughout the history of a file due to insertions and deletions of lines).
  • Cascabel
    Cascabel over 13 years
    @Amber: Pretty sure you're right that the feature doesn't exist, but it does sort of seem like it could be implemented naively, by simply doing what a human would do: blame it once, grab the reported information, blame that, and so on.
  • Amber
    Amber over 13 years
    Sure; you could certainly do it naively with a fairly trivial shell script.
  • Zitrax
    Zitrax about 13 years
    git gui makes it quite easy to check the history of a line as the versions are clickable.
  • shadyabhi
    shadyabhi over 11 years
    What is -- doing here? Where can I know more about it in the documentation?
  • Amber
    Amber over 11 years
    @shadyabhi -- is commonly used as a separator in command line arguments - in the case of Git, it's usually used to separate things like commit hashes from a list of filenames.
  • Max Nanasy
    Max Nanasy over 11 years
    Note that this is the last $REVS revisions in which $FILE changed, rather than the last $REVS revisions in which $LINE changed.
  • HRJ
    HRJ over 10 years
    @Zitrax which git GUI are you referring to?
  • ThorSummoner
    ThorSummoner about 10 years
    Amber's answer is correct but I found it unclear; The syntax is: git blame {sha1} -- {path/to/file} Note: the -- is used to separate the tree-ish sha1 from the relative file paths. 1 For example: git blame master -- index.php. Full credit to Amber for knowing all the things! :)
  • ThorSummoner
    ThorSummoner about 10 years
    I agree with your sentiment. The comment system is however too restricted to present all the information clearly. I have added the content of this answer in a comment; though, I insist on leaving this answer is place for ease of access.
  • Flimm
    Flimm almost 10 years
    Which answer are you referring to?
  • Flimm
    Flimm almost 10 years
    It should either be a separate post or an edit. I like this it as a separate answer.
  • Will Sheppard
    Will Sheppard almost 10 years
    I don't remember anymore. Possibly I could have future-proofed my answer better.
  • bigtex777
    bigtex777 almost 9 years
    This is a solid answer and addresses Anders Zommarin's question above on how to see the changes to specific lines over time.
  • ssgao
    ssgao over 8 years
    @AndersZommarin please see Thomas's answer for what you're looking for
  • Kasun Siyambalapitiya
    Kasun Siyambalapitiya over 7 years
    @Amber sorry, I need to get this kind information from the github repository. what I meant to ask is that how can we obtain the same information from the github API
  • Neon
    Neon over 6 years
    FYI: git log -L <start>,<end>:<file> requires Git 1.8.4+ see: git-scm.com/docs/git-log#git-log--Lltstartgtltendgtltfilegt for syntax options
  • hsestupin
    hsestupin over 6 years
    in Emacs it works like a charm - emacs.stackexchange.com/questions/31010/…
  • alexanderbird
    alexanderbird about 6 years
    if you just want the previous commit, git blame HEAD^ or the commit before that git blame HEAD^^ etc.
  • BeeOnRope
    BeeOnRope almost 6 years
    It doesn't work for me. I can click the change on the given line, but that just changes the view to be for that commit, and the current line now shows as this: but how do I see the previous version of the line and when it was added?
  • cambunctious
    cambunctious about 5 years
    This is the one use case I know of where git gui is the best solution
  • Tizio Fittizio
    Tizio Fittizio almost 5 years
    I created an alias to use this: git config --global alias.changes 'log -p -M --follow --stat --' and then I can simply type git changes path/to/your/file
  • maesk
    maesk over 4 years
    This is by far the best answer and exactly what I was looking for. Simple and elegant.
  • Steve Chambers
    Steve Chambers about 4 years
    This worked better than the other answers for me (using IntelliJ). Took a while to load all the revisions but worth the wait.
  • Timmmm
    Timmmm about 4 years
    This didn't work for me with a line that had been moved from another file.
  • gawkface
    gawkface over 3 years
    adding multiple ignore-rev <commit-id> --ignore-rev <commit-id> allows one to ignore multiple layers of commits even (which can be configured in config file; if those commits always need to be ignored)
  • Gabriel Staples
    Gabriel Staples over 3 years
    I highly recommend adding -p to show all the git diff-like changes as well, like this, for instance, for the whole file: git log -p filename.txt.
  • Scott McPeak
    Scott McPeak about 2 years
    @BeeOnRope After clicking on the line so it says this, right-click on that line, and choose "Blame Parent Commit" from the menu that pops up.