将Subversion存储库迁移到Git并永久删除其中的一部分

时间:2015-05-27 08:43:42

标签: git svn

我有一个Subversion v1.8格式的存储库(“apple”)。它包含两个项目(“苹果”和“橙色”)。我需要将它迁移到Git,并完全删除“橙色”项目,这样就没有历史,没有引用,没有证据表明它曾经存在过。

这是我所拥有的,但仍然有对已移除文件的引用(可能还有实际的文件数据,据我所知)...

#!/bin/bash

# installations
# sudo apt-get install subversion git git-svn

# cleanup
rm -rf apple* orange*

# current directory (because svn checkout needs absolute path)
D=`pwd`

# create svn repository and check it out
svnadmin create apple-svn-repo
svn checkout file:///$D/apple-svn-repo/ apple-svn

# commit trunk
cd apple-svn
mkdir trunk
svn add trunk
svn commit -m "trunk"

# commit apple dir
mkdir trunk/apple
echo "apple file" > trunk/apple/applefile.txt
svn add trunk/apple
svn commit -m "apple file"

# commit orange dir
mkdir trunk/orange
echo "orange file" > trunk/orange/orangefile.txt
svn add trunk/orange
svn commit -m "orange file"

# check log
svn update
svn log

# back to original dir (can also cd ..)
cd $D

# clone from SVN to GIT
git svn clone -s file:///$D/apple-svn-repo/ apple-git
cd apple-git
# remove the remote svn config section
git config --remove-section svn-remote.svn
# remove all SVN logs, refs and other data
rm -rf .git/svn .git/{logs/,}refs/remotes/{git-,}svn/
# garbage collect to tidy up
git gc
# still have unstaged changes... need to commit locally and reset head
git add .
git reset --hard
# remove "orange"
git filter-branch --index-filter "git rm -r --cached --ignore-unmatch orange" --prune-empty
# mark backup as expired
git reflog expire --expire=now --all
# remove 'git-svn-id: svn://' messages from untitled commits
git filter-branch -f --msg-filter 'sed "s/git-svn.*$//g"' -- --all
# garbage collect to remove expired backup and unused remotes
git gc --aggressive --prune=now

# moment of truth
git log
git rev-list --all --objects

产生输出:

8f746d2cf2f6dcf77135fc4ae6e7b1263a6a070a
812010c9a2ed14892420862718408c33ab948a3f
406ac30ad5770f1c3eec5b1e9bb2b7321a2dc0cb
567c85d300426a6f02e1a38a87980a28cbba27fa
69705a92944e1cc1ffafee747df317ef4d0a94f7
55f51f76bbc51ca206f6b62d8d51797455ff566c apple
358f29228e6394ad06ccbf46eaaa3f5104514f4c apple/applefile.txt
d4efd18a7e55206f8f8531cbd4b589ed0e47d5c5 orange
ba2d1295e8d99acf9f64f072b951dc1ebf516cd2 orange/orangefile.txt
a167088b4e4957ff6e78c227e8092139408c81f8

那么如何完全删除“orange”及其文件?

编辑:有人发布了答案,并在我试用时删除了他们的帖子。所以我不知道你是谁,但谢谢你。实际工作的解决方案是:

#!/bin/bash

# installations
# sudo apt-get install subversion git git-svn

# cleanup
rm -rf apple* orange*

# current directory (because svn checkout needs absolute path)
D=`pwd`

# create svn repository and check it out
svnadmin create apple-svn-repo
svn checkout file:///$D/apple-svn-repo/ apple-svn

# commit trunk
cd apple-svn
mkdir trunk
svn add trunk
svn commit -m "trunk"

# commit apple dir
mkdir trunk/apple
echo "apple file" > trunk/apple/applefile.txt
svn add trunk/apple
svn commit -m "apple file"

# commit orange dir
mkdir trunk/orange
echo "orange file" > trunk/orange/orangefile.txt
svn add trunk/orange
svn commit -m "orange file"

# check log
svn update
svn log

# back to original dir (can also cd ..)
cd $D

# clone from SVN to GIT
git svn clone -s file:///$D/apple-svn-repo/ apple-git
cd apple-git
# remove "orange"
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch orange/* orange" --prune-empty -- --all 
rm -rf .git/refs/original/refs/
rm -rf .git/logs/refs/
git reset --hard
git reflog expire --expire=now --all
git gc --aggressive --prune=now

# moment of truth
git log
git rev-list --all --objects

1 个答案:

答案 0 :(得分:2)

这是因为由于svn存储库上的 remote / trunk 引用,您仍然可以参考橙色文件。清理后,您可以执行克隆以确保您不再有待处理的引用

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch orange/* orange" --prune-empty -- --all 
rm -rf .git/refs/original/refs/
rm -rf .git/logs/refs/
git reset --hard
git reflog expire --expire=now --all
git gc --aggressive --prune=now
cd ..
git clone apple-git apple-git.clean
cd apple-git.clean
git remote rm origin
git rev-list --all --objects

你应该获得

f3b260205c3b6a1ea9e64bab2554bd232f4c7aca
65a5013381de325056828079834dcf9450318c9e 
31348b45188be3660383b6dc4e80971c1ef509d3 apple
358f29228e6394ad06ccbf46eaaa3f5104514f4c apple/applefile.txt