git push fails - what am I doing wrong?
Configuring and working with git locally seems to be working fine:
~/sb> mkdir proj1
~/sb> cd proj1
~/sb/proj1> echo "asdf" > file1.txt
~/sb/proj1> git init
~/sb/proj1> git add .
~/sb/proj1> git commit -a -m "Import"
~/sb/proj1> git branch
* master
The problem begins when I try to push this to a central repository:
~/sb/proj1> cd /home
~> mkdir temp-repo
~> cd temp-repo/
~/temp-repo> git init --bare
Initialized empty Git repository in /home/temp-repo/
~/temp-repo> cd ~/sb/proj1/
~/sb/proj1> git clone /home/temp-repo/
Cloning into temp-repo...
done.
warning: You appear to have cloned an empty repository.
~/sb/proj1> git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedl开发者_StackOverflowy
Note: The --bare
and origin master
above were taken from a solution to a problem posted here: pushing to a git repository does not work
Yet, I don't seem to figure out what I am missing. Must be something trivial, but what is it?
UPDATE: The answer by @Firoze Lafeer below works:
~/sb/proj1> cd /home
~> mkdir temp-repo
~> cd temp-repo/
~/temp-repo> git init --bare
Initialized empty Git repository in /home/temp-repo/
~/temp-repo> cd ~/sb/proj1/
~/sb/proj1> git remote add origin /home/temp-repo
~/sb/proj1> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/temp-repo
* [new branch] master -> master
~/sb/proj1>
Now I need to understand why. In particular, what did I misunderstand in this answer which suggests doing a git clone
before git push origin master
.
When you did:
git clone /home/temp-repo/
you should now have a clone of the repo in a new directory ~/sb/proj1/temp-repo/
If I'm understanding your question correctly, that's not what you wanted.
If you already have a repository in ~/sb/proj1 then don't clone the other repository. Just add the other one as a remote.
So assuming you created the local repo first in ~/sb/proj1 and then created the temp-repo and you want to push from the first to the second:
git remote add origin /home/temp-repo
git push origin master
Or just do it the other way around. Make the repo in /home/temp-repo first and then clone it and push to it.
Hope that helps?
edit
To hopefully further explain why do didn't want clone here:
A clone is in the case where you haven't already created a local repository. So you're asking git to copy some remote repository, which then implicitly sets it as the remote called 'origin' in the new local copy. In your case, you already had a local repo, so all you wanted is to set the other repo as the remote called 'origin'.
So now your local repo is aware of another repo called 'origin'. And then you can push to that 'origin'. You don't have to call it 'origin' of course, you could call it 'cats' and then 'git push cats master' if you like.
OR you can do it the other way around. So you could make the temp-repo first and then do something like:
cd ~/sb
git clone /home/temp-repo proj2
cd proj2
# make some changes...
git push origin master
That works, too. That copies temp-repo into a new repo in a new directory proj2. Then in proj2 that new repo is aware of a remote repo called 'origin' (/home/temp-repo), so can push commits back to it.
精彩评论