Git如何将不同的工作副本推送到不同的裸存储库

发布于 2024-11-30 20:53:50 字数 873 浏览 0 评论 0原文

当我尝试仅将我创建的新文件推送到不同的裸存储库时,我遇到了问题。

假设我有两个裸存储库:repo1.gitrepo2.git

  1. 我克隆了 repo1.git 的副本并创建了一个名为 test_repo1.txt 的新文件< /p>

  2. 我将 test_repo1.txt 推送到源 (repo1.git)

  3. 我现在创建另一个名为 test_repo2.txt 的文件 我想 test_repo2.txt 从我的 repo1.git 工作副本推送到 repo2.git 存储库。

因此,我在 repo1.git 工作副本下运行了以下命令。

git add test_repo2.txt
git commit -m "add only test_repo2.txt file to repo2.git"
git push repo2.git master:master

执行上述命令后,

我进入 repo2.git 工作副本,发现“test_repo2.txt”文件存在,但“test_repo1.txt”文件存在也在那里,这不是我想要的。我只希望“test_repo2.txt”存在而不是“test_repo1.txt”。

为什么“test_repo1.txt”文件最终出现在repo2.git裸存储库中?

我们将非常感谢您的帮助。

谢谢弗诺

I've run into a problem when I try to push only the new files I created to a different bare repository.

Say, I had two bare repository: repo1.git and repo2.git

  1. I clone a copy of repo1.git and create a new file called test_repo1.txt

  2. I push test_repo1.txt to the origin (repo1.git)

  3. I now create another file called test_repo2.txt
    I want to push ONLY test_repo2.txt from my repo1.git working copy to repo2.git repository.

So, I ran the following commands under my repo1.git working copy.

git add test_repo2.txt
git commit -m "add only test_repo2.txt file to repo2.git"
git push repo2.git master:master

Once I did the above commands,

I went in to the repo2.git working copy and find out that "test_repo2.txt" file is there but "test_repo1.txt" file is also there as well which is not what I want. I only want "test_repo2.txt" to be there not "test_repo1.txt".

Why did the "test_repo1.txt" file end up in the repo2.git bare repository ?

Your help will be much appriciated.

Thanks

Finau

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-12-07 20:53:50

git 通过以下分支推送提交,而不是单个文件。在您的情况下,您有两个提交,test_repo1.txt 的提交和 test_repo2.txt 的提交,但每个提交都位于同一分支中。

当您在添加 test_repo1.txtgit Pushrepo1 时,分支仅包含您对存储库 1 的提交。但是,一旦您执行 git add test_repo2.txt 并提交它,同一分支现在就有两个提交,因此这两个更改都会应用于 repo2当你推的时候。

为了完成您想要做的事情,您需要在本地工作副本中有两个分支,我们将它们称为 branch_repo1branch_repo2。您将git Push每个分支到其各自的存储库。您的程序是这样的:

  1. 像以前一样 git clone repo1git checkout master (或您想要从中启动的任何分支)
  2. git checkout -bbranch_repo1 > 为您的 repo1 更改创建并签出分支。
  3. git add test_repo1.txtgit commitgit Push repo1 master (用 上的任何分支替换 master >repo1 你想要推送到)
  4. git checkout master 再次返回到你的起始分支(该分支不会有 test_repo1.txt 的提交
  5. )重复:git checkout -bbranch_repo2 为您的 repo2 更改创建分支。
  6. git add test_repo2.txtgit commitgit push repo2 master
  7. 完成。 repo1 master 将拥有您在 branch_repo1 分支中放入的提交,repo2 master 将拥有您在 branch_repo2< 中放入的提交/代码> 分支。

注意,在 git Push 中,您不仅必须指定哪个存储库,还必须指定要推送到哪个分支。假设您在两者上都在 master 上工作,您会想要这样做:

git push repo1 master # while you have the branch_repo1 branch checked out

并且

git push repo2 master # while you have the branch_repo2 branch checked out

这将完成您想要做的事情。

(最后注意:我使用 branch_ 前缀命名分支,而不是仅使用 repo1repo2 以避免存储库名称/分支名称歧义。可以随意命名分支)。

git pushes commits by following branches, not individual files. In your case you have two commits, the commit for test_repo1.txt, and the commit for test_repo2.txt, but each is in the same branch.

When you git push to repo1 after adding test_repo1.txt, the branch only has your commit for repo one. However, once you do git add test_repo2.txt and commit it, the same branch now has both commits, so both changes are applied to repo2 when you push.

To accomplish what you want to do, you will need to have two branches in your local working copy, let's call them branch_repo1 and branch_repo2. You will git push each branch to its respective repo. Your procedure is this:

  1. git clone repo1 as before, and git checkout master (or whatever branch you want to start from)
  2. git checkout -b branch_repo1 to create and check out a branch for your repo1 changes.
  3. git add test_repo1.txt and git commit and git push repo1 master (replacing master by whatever branch on repo1 you want to push to)
  4. git checkout master again to go back to your starting branch (which will not have the commit for test_repo1.txt)
  5. now repeat: git checkout -b branch_repo2 to create a branch for your repo2 changes.
  6. git add test_repo2.txt and git commit and git push repo2 master
  7. Done. repo1 master will have the commit you put in the branch_repo1 branch, and repo2 master will have the commit you put in the branch_repo2 branch.

Note that in your git push you must specify not only which repo but which branch you want to push to. Assuming you are working from master on both, you would want to do:

git push repo1 master # while you have the branch_repo1 branch checked out

and

git push repo2 master # while you have the branch_repo2 branch checked out

This will accomplish what you want to do.

(final note: I named the branches with a prefix of branch_ instead of just repo1 and repo2 to avoid repo name/branch name ambiguity. You can name the branches whatever you want).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文