我创建了一个包含大量源文件和文件夹的文件夹。

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

我试过这些:

Git添加包含 Git mv common/ include/ 但是它会因为这个错误而失败 致命:坏源,源=myrepo/common,目的地=myrepo/include 我尝试了git mv common/ include/common,但我得到了相同的错误

你知道怎么做到吗?


当前回答

使用Git Tortoise非常简单。

右拖动源到目标(选择源,按住鼠标右键,拖动到目标,释放鼠标)。

将显示一个菜单,其中有Git移动版本的项目和Git移动和重命名版本的项目。

当然,git回购应该在本地克隆

其他回答

 git mv common include

应该工作。

从git mv手册页:

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

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

在移动之前不应该做“git添加”。


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

参见Matthieu Moy (Moy)针对Git 1.9/2.0(2014年第一季度)编写的commit c57f628:

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

另一个重要的注意,我错过了,并立即修复了“坏源”错误:

我只是想分享我的错误,以防有人遇到。:))

当你运行命令时,确保在git控制台中选择了正确的路径:

- git mv Source Destination

如有需要,请使用:

- cd SourceFolder

然后是mv命令。

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

我想我的留言会被删除 但我只是想警告“lurscher”和其他有同样错误的人:做的时候要小心

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

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

如何提问

$ git mv oldFolderName newFolderName

got

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

我所做的

git rm -r oldFolderName

and

git add newFolderName

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

只是想警告,小心使用“Andres Jaan Tack”的建议,如果你不想失去你的git历史。

您可以使用这个脚本。

# git mv a folder and sub folders in windows 

function Move-GitFolder {
    param (
        $target,
        $destination
    )
    
    Get-ChildItem $target -recurse |
    Where-Object { ! $_.PSIsContainer } |
    ForEach-Object { 
        $fullTargetFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $target))
        $fullDestinationFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $destination))
        $fileDestination = $_.Directory.FullName.Replace($fullTargetFolder.TrimEnd('\'), $fullDestinationFolder.TrimEnd('\'))

        New-Item -ItemType Directory -Force -Path $fileDestination | Out-Null

        $filePath = Join-Path $fileDestination $_.Name

        git mv $_.FullName $filePath
        
    }
}

使用

Move-GitFolder <Target folder> <Destination folder>

与其他解决方案相比,此解决方案的优势在于它递归地移动文件夹和文件,甚至在文件夹结构不存在时创建文件夹结构。

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

电源外壳控制台 运行dir Alt-click并拖动文件/文件夹名称列,然后复制 粘贴到notepad++ 执行replace with regex: replace (.*) with git mv "。\ \ \ 1”。\ \ < New_Folder_Here > \” 从notepad++复制所有文本到powershell 回车