简体   繁体   English

git filter-branch删除trunk文件夹

[英]Git filter-branch to remove trunk folder

I migrated my svn repository to git, however for some reason there are still trunk folders, which should not be the case. 我将svn信息库迁移到git,但是由于某些原因,仍然存在主干文件夹,事实并非如此。 I want to remove these folders without damaging the history line. 我想删除这些文件夹而不损坏历史记录行。

This is the git repository I have at the moment: 这是我目前拥有的git存储库:

- My repository
  -- Project1
     --- trunk
         ---- src
         ---- somefile.txt
         ---- someotherfile.cpp
         ---- andanotherfile.cpp

and this is what I want to have: 这就是我想要的:

- My repository
  -- Project1
     --- src
     --- somefile.txt
     --- someotherfile.cpp
     --- andanotherfile.cpp

I got to know that the command filter-branch is used to for this, however I could not find an example which suits my case, apart from this and this , but then even there I felt a bit confused and not sure if I should try or not. 我知道命令filter-branch用于此目的,但是除了之外,我找不到适合我的情况的示例,但是即使在那儿,我也感到有些困惑,不确定是否应该尝试或不。 Anyone ran into something similar and can confirm that those are the solutions? 任何人都遇到类似问题并且可以确认这些解决方案吗?

Thanks in advance. 提前致谢。

Update: So I got to know that the following command is used most of the time to do the deed: 更新:所以我知道大多数时候都使用以下命令来完成任务:

git filter-branch --index-filter \
    'git ls-files -s | sed "s-/trunk/-/-" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --index-info &&
     mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD

indeed, I checked the sed line and it does tell me that it would remove the trunk and move whatever is inside, to the upper directory. 确实,我检查了sed行,它确实告诉我,它将删除中继并将内部的所有内容移动到上一级目录。 However, the execution gives and error of: 但是,执行给出以下错误:

../myrepository/.git-rewrite/t/../index.new': No such file or directory

I don't know why am I getting this error. 我不知道为什么会收到这个错误。 I looked it up and it seems I need an addition to this command, a number before the HEAD , like 83df928c..HEAD . 83df928c..HEAD了一下,看来我需要在此命令前加一个数字,例如HEAD之前的数字83df928c..HEAD However, I don't know what this is for (I mean, revision number, most likely, but why?) or what is mine. 但是,我不知道这是什么意思(我的意思是,修订号,最有可能,但是为什么?)或我的意思是什么。 Would appreciate help. 希望能有所帮助。 Almost got it! 差不多了!

克隆另一个仓库进行测试

git filter-branch --tree-filter 'cd Project1;cp trunk/* ./; rm -rf trunk'

So, here it goes. 所以,就到这里。

The command which does the magic is: 神奇的命令是:

git filter-branch -f --index-filter \
    'git ls-files -s | sed "s-/trunk/-/-" |
            GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --index-info &&
     mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' af117ec86a30ba6676571cd1f73083c5cfbec0bd..HEAD

note the commit tag before ..HEAD , that should be either second or third earliest commit tag that you should enter. 请注意..HEAD之前的commit标签,它应该是您应该输入的第二或第三最早的commit标签。 If you enter the first, you get an error. 如果输入第一个,则会出现错误。

To find out the commit tag of the earliest commit, simply use: 要找出最早提交的commit标签,只需使用:

git log

and scroll down as much as you can, there you will see the earliest commits. 并尽可能地向下滚动,您将看到最早的提交。 Take the second or third. 拿第二或第三。 It's done. 完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM