Git 将文件移动到文件夹中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9237083/
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-19 06:27:42  来源:igfitidea点击:

Git Moving Files into Folders

gitgithub

提问by james_dean

I am quite new to git and version control in general, and have question about moving files around. I have a master repo on GitHub, with 6 source files.

一般来说,我对 git 和版本控制很陌生,并且对移动文件有疑问。我在 GitHub 上有一个主存储库,有 6 个源文件。

I've done a lot of work on the project and now my local branch contains two folders with the sources in those.

我在这个项目上做了很多工作,现在我的本地分支包含两个文件夹,其中包含源代码。

The directory structure used to be like:

以前的目录结构是这样的:

Master:

掌握:

  • File 1
  • File 2
  • File 3
  • File 4
  • File 5
  • File 6
  • 文件 1
  • 档案 2
  • 文件 3
  • 文件 4
  • 文件5
  • 文件 6

Where as my local branch now looks like this:

我的本地分支现在看起来像这样:

New Folder 1:

新建文件夹 1:

  • New File 1
  • New File 2
  • New File 3
  • 新文件 1
  • 新文件 2
  • 新文件 3

New Folder 2:

新建文件夹 2:

  • File 1
  • File 2
  • File 3
  • File 4
  • File 5
  • File 6
  • 文件 1
  • 档案 2
  • 文件 3
  • 文件 4
  • 文件5
  • 文件 6

How can I move my local structure to the master branch without losing my commit history on the old files?

如何将我的本地结构移动到 master 分支而不会丢失旧文件的提交历史?

采纳答案by Amber

Just commit. Git blame etc will generally do a pretty good job of automatically detecting moves.

只要承诺。Gitblame等通常会在自动检测动作方面做得很好。

回答by Daniel Pittman

git doesn't actually track "moves" of files, it infers it on demand from similar content. So, just make the move and add / remove files as appropriate. (You will make life easier for the tools in future if you avoid making changes to the content as well as moving them in a single commit.)

git 实际上并不跟踪文件的“移动”,它会根据需要从类似的内容中推断出来。因此,只需进行移动并根据需要添加/删除文件。(如果您避免对内容进行更改以及在一次提交中移动它们,那么您将来会更轻松地使用这些工具。)

Then, to see the log accounting for moves, use -M, -C, or their variants to git log. Similar flags apply to other tools, and you should read the help to understand the detail of what they do.

然后,看到了移动,使用日志记账-M-C或它们的变体git log。类似的标志适用于其他工具,您应该阅读帮助以了解它们的详细信息。

If you use git mvon a file, it just does the git rmand git addfor you.

如果您git mv在文件上使用,它只会为您执行git rmgit add

回答by Chris Shain

The other posters are correct, however if you want to confirm that git is going to see these as moves (and not a combination of delete/create), run a git status. You should see:

其他海报是正确的,但是如果您想确认 git 会将这些视为移动(而不是删除/创建的组合),请运行git status. 你应该看到:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    oldfile.txt -> newFolder/newfile.txt

回答by Borjovsky

In github, if you don't have too many files to move, an interesting alternative is to Edit the file, and change it's name to:

在 github 中,如果您没有太多文件要移动,一个有趣的替代方法是编辑文件,并将其名称更改为:

folder_to_move_into/file_name

When you commit, the file is moved to the folder. If the folder doesn't exist, it will be created.

提交时,文件将移动到文件夹中。如果文件夹不存在,它将被创建。