2010-10-10 109 views
133

我创建了一个文件夹common,其中包含一堆源文件和文件夹。git命令将文件夹移入另一个文件夹

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

我尝试这些:

  1. git add include

  2. git mv common/ include/

    但它失败与此错误

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

  3. 我试着混帐MV普通/包括/常见,但我得到了同样的错误

任何想法如何实现这一目标?

回答

148

其中一个最好的东西约Git是,你不需要跟踪文件重命名明确。 Git会通过比较文件的内容来找出它。

所以,你的情况,不用这么辛苦:

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

运行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 
# 
+24

这对我不起作用(使用Windows 7,1.7.6.msysgit.0)。 Git认为旧文件已被删除并添加了新文件。 – Bart 2011-11-09 13:18:47

+0

嗯,也许git使用一些外部实用程序来确定文件的相同性在Windows上不起作用?这是git实现的核心部分。 – 2011-11-09 15:01:00

+41

使用'git mv'命令是更好的方法。 – Oliver 2012-07-06 14:13:31

109
git mv common include 

应该工作。

git mv man page

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.

否 “git add” 应移动之前完成


注:“git mv A B/”,当B不为目录中,应该报错了,但事实并非如此。

Git 1.9/2.0(2014年第一季度)查看commit c57f628Matthieu Moy (moy)

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 mv file no-such-dir/ 
fatal: renaming 'file' failed: Not a directory 
+1

工作完美 - 使用'git mv'看起来好像更好的方法! – 2015-03-17 17:18:36

0

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

让我们说我有文件

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

,我想只移动跟踪文件到子文件夹subdir,因此我们的目标是:

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

我做了什么是:

  • 我创建了新文件夹并移动了我感兴趣的所有文件:mkdir tmpdir && mv a b tmpdir
  • 签出旧文件git checkout a b
  • 创建新的目录和移动干净的文件夹(不含未跟踪文件)到新的子目录:mkdir subdir && mv a b subdir
  • 添加从子目录中的所有文件(这样的Git可以只添加先前跟踪的文件 - 这是的somekind的git add --update与目录更改):git add subdir(通常这会增加甚至未跟踪文件 - 这将需要创建.gitignore文件)
  • git status节目现在只有移动的文件
  • 移动的文件的休息从TMPDIR到子目录:mv tmpdir/* subdir
  • git status看起来我们执行git mv :)
2

我很抱歉,我没有足够的信誉评论的“安德烈斯家安粘性”的“答案”。

我想我messege将被删除(( 但我只是想告诫“lurscher”和其他人谁得到了同样的错误:要小心做

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

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

我tryed

$ git mv oldFolderName newFolderName 

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

我做

git rm -r oldFolderName 

git add newFolderName 

,我没有看到老蠢货历史在我的项目。至少我的项目不会丢失。现在,我有我的newFolderName项目,但没有历史(

只是想提醒,使用“安德烈斯家安粘性”建议要小心,如果你不想失去你的git hsitory

+0

确保已添加所有更改。如果没有添加更改,Git不会说“不良来源”,所以我只是发现了。 – 2015-03-16 13:49:05

11

命令:

$ git mv oldFolderName newFolderName 

它通常工作正常。

错误“坏源...“通常表明最后提交后出现了在源目录中的一些重命名,因此git mv找不到预期的文件

解决方法很简单 - 将git mv之前刚刚提交

3

另一种方法是将所有文件。一个目录的子目录(保持git的历史):

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

6

确保已将所有更改的临时区域运行

git mv oldFolderName newFoldername 

混帐失败,错误

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

,如果有任何unadded文件,所以我刚刚发现。

+0

“如果有任何未添加的文件,所以我只是发现了。” - 谢谢! – 2017-09-22 03:08:52

3

我有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:查找所有文件(在common文件夹中)签入git
  • newdir="../include/$(dirname $f)"; mkdir -p $newdir;:创建一个新的文件夹中的include文件夹内,使用相同的目录结构common
  • git mv $f $newdir/$(basename "$f"):将文件移动到新创建的文件夹

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

这种方法的好处是它只有接触已经检入到git的文件。通过简单地使用git mv移动整个文件夹,并且该文件夹包含未分离的更改,git不知道该怎么做。

移动文件后,你可能想clean the repository删除任何剩余的未分级的变化 - 只记得晾干运行第一!

git clean -fd -n 
相关问题