Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In git, can you view the older commits applied to a file after it has been moved?

Tags:

git

After I have moved a file in git (using git mv), looking at the log for that file only shows the commits including and after the move.

Is there any way to view the commits applied to the file under its old name? In the example, below, I'd like to see commits b04033bdc44f1 and 8ca40d563ce5d in git log after I've done the move.

 $ git init
Initialized empty Git repository in /Users/ben/code/git_mv_example/.git/
 $ touch foo
 $ git add foo
 $ git commit -m "Initial commit"
Created initial commit 8ca40d5: Initial commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
 [master*]$ echo "abcdefg" > foo
 [master*]$ git commit -a -m "edited foo"
Created commit b04033b: edited foo
 1 files changed, 1 insertions(+), 0 deletions(-)
 $ git log foo
commit b04033bdc44f1eb3477270b4b7ca727377d8c03a
Author: Ben Brinckerhoff <[email protected]>
Date:   Tue Jun 2 13:26:53 2009 -0600

    edited foo

commit 8ca40d563ce5d07d965bfb75a01b9c23378fd321
Author: Ben Brinckerhoff <[email protected]>
Date:   Tue Jun 2 13:26:15 2009 -0600

    Initial commit
 $ git mv foo bar
 [master+]$ git commit -a -m "renamed foo to bar"
Created commit 2bccdf6: renamed foo to bar
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename foo => bar (100%)
 $ git log bar
commit 2bccdf6fc65b9da5b279d9f1117e436549dd3a7b
Author: Ben Brinckerhoff <[email protected]>
Date:   Tue Jun 2 13:27:14 2009 -0600

    renamed foo to bar
 $ git log foo
fatal: ambiguous argument 'foo': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

Is this not possible? Or perhaps is git log the wrong command for this?

like image 489
Ben Brinckerhoff Avatar asked Jun 02 '09 19:06

Ben Brinckerhoff


People also ask

How can I see old commits?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

Does git keep track of moved files?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).


2 Answers

'git-log -M' will give the commit history with rename information, as mentioned in GitFaq "Why does git not "track" renames ?"

The diff machinery in Git has support for automatically detecting renames, this is turned on by the '-M' switch to the git-diff-* family of commands.
The rename detection machinery is used by git-log and git-whatchanged.
Git also supports a limited form of merging across renames. The two tools for assigning blame, git-blame and git-annotate both use the automatic rename detection code to track renames.

As a very special case, 'git log' version 1.5.3 and later has '--follow' option that allows you to follow renames when given a single path.


in your example:

F:\prog\git\test\rename>git log --follow bar
commit 81f52b91eb2fc7ad18051c93f3e4d583f27c15ca
Author: VonC <>
Date:   Tue Jun 2 21:54:43 2009 +0200

    renamed foo to bar

commit 71aff26ace6ab249ab2042d1e5d20377486ce478
Author: VonC <>
Date:   Tue Jun 2 21:54:19 2009 +0200

    edited foo

commit c893199da767eddac6a547b940557435ade4d18c
Author: VonC <>
Date:   Tue Jun 2 21:53:51 2009 +0200

    initial commit

Note: all of this is possible because you did not drastically modified content of foo before renaming to bar. If the content was entirely different, the --follow option would not been able to get the all history.

like image 132
VonC Avatar answered Oct 27 '22 09:10

VonC


Short answer: use

$ git log -M --follow bar

or

$ git log -M -- foo bar

specifying both old and new name.

By the way, as git uses heuristic similarity based rename detection, toy examples might not work like you want, while real life examples (or examples with more unchanged contents) would work as expected.

Note that in git path as an argument to git-log is not path filter, but path limiter (and can for example be a directory).

like image 36
Jakub Narębski Avatar answered Oct 27 '22 11:10

Jakub Narębski