如何提取一个git子目录并从中创建一个子模块? [英] How to extract a git subdirectory and make a submodule out of it?
问题描述
几个月前,我开始了一个项目,并将所有内容存储在主目录中. 在我的主目录"Project"中,有几个包含不同内容的子目录: 项目/论文包含用LaTeX编写的文档 项目/源代码/RailsApp包含我的Rails应用程序.
I started a project some months ago and stored everything within a main directory. In my main directory "Project" there are several subdirectories containing different things: Project/paper contains a document written in LaTeX Project/sourcecode/RailsApp contains my rails app.
项目"已GIT化,并且"paper"和"RailsApp"目录中都有很多提交.现在,因为我想在我的"RailsApp"中使用cruisecontrol.rb,所以我想知道是否有一种方法可以在不丢失历史记录的情况下从"RailsApp"中制作一个子模块.
"Project" is GITified and there have been a lot of commits in both "paper" and "RailsApp" directory. Now, as I'd like to use cruisecontrol.rb for my "RailsApp" I wonder if there is a way to make a submodule out of "RailsApp" without losing the history.
推荐答案
如今,有比手动使用git filter-branch简便得多的方法:
Nowadays there's a much easier way to do it than manually using git filter-branch: git subtree
注意 git-subtree
现在是git
的一部分(如果您安装contrib),因此您可能已经安装了它.您可以通过执行git subtree
进行检查.
NOTE git-subtree
is now part of git
(if you install contrib) as of 1.7.11, so you might already have it installed. You may check by executing git subtree
.
要从源代码安装git-subtree(对于git的较早版本):
To install git-subtree from source (for older versions of git):
git clone https://github.com/apenwarr/git-subtree.git
cd git-subtree
sudo rsync -a ./git-subtree.sh /usr/local/bin/git-subtree
或者,如果您需要手册页和全部
Or if you want the man pages and all
make doc
make install
用法
将大块切成小块:
Usage
Split a larger into smaller chunks:
# Go into the project root
cd ~/my-project
# Create a branch which only contains commits for the children of 'foo'
git subtree split --prefix=foo --branch=foo-only
# Remove 'foo' from the project
git rm -rf ./foo
# Create a git repo for 'foo' (assuming we already created it on github)
mkdir foo
pushd foo
git init
git remote add origin git@github.com:my-user/new-project.git
git pull ../ foo-only
git push origin -u master
popd
# Add 'foo' as a git submodule to `my-project`
git submodule add git@github.com:my-user/new-project.git foo
有关详细文档(手册页),请阅读git-subtree.txt
.
For detailed documentation (man page), please read git-subtree.txt
.
这篇关于如何提取一个git子目录并从中创建一个子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!