View the change history of a file using Git versioning

后端 未结 24 1769
臣服心动
臣服心动 2020-11-22 16:40

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]
         


        
相关标签:
24条回答
  • 2020-11-22 17:07

    I'm probably about where the OP was when this started, looking for something simple that would let me use git difftool with vimdiff to review changes to files in my repo starting from a specific commit. I wasn't too happy with answers I was finding, so I threw this git incremental reporter (gitincrep) script together and it's been useful to me:

    #!/usr/bin/env bash
    
    STARTWITH="${1:-}"
    shift 1
    
    DFILES=( "$@" )
    
    RunDiff()
    {
            GIT1=$1
            GIT2=$2
            shift 2
    
            if [ "$(git diff $GIT1 $GIT2 "$@")" ]
            then
                    git log ${GIT1}..${GIT2}
                    git difftool --tool=vimdiff $GIT1 $GIT2 "$@"
            fi
    }
    
    OLDVERS=""
    RUNDIFF=""
    
    for NEWVERS in $(git log --format=format:%h  --reverse)
    do
            if [ "$RUNDIFF" ]
            then
                    RunDiff $OLDVERS $NEWVERS "${DFILES[@]}"
            elif [ "$OLDVERS" ]
            then
                    if [ "$NEWVERS" = "${STARTWITH:=${NEWVERS}}" ]
                    then
                            RUNDIFF=true
                            RunDiff $OLDVERS $NEWVERS "${DFILES[@]}"
                    fi
            fi
            OLDVERS=$NEWVERS
    done
    
    

    Called with no args, this will start from the beginning of the repo history, otherwise it will start with whatever abbreviated commit hash you provide and proceed to the present - you can ctrl-C at any time to exit. Any args after the first will limit the difference reports to include only the files listed among those args (which I think is what the OP wanted, and I'd recommend for all but tiny projects). If you're checking changes to specific files and want to start from the beginning, you'll need to provide an empty string for arg1. If you're not a vim user, you can replace vimdiff with your favorite diff tool.

    Behavior is to output the commit comments when relevant changes are found and start offering vimdiff runs for each changed file (that's git difftool behavior, but it works here).

    This approach is probably pretty naive, but looking through a lot of the solutions here and at a related post, many involved installing new tools on a system where I don't have admin access, with interfaces that had their own learning curve. The above script did what I wanted without dealing with any of that. I'll look into the many excellent suggestions here when I need something more sophisticated - but I think this is directly responsive to the OP.

    0 讨论(0)
  • 2020-11-22 17:09

    SmartGit:

    1. In the menu enable to display unchanged files: View / Show unchanged files
    2. Right click the file and select 'Log' or press 'Ctrl-L'
    0 讨论(0)
  • 2020-11-22 17:11

    Or:

    gitx -- <path/to/filename>

    if you're using gitx

    0 讨论(0)
  • 2020-11-22 17:13

    git whatchanged -p filename is also equivalent to git log -p filename in this case.

    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).

    0 讨论(0)
  • 2020-11-22 17:15

    For this I'd use:

    gitk [filename]
    

    or to follow filename past renames

    gitk --follow [filename]
    
    0 讨论(0)
  • 2020-11-22 17:15

    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 foo, then 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 foo. Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo. The -p option ensures that diffs are included for each change.

    0 讨论(0)
提交回复
热议问题