A guide to moving files in Git

A guide to moving files in Git

Git is awesome, but mistakes are inevitable. Sometimes files need to be moved/renamed. But how do you do this on a *nix system?

mv old-file.txt new-file.txt

Also how should you do this with files that are already in git? Just using mv results in one deleted file and one untracked file.

# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    old-file.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    new-file.txt
no changes added to commit (use "git add" and/or "git commit -a")

Why is this bad?

It results in the history of the old file to be lost and the contents of the new file to be committed as though it is new. Any subsequent git blames will be worthless. Instead of getting a useful commit message for why something was done, you just get a “renamed file” commit. This is no use to anyone.

How can I do this in Git?

Fortunately the answer is simple. Prefix your mv command with git.

git mv old-file.txt new-file.txt

Then running git status is a lot more helpful and history is preserved.

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    old-file.txt -> new-file.txt
#

Hurrah!

What if I’ve already moved the file? Is all hope lost?

Git is clever. Running git add new-file.txt and git rm old-file.txt gives the same end result, but it does take twice as long.

Want to discuss a project?

Got a question? Want some more detail? We'd love to hear from you, contact Jason on +44 (0)1923 261166 or jason.treloar@clock.co.uk.

Related

databreach_istock__matej_moderc_thumb800.jpgRead
Article
26 May 2016

How to stop your customers' data being stolen

If we, as an industry, take anything from the data leaks at TalkTalk, British Gas and Morrisons, it should be that we must take every measure we can to secure customer data. Offering customers a more personalised experience means providing an environment where they are confident that the information they provide will be safe. Collecting and storing customer data and finding out more about your users is key to generating leads and gaining customer insight. But in the rush to get campaigns out the door and find affordable ways to create your digital products, ensuring third parties don’t risk your customers’ privacy and your reputations can be overlooked.

Screen Shot 2016-05-13 at 12.44.37.pngRead
Article
16 May 2016

How to build, test, share and publish a javascript Hybrid mobile application using Cordova

Mobile Applications (Apps) are something every developer wants to create, however, not every developer wants to have to learn multiple languages to be able to create an App which works across different types of devices, such as Android and iOS. Learning Objective C (or Swift) and Java is probably enough to put most people off the idea of creating a cross-platform App. However, it’s possible to create one using technologies which most developers are familiar with. Good old HTML, CSS and JavaScript is all you need. Well, that and Apache Cordova, the mobile application development framework that allows you to build Apps for multiple platforms using a single code base.

Blog-post-img-01.jpgRead
Article
26 April 2016

MongoDB Performance on ZFS and Linux

Here at Clock we love ZFS, and have been running it in production on our Linux file servers for several years. It provides us with numerous excellent features. With the recent release of Ubuntu Xenial 16.04 official support for ZFS is now here, and we are keen to integrate it fully into our next generation hosting stack.

Come and work for Clock

Clock is made up of bright, hard-working and talented people and we're always on the look out for more. You can browse the current jobs below or follow us @clock for the latest vacancies.

View Latest
Vacancies