git - 使用 Git 分离要修改的目录作为分支 elsewere

标签 git

假设我的项目布局如下:

MyProject/
├── Examples
│   ├── ExamplesA
│   ├── ExamplesB
│   └── ExamplesC
└── TheLibrary

所以我开发了这个很棒的库和一些我想与库一起分发的使用示例。

问题是,必须将库和单独的库 checkin TFS 存储库。同时,我的老板让我保留一份原样的项目副本,以备后用。

MyProject directory 是一个 Git 存储库,当然。我正在考虑分支一个目录(即 MyProject/TheLibrary ),但我如何使用 Git 来做到这一点?我读过 git-subtree ,但恐怕我在无尽的丛林中迷路了pull s 和 push es。有人可以帮我吗?

理想情况下,我希望有两个分支:master包含整个项目,以及tfsMyProject/TheLibrary 的内容文件夹。然后我会创建开发分支并将它们 merge 到上面的这两个分支。使用 Git 是否可以做到这一点或类似的事情?

最佳答案

如果你只想将最新版本的 TheLibrary 移动到一个新的分支,你可以这样做(我假设你在分支 master 上):

$ git rm -rf Examples
$ git symbolic-ref HEAD refs/heads/tfs
$ git commit -m 'New Branch for TheLibrary'

现在切换回 master 并删除 TheLibrary

$ git checkout master
$ git rm -rf TheLibrary
$ git commit -m 'TheLibrary moved to branch tfs'

结果是这样的

Repository View                     Working Directory View

o-----o-----o-----o                 MyProject/
                  ^                   ├── Examples
                  |                    ├── ExamplesA
                master                 ├── ExamplesB
                                       └── ExamplesC


o                                    MyProject/
^                                     └── TheLibrary
|
tfs

那么会发生什么......

  1. git rm -rf Examples 从您的工作目录和索引(暂存区)中删除 Examples 及其子目录
  2. git symbolic-ref HEAD refs/heads/tfsHEAD 引用指向一个名为 tfs 的新分支
  3. git commit -m 'New Branch for TheLibrary' 提交索引(现在只包含 TheLibrary)作为 tfs 的新提交分支机构。
  4. git checkout master 切换回master
  5. git rm -rf TheLibrary 从工作目录和索引中删除 TheLibrary
  6. git commit -m 'TheLibrary moved to branch tfs' 在不再包含 TheLibrary 的 master 上进行新的提交,并提交一条指向新的提交消息地点。

关于git - 使用 Git 分离要修改的目录作为分支 elsewere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844190/

相关文章:

Git/SVN 互操作(保留两个存储库的历史记录)

linux - Debian - GIT 从 1.5 升级到 1.7

gitignore 加载其他 gitignores

git 分支和提交——你能解释一下这种(对我来说)令人费解的行为背后的理论吗?

git - 错误 merge 后恢复未提交的更改

git - 为什么 `git show-ref FETCH_HEAD` 是空的?

git - 如果更改的字符少于 2 个,GIT 中是否有一种方法可以删除已提交的更改文件?

git - 将 GIT-SVN 调试-> 功能分支传输到主干的最佳方法是什么?

git - 使用 git commit history 查找项目热点?

ruby-on-rails - 我应该在Heroku上部署Ruby on Rails应用程序吗