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

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

我试过这些:

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

你知道怎么做到吗?


当前回答

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

假设我有文件

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中遇到了类似的问题,我想将一个文件夹的内容移动到一个现有的文件夹中,并以这个“简单”脚本结束:

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

解释

git ls-files:找到所有签入git的文件(在公共文件夹中) newdir = " . .美元/ include / $(目录名)”;mkdir -p $newdir;:在include文件夹中创建一个新文件夹,目录结构与普通目录相同 git mv $f $newdir/$(basename "$f"):将文件移动到新创建的文件夹中

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

这种方法的优点是它只触及已经签入git的文件。通过简单地使用git mv来移动整个文件夹,而文件夹中包含了未分阶段的更改,git将不知道该做什么。

在移动文件之后,您可能想要清理存储库以删除任何剩余的未分阶段更改-只要记住先运行!

git clean -fd -n

我使用这个命令解决了这个问题 Mv <parent_folder/folder_to_move> <new_folder>而在<new_folder>的工作目录。 这个命令的作用是将<folder_to_move>重命名为<new_folder>名称。之后,您可以重命名目标文件夹(folder_to_move)使用mv <new_folder>(以前<folder_to_move>) <new_name_of_folder>

Mv <new_folder> <new_name_of_folder>

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

假设我有文件

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历史):

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