121

I would like to rename file from README to README.md. What is the best practice to do that?


I have only one repo called "change-z-index".

  1. I open and login like that:

    ssh -T git@github.com

And I enter my passphrase.

  1. I try to rename the file like that:

    git mv README README.md git commit -m "renamed" git push origin master

It gives me an error saying bad source.

I think I need to select my repo first... its name is "change-z-index". I have read manual many times, but still can't understand how to do it.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Badr Hari
  • 8,114
  • 18
  • 67
  • 100
  • Is `README` definitely under git's control already? The error you are getting suggests that either `README` does not exist or that it is not in git's index. – CB Bailey Jul 31 '11 at 11:31
  • @Charles Bailey I can see readme file in my repo when I visit my Github's webpage – Badr Hari Jul 31 '11 at 11:32
  • I'm not familiar with the details of github, but where are you trying to rename the file, on your local clone or on the github server itself? – CB Bailey Jul 31 '11 at 11:43
  • @Charles Bailey on the github server itself – Badr Hari Jul 31 '11 at 11:54
  • @BadrHari: OK, I wasn't aware that the github servers hosted a non-bare repository with a working tree. What happens when you run `git status` on the server? – CB Bailey Jul 31 '11 at 11:58
  • Actually, I've just read some github documentation and I'm almost sure that github doesn't provide shell access or a non-bare working copy. I'm not sure how you've managed to run any commands on the remote github server but I am almost certain that you should be running `git mv` and `git commit` on your local clone of the github hosted repository and pushing the rename commit to the github server. – CB Bailey Jul 31 '11 at 12:01

9 Answers9

174

As far as I can tell, GitHub does not provide shell access, so I'm curious about how you managed to log in in the first place.

$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not provide
shell access.

You have to clone your repository locally, make the change there, and push the change to GitHub.

$ git clone git@github.com:username/reponame.git
$ cd reponame
$ git mv README README.md
$ git commit -m "renamed"
$ git push origin master
hammar
  • 138,522
  • 17
  • 304
  • 385
  • 25
    Just adding for noobs like myself that _using `git mv` automatically renames the file on your computer_. I tried saving the file as a different name first before using `git mv` and was met with the error `fatal: destination exists` because of this silly mistake. – MichaelChirico Jul 27 '15 at 21:41
56

You can rename a file using git's mv command:

$ git mv file_from file_to

Example:

$ git mv helo.txt hello.txt

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

$ git commit -m "renamed helo.txt to hello.txt"
[master 14c8c4f] renamed helo.txt to hello.txt
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename helo.txt => hello.txt (100%)
user664833
  • 18,397
  • 19
  • 91
  • 140
jaredwilli
  • 11,762
  • 6
  • 42
  • 41
  • 3
    It gives me an error: bad source, source = README, destination = README.md – Badr Hari Jul 31 '11 at 11:24
  • Are you trying to rename it on the github.com repo, or your local repo? – jaredwilli Jul 31 '11 at 11:59
  • If you are trying to rename it on github, then you need to remove it from the local repo, $ rm file_name. do a commit, then re-add that file to the repo under the name you want to change it to and commit that. The source and the destination files aren't matching up so it's erroring. I think that's what it is anyways... – jaredwilli Jul 31 '11 at 12:01
  • If there's an error, try also using force `git mv -f helo.txt hello.txt` – cho_uc Feb 17 '22 at 16:37
45

Note that, from March 15th, 2013, you can move or rename a file directly from GitHub:

(you don't even need to clone that repo, git mv xx and git push back to GitHub!)

renaming

You can also move files to entirely new locations using just the filename field.
To navigate down into a folder, just type the name of the folder you want to move the file into followed by /.
The folder can be one that’s already part of your repository, or it can even be a brand-new folder that doesn’t exist yet!

moving

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7

I had a similar problem going through a tutorial.

# git mv README README.markdown

fatal: bad source, source=README, destination=README.markdown

I included the filetype in the source file:

# git mv README.rdoc README.markdown

and it worked perfectly. Don't forget to commit the changes with i.e.:

# git commit -a -m "Improved the README"

Sometimes it is simple little things like that, that piss us off. LOL

josliber
  • 43,891
  • 12
  • 98
  • 133
Chris Howard
  • 111
  • 1
  • 4
3

Do a git status to find out if your file is actually in your index or the commit.

It is easy as a beginner to misunderstand the index/staging area.

I view it as a 'progress pinboard'. I therefore have to add the file to the pinboard before I can commit it (i.e. a copy of the complete pinboard), I have to update the pinboard when required, and I also have to deliberately remove files from it when I've finished with them - simply creating, editing or deleting a file doesn't affect the pinboard. It's like 'storyboarding'.

Edit: As others noted, You should do the edits locally and then push the updated repo, rather than attempt to edit directly on github.

Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
3

This might be relevant for some geeks; if you want to rename a file on GitHub (without the command line), all you can do is open your repository and press period ., this will open a web version of VScode, where you can make changes and commit.

first
  • 616
  • 1
  • 7
  • 13
2

You've got "Bad Status" its because the target file cannot find or not present, like for example you call README file which is not in the current directory.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Ryan S
  • 6,076
  • 1
  • 20
  • 14
  • Indeed this was my problem. I had forgotten to change the current directory to where the file was. – TheIT Aug 12 '14 at 03:30
0

What I learned after fixing this is that, Your mv command should fulfil two conditions, after providing the correct file path:

  1. Give the full file name, including the file extension.
  2. There should NOT be any pending changes to some other files.
Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43
0

One of the simplest way to do it is remove the file from repository first i.e remove the file from the folder structure, paste it somewhere else for later reference and then push those changes. Next you need to push files with updated name so go back to the folder location where files were pasted, update all with correct names and references, paste back at original file location and then push those changes. Works fine

Monalisa Das
  • 667
  • 5
  • 3