使用 Git 重命名文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6889119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:27:32  来源:igfitidea点击:

Rename file with Git

gitgithub

提问by Badr Hari

I'm new to Git/Github and I needs some help. I would like to rename file from README to README.md. I have only one repo called "change-z-index".

我是 Git/Github 的新手,我需要一些帮助。我想将文件从 README 重命名为 README.md。我只有一个名为“change-z-index”的存储库。

1) I open and login like that:

1)我像这样打开并登录:

ssh -T [email protected]

And I enter my passphrase.

然后我输入我的密码。

2) I try to rename the file like that:

2)我尝试像这样重命名文件:

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... it's name is "change-z-index". I have read manual many times, but still can't understand how to do it.

我想我需要先选择我的仓库......它的名字是“change-z-index”。我已经阅读了很多次手册,但仍然无法理解如何去做。

回答by hammar

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.

据我所知,GitHub 不提供 shell 访问,所以我很好奇你最初是如何设法登录的。

$ ssh -T [email protected]
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.

您必须在本地克隆您的存储库,在那里进行更改,然后将更改推送到 GitHub。

$ git clone [email protected]:username/reponame.git
$ cd reponame
$ git mv README README.md
$ git commit -m "renamed"
$ git push origin master

回答by VonC

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

请注意,从 2013 年 3 月 15 日起,您可以直接从 GitHub 移动或重命名文件

(you don't even need to clone that repo, git mv xxand git pushback to GitHub!)

(你甚至不需要克隆那个 repo,git mv xx然后git push回到 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

移动

回答by jaredwilli

You can rename a file using git's mvcommand:

您可以使用git'smv命令重命名文件:

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

回答by Chris Howard

I had a similar problem going through a tutorial.

我在学习教程时遇到了类似的问题。

# git mv README README.markdown

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

致命:源错误,源=README,目标=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.:

它工作得很好。不要忘记使用 ie 提交更改:

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

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

有时就是这样简单的小事,让我们生气。哈哈

回答by Philip Oakley

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

做一个git status看看你的文件是否真的在你的索引或提交中。

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

初学者很容易误解索引/暂存区。

I view it as a 'progress pinboard'. I therefore have to addthe file to the pinboard before I can commitit (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'.

我认为它是一个“进度板”。因此,我必须add在我可以之前将文件发送到插板commit(即完整插板的副本),我必须在需要时更新插板,并且我还必须在完成后故意从中删除文件 -简单地创建、编辑或删除文件不会影响 pinboard。这就像“故事板”。

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

编辑:正如其他人所指出的,您应该在本地进行编辑,然后推送更新的 repo,而不是尝试直接在 github 上进行编辑。

回答by Ryan S

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.

您的“状态不佳”是因为目标文件找不到或不存在,例如您调用不在当前目录中的 README 文件。