9

I have two files checked into GitHub:

  1. index.html
  2. backup.html

Now I want to rename backup.html into index.html and vice versa. I don't really care about maintaining the change history for each file. How do I do this in Git?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64

3 Answers3

16

This will contain the history:

git mv backup.html backup2.html
git mv index.html backup.html
git mv backup2.html index.html

Without history just rename the file to your liking on your file system.

kevpie
  • 25,206
  • 2
  • 24
  • 28
tom
  • 8,189
  • 12
  • 51
  • 70
  • 2
    Note that `git mv` doesn't do much of anything special. This is more or less a shortcut for Charles Bailey's answer. – Tyler Apr 09 '11 at 00:11
  • 1
    git mv does preserve history of the renamed files. – tom Apr 09 '11 at 10:59
  • 1
    Right but so does Charles Bailey's answer. It's not like svn where it's possible to preserve the history, or not. Git always preserves history when possible. – Tyler Apr 10 '11 at 02:49
  • @MatrixFrog I've [found out the hard way](http://stackoverflow.com/questions/11351091/git-and-binary-files-history) this ain't so [pretty as it should](http://stackoverflow.com/questions/2314652/is-it-possible-to-move-rename-files-in-git-and-maintain-their-history). The history might be preserved somewhere, but you can't find it in any easy way with binaries or in some specific cases. It's not just "when possible" - [following a file history is a highly underlooked feature by git developers (namely Linus)](https://groups.google.com/forum/?fromgroups#!topic/git-version-control/07oWjHiQTtg). – cregox Jul 30 '12 at 19:39
3

You don't have to do anything special as you are not adding any new paths to be tracked.

You can just move the files around and use git add to update their contents.

mv index.html tmpname
mv backup.html index.html
mv tmpname backup.html

then:

git add index.html backup.html

or:

git add -u

or:

git commit -a -m "swap backup.html and index.html"
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

Use git mv and move the first file to a temporary name, then the second to the first and finally the temporary file to the second.

hammar
  • 138,522
  • 17
  • 304
  • 385