用 Git 重命名文件

我想把文件从 README重命名为 README.md。这样做的最佳实践是什么?


我只有一个名为“ change-z-index”的回购协议。

  1. 我像这样打开和登录:

    Ssh-T git@github.com

然后输入密码。

  1. 我尝试这样重命名文件:

    Git mv README README.md Git commit-m“重命名” 基特推动原点控制器

它给我一个错误说 来源不明。

我想我需要先选择我的回购... 它的名字是“变化 z 索引”。我读了很多次说明书,但还是不明白怎么做。

173516 次浏览

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%)

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.

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

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.

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

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

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.

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.