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
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:
git clone repo1
as before, and git checkout master
(or whatever branch you want to start from) git checkout -b branch_repo1
to create and check out a branch for your repo1
changes.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)git checkout master
again to go back to your starting branch (which will not have the commit for test_repo1.txt
)git checkout -b branch_repo2
to create a branch for your repo2
changes.git add test_repo2.txt
and git commit
and git push repo2 master
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).