Git push pushes nothing

2019-08-11 09:39发布

问题:

I have 2 remote repos: one at bitbucket, another at amazon ec2. I added the second one recently.

git remote -v

ec2 ubuntu@xx.xx.xx.xx:/home/ubuntu/my_app.git (fetch)
ec2 ubuntu@5xx.xx.xx.xx:/home/ubuntu/my_app.git (push)
origin  ssh://git@bitbucket.org/me/my_app.git (fetch)
origin  ssh://git@bitbucket.org/me/my_app.git (push)

When I push origin it works well. But when I push to ec2, it also seems to work well, however via ssh don't see anything it pushed. I've done it 2 times already but I there is no source in there. Here is a git directory at ec2:

ubuntu@ip-xx-xx-xx-xx:~/my_app.git$ ls -al
total 40
drwxrwxr-x  7 ubuntu ubuntu 4096 Mar 15 01:52 .
drwxr-xr-x  6 ubuntu ubuntu 4096 Mar 15 01:50 ..
drwxrwxr-x  2 ubuntu ubuntu 4096 Mar 15 01:52 branches
-rw-rw-r--  1 ubuntu ubuntu   66 Mar 15 01:52 config
-rw-rw-r--  1 ubuntu ubuntu   73 Mar 15 01:52 description
-rw-rw-r--  1 ubuntu ubuntu   23 Mar 15 01:52 HEAD
drwxrwxr-x  2 ubuntu ubuntu 4096 Mar 15 12:04 hooks
drwxrwxr-x  2 ubuntu ubuntu 4096 Mar 15 13:30 info
drwxrwxr-x 10 ubuntu ubuntu 4096 Mar 15 13:30 objects
drwxrwxr-x  4 ubuntu ubuntu 4096 Mar 15 01:52 refs

I took this advice Can't push to repository on EC2 - Could not resolve hostname for adding ec2 as a remote git repo.

Here is one of the pushes:

$ git push ec2 master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 749 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
To ubuntu@xx.xx.xx.xx:/home/ubuntu/my_app.git
   123..123abc  master -> master

Note: there is only one branch, gitk displays everything correctly. It just doesn't push anything to ec2.

回答1:

The repo you have created on your EC2 instance is a bare repo. So, as such, you won't see any code files over there, only the git blobs of your repo. Check this and this.

The way to check if the code is actually being pushed will be

ssh ubuntu@xx.xx.xx.xx #log-in to your EC2 server
git clone my_project my_project.git

Now in the folder my_project, you will find the actual code checked out.

So, your normal work flow in this case will be something like

git push ec2 master #from your dev machine
ssh ubuntu@xx.xx.xx.xx #log-in to your EC2 server
cd my_project
git pull origin master #after last clone, the original bare repo became the origin repo for this clone

After the above, any new code you push will be available here as well.

EDIT

In your case, you are already using bitbucket, so I would suggest exploring this workflow - clone the repo from bitbucket, and use the bitbucket as the reference point, both on ec2, and from your local dev environment. This will free you of any backup related hassles on your server. You can add multiple ssh keys on bitbucket, so you will need to generate the keys on your ec2 and add the public key to bitbucket.

So in this case,

  1. push code from your Dev to bitbucket
  2. Ssh to ec2
  3. On ec2, either clone the bitbucket repo using git clone https_bitbucket_url
  4. Or setup ssh keys on ec2 and now use git clone ssh_bitbucket_url
  5. Or to a new non bare repo, add a new remote using bitbucket URL.