git git命令将文件夹移动到另一个文件夹中

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

git command to move a folder inside another

git

提问by lurscher

I have created a folder commonwith a bunch of source files and folders.

我创建了一个common包含一堆源文件和文件夹的文件夹。

Now I want to move the commonfolder into the includefolder so it looks like include/common

现在我想将common文件夹移动到文件include夹中,所以它看起来像include/common

I tried these:

我试过这些:

  1. git add include

  2. git mv common/ include/

    but it fails with this error

    fatal: bad source, source=myrepo/common, destination=myrepo/include

  3. I tried git mv common/ include/commonbut I get the same error

  1. git add include

  2. git mv common/ include/

    但它因此错误而失败

    致命:源错误,源=myrepo/common,目标=myrepo/include

  3. 我试过了,git mv common/ include/common但我得到了同样的错误

Any idea how to achieve this?

知道如何实现这一目标吗?

采纳答案by Andres Jaan Tack

One of the nicest things about git is that you don't need to track file renames explicitly. Git will figure it out by comparing the contents of the files.

关于 git 最好的事情之一是您不需要明确跟踪文件重命名。Git 会通过比较文件的内容来弄清楚。

So, in your case, don't work so hard:

所以,在你的情况下,不要那么努力:

$ mkdir include
$ mv common include
$ git rm -r common
$ git add include/common

Running git statusshould show you something like this:

运行git status应该会显示如下内容:

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

回答by VonC

 git mv common include

should work.

应该管用。

From the git mvman page:

git mv手册页

git mv [-f] [-n] [-k] <source> ... <destination directory>

In the second form, the last argument has to be an existing directory; the given sources will be moved into this directory.
The index is updated after successful completion, but the change must still be committed.

在第二种形式中,最后一个参数必须是现有目录;给定的源将被移动到这个目录中
成功完成后会更新索引,但仍必须提交更改。

No "git add" should be done beforethe move.

移动git add应该做“ ” 。



Note: "git mv A B/", when Bdoes not exist as a directory, should error out, but it didn't.

注意:“ git mv A B/”,当B目录不存在时,应该出错,但没有。

See commit c57f628by Matthieu Moy (moy)for Git 1.9/2.0(Q1 2014):

