Using gitk to view the full history of a moved fil

2019-03-14 12:21发布

问题:

After much searching, I have not found a satisfactory method that is easy to use to view the complete history of a moved file in Git and more importantly in Gitk. Using "git log --follow [filePath]" and even "gitk --follow [filePath]" gives you the commits that the file was involved in but will not show you the actual change history of the file before the move. I have thus come up with a crude but simple workaround solution.

  1. Do a gitk on the file that has been moved : “gitk [newFilePath]”. Copy the SHA1 ID of the first commit, this should be the commit where the file has been moved.
  2. Do a gitk on the copied SHA1 ID : “gitk [SHA1ID]”. The latest commit should be when the move has happened. Find the moved file and copy the old path.
  3. Do a gitk on the SHA1 ID we just copied and the old file path : “gitk [SHA1ID] -- [oldFilePath]”

This process will allow you to view the history of the file before the move. If there have been multiple moves the above process can be repeated.

If there are any better solutions to this problem, especially if there is a way to combine these steps to display the full history with the moves, it would be highly appreciated.

回答1:

If you want to see the changes that was made in each commit even the file has been renamed, you can use the option -p of git log:

git log -p --follow [file/with/path]


回答2:

Here's a bash function that should show you the history of a file (using gitk) in all its incarnations ... I'll leave it as an exercise to the reader if they need it for another shell:

# bash
gitk_follow () {
  while (( "$#" )); do
    git log --oneline --name-status --follow $1;
    shift;
  done | perl -ne 'if( s{^(?:[ACDMRTUXB]|R\d+)\s+}{} ) { s{\s+}{\n}g; print; }' | sort -u
}

# used as:
gitk $(gitk_follow some_file)

updated:

Changed to use perl because I didn't pay close enough attention to the output from git log in the last version.



回答3:

A simpler function using different git log options and awk to exact the file names (and the "--" that gitk needs is included):

# bash
gitk_follow () {
  while (( "$#" )); do
    git log --pretty="" --name-status --follow $1;
    shift;
  done | awk '{print $NF}' | sort -u
}

# used as:
gitk -- $(gitk_follow some_file)


回答4:

Another alternative is to use the SourceTree application (GUI) developed by Atlassian. Inside the application you can right-click on the file and select "Log Selected ..."

.

In the pop-up window it then allows you to "Follow renamed files":

I also use GitHub for Mac, but didn't see that kind of functionality in there, yet.

(I am not affiliated with either of them!)



标签: git move gitk