Git如何将不同的工作副本推送到不同的裸存储库
当我尝试仅将我创建的新文件推送到不同的裸存储库时,我遇到了问题。
假设我有两个裸存储库:repo1.git 和 repo2.git
我克隆了 repo1.git 的副本并创建了一个名为 test_repo1.txt 的新文件< /p>
我将 test_repo1.txt 推送到源 (repo1.git)
我现在创建另一个名为 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
I clone a copy of repo1.git and create a new file called test_repo1.txt
I push test_repo1.txt to the origin (repo1.git)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
git
通过以下分支推送提交,而不是单个文件。在您的情况下,您有两个提交,test_repo1.txt
的提交和test_repo2.txt
的提交,但每个提交都位于同一分支中。当您在添加
test_repo1.txt
后git Push
到repo1
时,分支仅包含您对存储库 1 的提交。但是,一旦您执行git add test_repo2.txt
并提交它,同一分支现在就有两个提交,因此这两个更改都会应用于repo2
当你推的时候。为了完成您想要做的事情,您需要在本地工作副本中有两个分支,我们将它们称为
branch_repo1
和branch_repo2
。您将git Push
每个分支到其各自的存储库。您的程序是这样的:git clone repo1
和git checkout master
(或您想要从中启动的任何分支)git checkout -bbranch_repo1
> 为您的repo1
更改创建并签出分支。git add test_repo1.txt
和git commit
和git Push repo1 master
(用上的任何分支替换
你想要推送到)master
>repo1git checkout master
再次返回到你的起始分支(该分支不会有test_repo1.txt
的提交git checkout -bbranch_repo2
为您的repo2
更改创建分支。git add test_repo2.txt
和git commit
和git push repo2 master
repo1 master
将拥有您在branch_repo1
分支中放入的提交,repo2 master
将拥有您在branch_repo2< 中放入的提交/代码> 分支。
注意,在
git Push
中,您不仅必须指定哪个存储库,还必须指定要推送到哪个分支。假设您在两者上都在master
上工作,您会想要这样做:并且
这将完成您想要做的事情。
(最后注意:我使用
branch_
前缀命名分支,而不是仅使用repo1
和repo2
以避免存储库名称/分支名称歧义。可以随意命名分支)。git
pushes commits by following branches, not individual files. In your case you have two commits, the commit fortest_repo1.txt
, and the commit fortest_repo2.txt
, but each is in the same branch.When you
git push
torepo1
after addingtest_repo1.txt
, the branch only has your commit for repo one. However, once you dogit add test_repo2.txt
and commit it, the same branch now has both commits, so both changes are applied torepo2
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
andbranch_repo2
. You willgit push
each branch to its respective repo. Your procedure is this:git clone repo1
as before, andgit checkout master
(or whatever branch you want to start from)git checkout -b branch_repo1
to create and check out a branch for yourrepo1
changes.git add test_repo1.txt
andgit commit
andgit push repo1 master
(replacingmaster
by whatever branch onrepo1
you want to push to)git checkout master
again to go back to your starting branch (which will not have the commit fortest_repo1.txt
)git checkout -b branch_repo2
to create a branch for yourrepo2
changes.git add test_repo2.txt
andgit commit
andgit push repo2 master
repo1 master
will have the commit you put in thebranch_repo1
branch, andrepo2 master
will have the commit you put in thebranch_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 frommaster
on both, you would want to do:and
This will accomplish what you want to do.
(final note: I named the branches with a prefix of
branch_
instead of justrepo1
andrepo2
to avoid repo name/branch name ambiguity. You can name the branches whatever you want).