提交c57f628马修莫伊(moyGIT中1.9 / 2.0(Q1 2014):

Git used to trim the trailing slash, and make the command equivalent to 'git mv file no-such-dir', which created the file no-such-dir(while the trailing slash explicitly stated that it could only be a directory).

This patch skips the trailing slash removal for the destination path.
The path with its trailing slash is passed to rename(2), which errors out with the appropriate message:

Git 用于修剪尾部斜杠,并使命令等效于git mv file no-such-dir创建文件的' ' no-such-dir(而尾部斜杠明确表示它只能是一个目录)。

此补丁跳过目标路径的尾部斜杠删除。
带有尾部斜杠的路径被传递给 rename(2),它会以适当的消息出错:

$ git mv file no-such-dir/
fatal: renaming 'file' failed: Not a directory

回答by Igor Zvorygin

Command:

命令:

$ git mv oldFolderName newFolderName

It usually works fine.

它通常工作正常。

Error "bad source ..." typically indicates that after last commit there were some renames in the source directory and hence git mvcannot find the expected file.

错误“bad source ...”通常表示在上次提交后,源目录中有一些重命名,因此git mv找不到预期的文件。

The solution is simple - just commit before applying git mv.

解决方案很简单——只需在应用之前提交git mv

回答by Kevin Pluck

Make sure you have added all your changes to the staging area before running

确保在运行之前已将所有更改添加到暂存区

git mv oldFolderName newFoldername

git fails with error

git 失败并出现错误

fatal: bad source, source=oldFolderName/somepath/somefile.foo, destination=newFolderName/somepath/somefile.foo

if there are any unadded files, so I just found out.

如果有任何未添加的文件,所以我才发现。

回答by loket

I had a similar problem with git mvwhere I wanted to move the contents of one folder into an existing folder, and ended up with this "simple" script:

我有一个类似的问题git mv,我想将一个文件夹的内容移动到一个现有的文件夹中,最后得到了这个“简单”的脚本:

pushd common; for f in $(git ls-files); do newdir="../include/$(dirname $f)"; mkdir -p $newdir; git mv $f $newdir/$(basename "$f"); done; popd

Explanation

解释

  • git ls-files: Find all files (in the commonfolder) checked into git
  • newdir="../include/$(dirname $f)"; mkdir -p $newdir;: Make a new folder inside the includefolder, with the same directory structure as common
  • git mv $f $newdir/$(basename "$f"): Move the file into the newly created folder
  • git ls-files:查找所有common签入git的文件(在文件夹中)
  • newdir="../include/$(dirname $f)"; mkdir -p $newdir;: 在文件夹内新建一个文件include夹,目录结构与common
  • git mv $f $newdir/$(basename "$f"): 将文件移动到新创建的文件夹中

The reason for doing this is that git seems to have problems moving files into existing folders, and it will also fail if you try to move a file into a non-existing folder (hence mkdir -p).

这样做的原因是 git 似乎在将文件移动到现有文件夹中时遇到问题,如果您尝试将文件移动到不存在的文件夹中(因此mkdir -p),它也会失败。

The nice thing about this approach is that it onlytouches files that are already checked in to git. By simply using git mvto move an entire folder, and the folder contains unstaged changes, git will not know what to do.

这种方法的好处是它只涉及已经签入到 git 的文件。通过简单地使用git mv移动整个文件夹,并且该文件夹包含未暂存的更改,git 将不知道该怎么做。

After moving the files you might want to clean the repositoryto remove any remaining unstaged changes - just remember to dry-run first!

移动文件后,您可能想要清理存储库以删除任何剩余的未暂存更改 - 请记住先进行试运行!

git clean -fd -n

回答by Michael Smolenski

Another way to move all files in a directory to a sub directory (keeps git history):

将目录中的所有文件移动到子目录的另一种方法(保留 git 历史记录):

$ for file in $(ls | grep -v 'subDir'); do git mv $file subDir; done;

$ for file in $(ls | grep -v 'subDir'); do git mv $file subDir; done;

回答by Ivan F.

I'm sorry I don't have enough reputation to comment the "answer" of "Andres Jaan Tack".

很抱歉,我没有足够的声誉来评论“Andres Jaan Tack”的“答案”。

I think my messege will be deleted (( But I just want to warn "lurscher" and others who got the same error: be carefull doing

我认为我的消息将被删除((但我只想警告“lurscher”和其他遇到相同错误的人:小心做

$ mkdir include
$ mv common include
$ git rm -r common
$ git add include/common

It may cause you will not see the git history of your project in new folder.

这可能会导致您不会在新文件夹中看到项目的 git 历史记录。

I tryed

我试过了

$ git mv oldFolderName newFolderName

got

得到了

fatal: bad source, source=oldFolderName/somepath/__init__.py, dest
ination=ESWProj_Base/ESWProj_DebugControlsMenu/somepath/__init__.py

I did

我做了

git rm -r oldFolderName

and

git add newFolderName

and I don't see old git history in my project. At least my project is not lost. Now I have my project in newFolderName, but without the history (

我在我的项目中没有看到旧的 git 历史。至少我的项目没有丢失。现在我在 newFolderName 中有我的项目,但没有历史记录(

Just want to warn, be carefull using advice of "Andres Jaan Tack", if you dont want to lose your git hsitory.

只是想警告一下,如果你不想丢失你的 git hsiory,请小心使用“Andres Jaan Tack”的建议。

回答by test30

I had similar problem, but in folder which I wanted to move I had files which I was not tracking.

我有类似的问题,但在我想移动的文件夹中,我有我没有跟踪的文件。

let's say I had files

假设我有文件

a/file1
a/untracked1
b/file2
b/untracked2

And I wanted to move only tracked files to subfolder subdir, so the goal was:

我只想将跟踪的文件移动到子文件夹subdir,所以目标是:

subdir/a/file1
subdir/a/untracked1
subdir/b/file2
subdir/b/untracked2

what I had done was:

我所做的是:

  • I created new folder and moved all files that I was interested in moving: mkdir tmpdir && mv a b tmpdir
  • checked out old files git checkout a b
  • created new dir and moved clean folders (without untracked files) to new subdir: mkdir subdir && mv a b subdir
  • added all files from subdir (so Git could add only tracked previously files - it was somekind of git add --updatewith directory change trick): git add subdir(normally this would add even untracked files - this would require creating .gitignorefile)
  • git statusshows now only moved files
  • moved rest of files from tmpdir to subdir: mv tmpdir/* subdir
  • git statuslooks like we executed git mv:)
  • 我创建了新文件夹并移动了我有兴趣移动的所有文件: mkdir tmpdir && mv a b tmpdir
  • 检出旧文件 git checkout a b
  • 创建新目录并将干净的文件夹(没有未跟踪的文件)移动到新的子目录: mkdir subdir && mv a b subdir
  • 添加来自 subdir 的所有文件(因此 Git 只能添加以前跟踪的文件 - 这是某种git add --update目录更改技巧):(git add subdir通常这会添加甚至未跟踪的文件 - 这将需要创建.gitignore文件)
  • git status现在只显示移动的文件
  • 将其余文件从 tmpdir 移动到 subdir: mv tmpdir/* subdir
  • git status看起来我们执行了git mv:)

回答by FalcoGer

I solved this on windows by doing this:

我通过这样做在 Windows 上解决了这个问题:

  • Open Power shell console
  • run dir
  • Alt-click and drag over the file/folder name column, then copy
  • Paste to notepad++
  • run replace with regex: replace (.*)with git mv ".\\\1" ".\\<New_Folder_Here>\"
  • copy all text from notepad++ into powershell
  • hit enter
  • 打开 Power shell 控制台
  • 运行目录
  • Alt 键单击并拖动文件/文件夹名称列,然后复制
  • 粘贴到记事本++
  • 运行替换为正则表达式:替换(.*)git mv ".\\\1" ".\\<New_Folder_Here>\"
  • 将记事本++中的所有文本复制到powershell中
  • 按回车