# How to tell if a file was renamed between two commits

6 messages from 2010-02-03 to 2010-02-03. Participants: Ron Garret, Shawn O. Pearce.
Thread: https://gitlist.dev/t/22499

## Ron Garret, 2010-02-03 02:29

Subject: How to tell if a file was renamed between two commits
Message-ID: <ron1-2EA926.18292002022010@news.gmane.org>
URL: https://gitlist.dev/e/ron1-2EA926.18292002022010%40news.gmane.org

```
I'm trying to write a little utility that will extract all the revisions 
of a particular file.  I start with a git rev-list HEAD -- filename, get 
the tree objects with git cat-file commit, the file objects with git 
ls-tree, and finally the file contents themselves with git cat-file 
blob.  It works, except in the case where the file name was changed.  
git rev-list is smart enough to track those name changes, but my little 
revision tracker isn't.  It dies when suddenly there is no file with the 
right name in the tree.

So... is there an easy way to work around this?  Is there a way to get, 
say, rev-list to tell me when the file it is tracking changed names?  Or 
a git-diff incantation?  I just need something that will tell me given 
two commits and a file name whether the file was renamed between those 
two commits and if so what its new name is.  There must be an easy way 
to do this, but I can't figure out what it is.

Thanks,
rg

```

## Shawn O. Pearce, 2010-02-03 02:32

Subject: Re: How to tell if a file was renamed between two commits
Message-ID: <20100203023219.GA13092@spearce.org>
URL: https://gitlist.dev/e/20100203023219.GA13092%40spearce.org
In-Reply-To: <ron1-2EA926.18292002022010@news.gmane.org>

```
Ron Garret <ron1@flownet.com> wrote:
> I'm trying to write a little utility that will extract all the revisions 
> of a particular file.  I start with a git rev-list HEAD -- filename, get 
> the tree objects with git cat-file commit, the file objects with git 
> ls-tree, and finally the file contents themselves with git cat-file 
> blob.  It works, except in the case where the file name was changed.  
> git rev-list is smart enough to track those name changes, but my little 
> revision tracker isn't.  It dies when suddenly there is no file with the 
> right name in the tree.
> 
> So... is there an easy way to work around this?  Is there a way to get, 
> say, rev-list to tell me when the file it is tracking changed names?  Or 
> a git-diff incantation?  I just need something that will tell me given 
> two commits and a file name whether the file was renamed between those 
> two commits and if so what its new name is.  There must be an easy way 
> to do this, but I can't figure out what it is.

Maybe use the -M flag to git log, or the --follow flag to
log/rev-list?

-- 
Shawn.

```

## Ron Garret, 2010-02-03 02:48

Subject: Re: How to tell if a file was renamed between two commits
Message-ID: <ron1-08A0F6.18483502022010@news.gmane.org>
URL: https://gitlist.dev/e/ron1-08A0F6.18483502022010%40news.gmane.org
In-Reply-To: <20100203023219.GA13092@spearce.org>

```
In article <20100203023219.GA13092@spearce.org>,
 "Shawn O. Pearce" <spearce@spearce.org> wrote:

> Ron Garret <ron1@flownet.com> wrote:
> > I'm trying to write a little utility that will extract all the revisions 
> > of a particular file.  I start with a git rev-list HEAD -- filename, get 
> > the tree objects with git cat-file commit, the file objects with git 
> > ls-tree, and finally the file contents themselves with git cat-file 
> > blob.  It works, except in the case where the file name was changed.  
> > git rev-list is smart enough to track those name changes, but my little 
> > revision tracker isn't.  It dies when suddenly there is no file with the 
> > right name in the tree.
> > 
> > So... is there an easy way to work around this?  Is there a way to get, 
> > say, rev-list to tell me when the file it is tracking changed names?  Or 
> > a git-diff incantation?  I just need something that will tell me given 
> > two commits and a file name whether the file was renamed between those 
> > two commits and if so what its new name is.  There must be an easy way 
> > to do this, but I can't figure out what it is.
> 
> Maybe use the -M flag to git log, or the --follow flag to
> log/rev-list?

Nope.  git log --follow will follow through a name change but won't 
actually say when the name changed happened or what the previous name of 
the file was.

And actually playing around with it some more, it appears that git 
rev-list doesn't actually track file renames, or at least it doesn't do 
it all the time.  Weird.  I'm going to have to play around with this 
some more.

rg

```

## Ron Garret, 2010-02-03 02:52

Subject: Re: How to tell if a file was renamed between two commits
Message-ID: <ron1-5818AE.18520202022010@news.gmane.org>
URL: https://gitlist.dev/e/ron1-5818AE.18520202022010%40news.gmane.org
In-Reply-To: <ron1-08A0F6.18483502022010@news.gmane.org>

