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
gitpushes 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 pushtorepo1after addingtest_repo1.txt, the branch only has your commit for repo one. However, once you dogit add test_repo2.txtand commit it, the same branch now has both commits, so both changes are applied torepo2when 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_repo1andbranch_repo2. You willgit pusheach branch to its respective repo. Your procedure is this:git clone repo1as before, andgit checkout master(or whatever branch you want to start from)git checkout -b branch_repo1to create and check out a branch for yourrepo1changes.git add test_repo1.txtandgit commitandgit push repo1 master(replacingmasterby whatever branch onrepo1you want to push to)git checkout masteragain to go back to your starting branch (which will not have the commit fortest_repo1.txt)git checkout -b branch_repo2to create a branch for yourrepo2changes.git add test_repo2.txtandgit commitandgit push repo2 masterrepo1 masterwill have the commit you put in thebranch_repo1branch, andrepo2 masterwill have the commit you put in thebranch_repo2branch.Note that in your
git pushyou must specify not only which repo but which branch you want to push to. Assuming you are working frommasteron 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 justrepo1andrepo2to avoid repo name/branch name ambiguity. You can name the branches whatever you want).