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

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

我试过这些:

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

你知道怎么做到吗?


当前回答

使用Git Tortoise非常简单。

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

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

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

其他回答

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

$(ls | grep -v 'subDir');做git mv $file subDir;完成;

移动文件并使用git stage

你可以使用你最喜欢的工具(命令行,图形文件资源管理器)移动你的文件,git会发现你做了移动,而不是删除和创建,一旦你执行了你的操作,正如@Andres所建议的,但如果你做了太多的修改,Erik Kaplun指出,显式git mv可以帮助。

在某些应用程序中不使用Stage

一些GUI git工具(如GitHub Desktop)不提供对stage的直接访问:您可以选择和取消选择修改,但它不会修改底层git stage。你需要使用更强大的git工具,如命令行工具或正宗的git GUI来进行更改,然后GitHub Desktop也将显示操作为重命名。

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

假设我有文件

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

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

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

我所做的是:

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 --update with directory change trick): git add subdir (normally this would add even untracked files - this would require creating .gitignore file) git status shows now only moved files moved rest of files from tmpdir to subdir: mv tmpdir/* subdir git status looks like we executed git mv :)

您可以使用这个脚本。

# 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>

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

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

git mv oldFolderName newFoldername

Git错误失败

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

是否有任何未添加的文件,所以我才知道。