```
In article <ron1-08A0F6.18483502022010@news.gmane.org>,
 Ron Garret <ron1@flownet.com> wrote:

> In article <20100203023219.GA13092@spearce.org>,
>  "Shawn O. Pearce" <spearce@spearce.org> wrote:
> 
> > Ron Garret <ron1@flownet.com> wrote:
> > > I'm trying to write a little utility that will extract all the revisions 
> > > of a particular file.  I start with a git rev-list HEAD -- filename, get 
> > > the tree objects with git cat-file commit, the file objects with git 
> > > ls-tree, and finally the file contents themselves with git cat-file 
> > > blob.  It works, except in the case where the file name was changed.  
> > > git rev-list is smart enough to track those name changes, but my little 
> > > revision tracker isn't.  It dies when suddenly there is no file with the 
> > > right name in the tree.
> > > 
> > > So... is there an easy way to work around this?  Is there a way to get, 
> > > say, rev-list to tell me when the file it is tracking changed names?  Or 
> > > a git-diff incantation?  I just need something that will tell me given 
> > > two commits and a file name whether the file was renamed between those 
> > > two commits and if so what its new name is.  There must be an easy way 
> > > to do this, but I can't figure out what it is.
> > 
> > Maybe use the -M flag to git log, or the --follow flag to
> > log/rev-list?
> 
> Nope.  git log --follow will follow through a name change but won't 
> actually say when the name changed happened or what the previous name of 
> the file was.
> 
> And actually playing around with it some more, it appears that git 
> rev-list doesn't actually track file renames, or at least it doesn't do 
> it all the time.  Weird.  I'm going to have to play around with this 
> some more.
> 
> rg

Ah, I think I found it:

git log --follow --raw -- [filename]

gives you e.g.:

:100644 100644 01e79c3... 01e79c3... R100       foo     baz

which seems to be what I'm looking for.

rg

```

## Shawn O. Pearce, 2010-02-03 02:53

Subject: Re: How to tell if a file was renamed between two commits
Message-ID: <20100203025347.GB13092@spearce.org>
URL: https://gitlist.dev/e/20100203025347.GB13092%40spearce.org
In-Reply-To: <ron1-08A0F6.18483502022010@news.gmane.org>

```
Ron Garret <ron1@flownet.com> wrote:
> In article <20100203023219.GA13092@spearce.org>,
>  "Shawn O. Pearce" <spearce@spearce.org> wrote:
> > > So... is there an easy way to work around this?  Is there a way to get, 
> > > say, rev-list to tell me when the file it is tracking changed names?
> > 
> > Maybe use the -M flag to git log, or the --follow flag to
> > log/rev-list?
> 
> Nope.  git log --follow will follow through a name change but won't 
> actually say when the name changed happened or what the previous name of 
> the file was.
> 
> And actually playing around with it some more, it appears that git 
> rev-list doesn't actually track file renames, or at least it doesn't do 
> it all the time.  Weird.  I'm going to have to play around with this 
> some more.

Use:

  git log --format=%H -M --name-status --follow -- path 

I just tried it:

$ git log --format=%H -M --name-status --follow gerrit-prettify/src/main/resources/com/google/gerrit/prettify/client/prettify.js
8db22c85c49814b99639b2e6346583e9be4c289f

R100    gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/client/
544546fcd680f82a88df3e9eba7df8acfadf1e46

M       gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/client/
d83ac11a52c1b6d4acae932a8495daf1e9129fdf

R100    gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/public/
44671f5c6929a8f05223dd359182610286ceb98a

R100    src/main/java/com/google/gerrit/public/prettify20090521/prettify.js     
56fc9e3d951b0886c4781a5c8623dbc3da824f30

A       src/main/java/com/google/gerrit/public/prettify20090521/prettify.js


Yay, its been renamed 3 times in its life here.  :-)

-- 
Shawn.

```

## Ron Garret, 2010-02-03 17:48

Subject: Re: How to tell if a file was renamed between two commits
Message-ID: <ron1-F82E20.09482203022010@news.gmane.org>
URL: https://gitlist.dev/e/ron1-F82E20.09482203022010%40news.gmane.org
In-Reply-To: <20100203025347.GB13092@spearce.org>

```
In article <20100203025347.GB13092@spearce.org>,
 "Shawn O. Pearce" <spearce@spearce.org> wrote:

> Ron Garret <ron1@flownet.com> wrote:
> > In article <20100203023219.GA13092@spearce.org>,
> >  "Shawn O. Pearce" <spearce@spearce.org> wrote:
> > > > So... is there an easy way to work around this?  Is there a way to get, 
> > > > say, rev-list to tell me when the file it is tracking changed names?
> > > 
> > > Maybe use the -M flag to git log, or the --follow flag to
> > > log/rev-list?
> > 
> > Nope.  git log --follow will follow through a name change but won't 
> > actually say when the name changed happened or what the previous name of 
> > the file was.
> > 
> > And actually playing around with it some more, it appears that git 
> > rev-list doesn't actually track file renames, or at least it doesn't do 
> > it all the time.  Weird.  I'm going to have to play around with this 
> > some more.
> 
> Use:
> 
>   git log --format=%H -M --name-status --follow -- path 
> 
> I just tried it:
> 
> $ git log --format=%H -M --name-status --follow 
> gerrit-prettify/src/main/resources/com/google/gerrit/prettify/client/prettify.
> js
> 8db22c85c49814b99639b2e6346583e9be4c289f
> 
> R100    
> gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/client/
> 544546fcd680f82a88df3e9eba7df8acfadf1e46
> 
> M       
> gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/client/
> d83ac11a52c1b6d4acae932a8495daf1e9129fdf
> 
> R100    
> gerrit-patch-gwtexpui/src/main/java/com/google/gwtexpui/safehtml/public/
> 44671f5c6929a8f05223dd359182610286ceb98a
> 
> R100    src/main/java/com/google/gerrit/public/prettify20090521/prettify.js   
>   
> 56fc9e3d951b0886c4781a5c8623dbc3da824f30
> 
> A       src/main/java/com/google/gerrit/public/prettify20090521/prettify.js
> 
> 
> Yay, its been renamed 3 times in its life here.  :-)

Cool!  Thanks!

rg

```
