Trong bài đăng này, tôi mô tả cách đơn giản nhất mà tôi tìm thấy để quản lý các kho lưu trữ con của Git.
Cảnh báo: bài đăng này đã cũ và có thể không phản ánh tình trạng hiện tại của nghệ thuật
Giả sử trong dự án của bạn, bạn có một thư mục mà bạn muốn quản lý trên một kho lưu trữ Git riêng biệt.
Hoặc, giả sử bạn muốn một phần mã nguồn mở của ứng dụng của mình, nhưng bạn không muốn gánh nặng quản lý một thư mục riêng biệt, các liên kết tượng trưng và các lần đẩy Git riêng biệt.
Bạn có thể làm gì?
Nhập cây con
Đầu tiên, tạo một kho lưu trữ mới sẽ lưu trữ các tệp bạn cần. Nếu sử dụng GitHub, hãy khởi tạo nó bằng tệp README.md để nó sẽ tự động tạomaster
chi nhánh.
Tiếp theo, thêm nó làm điều khiển từ xa bên trong dự án của bạn.
$ git remote add repository-name [email protected]:yourname/repository-name.git
$ git subtree add --prefix=subfolder/path/you/want repository-name masterChange repository-name
, subfolder/path/you/want
and yourname
with your chosen names.
You can add --squash
to the last command if the project want to incorporate has already many commits, and you want to pull all changes in one single commit.
You’re done!
Pushing and pulling
Now, suppose someone pushes a PR on that subrepository, or you edit it from another project.
To incorporate the changes inside your project, do:
git subtree pull --prefix=subfolder/path/you/want repository-name masterYou can add --squash
if you want to incorporate all changes in one single commit.
Now you’ll push to your own project, and the changes made in the subrepository are stored in the Git history of it.
To also push those changes to the subrepository Git history, and to the remote, just do
git subtree push --prefix=subfolder/path/you/want repository-name masterAgain, use --squash
if you feel it appropriate. Git will cherry-pick the commits that involve the subrepository subdirectory, so you’ll just see those changes in the subrepository history.
You might want to be careful and differentiate the commits that involve the subrepository. In this way you’ll keep things separated.
More git tutorials:
- A Git Cheat Sheet
- Git workflow to manage work on multiple branches
- An easy way to handle Git subrepositories
- An incomplete list of great Git tutorials
- A developer's introduction to GitHub
- The complete Git guide
- How to discover a bug using git bisect
- How to make your first Pull Request on GitHub
- How to update a Git branch from another branch
- I posted my password / API key on GitHub
- Squashing Git commits
- How to remove a Git remote