Skip to main content

Renaming Files in Git

Renaming Files

I recently had to rename a lot of files in brf-mode for submission into MELPA. These files have 10+ years of version control history I wanted to keep. In the process I realised retaining all that history in Git isn't as simple as I thought 😀

I thought it was as simple as using git mv rather than filesystem mv and Git would know everything had been renamed. It turns out I was wrong and I should have known that from my knowledge of how Git works 😖

In reality, Git works by storing snapshots rather than file or directory metadata and git mv is just a convenience shortcut for typing:

$ mv <old name> <new name>
$ git rm <old name>
$ git add <new name>

That's all there is to it!

Now the "magic" happens when you git log or git blame a file and Git works out the file was renamed by diffing the contents.  If files have the same contents (within a certain threshold %) it thinks a rename happened. See here in the Git SCM Book for the gory details.

A consequence of this is if you rename files and then make a whole load of changes to the contents of those files, Git will not recognise this as a rename.

Therefore it is important to make the rename in one commit and any changes to the contents in another commit.

Of course Git being Git, there is a further wrinkle. While git blame will follow renames and give you a consistent annotation by default, git log does not.  git log will only show history to the last rename unless you supply the --follow argument. You really couldn't make this stuff up...

You can work around this by setting log.follow in .gitconfig.
However when I'm doing command line Git I like to have full control of what I'm doing, so instead of doing that I just tell Emacs VC to follow by default:

(setq vc-git-print-log-follow t)

Moving Files

Unfortunately moving files between directories creates further problems - history is only partially retained. git log --follow allows you to see the history of the moved files, but git diffbisect etc can't find the revisions. You can still diff commits from the top-level log, just not for an individual file. Hopefully future Git revisions will fix this - until then the only way around it I know about is to destructively edit the commit history with filter-branch etc. That will have to be the subject of a future post 😀

Comments

Popular posts from this blog

Merging Git Repositories

No project of significant size that I've ever seen has retained its initial structure. Restructuring projects is a fact of life, but unfortunately Git doesn't make it easy. Fundamentally this stems from the way Git works, treating changes as a succession of snapshots and not storing any other metadata. Of course this is part of what makes Git fast and efficient, but at the expense of making some common operations more difficult for users. Git really is a perfect 21st century illustration of the classic  "Worse Is Better"  paradigm of successful software 😀 Previously I discussed how to split a Git project apart into separate repositories . Now I'm going to discuss how to do the opposite and merge separate repositories into one. On the face of it, this would seem a simpler task as Git has powerful support for merging... Let's take the opposite example to my splitting apart article - say you have a main Git repo (ProjA) and a second repo (ProjB) in...

My Work in new Top Trumps Birds of Prey Pack

The new Top Trumps "Birds of Prey" pack has my picture illustrating the Secretary Bird card 😀 Here's the original picture: From Flickr

Indian Summer Continues

The unseasonably warm weather continues with wildlife aplenty. I managed to get a little bit closer to one of the SparrowHawks that lives around here: Soaring SparrowHawk Nikon D300s, 70-200 f/2.8G VR II and TC-1.4E II converter. Still not close enough to get a decent shot though! A Grey Heron also flew over while I was watching out for the SparrowHawk. St Albans must be one of the best places in Britain to see Herons, as they are a common sight in the fields. Never mind the large breeding colony in Verulamium Park. Heron Flying Over Local Fields Also saw some more mundane wildlife, still enjoying the last vestiges of Summer: Bee Supping Nectar from a Wildflower Young Rabbit at Sunset