Setup new remote in sourcetree - can't work out correct path
Asked by anthubbard:

Hi,

I have a repo setup in Bitbucket (origin) and a clone on my local machine for dev work and a clone on my production server (a VPS). Currently my workflow is:

- I commit my changes in sourcetree and push them to back to the origin (bitbucket)

- SSH into my VPS and pull the repo changes from origin

I would much rather setup another remote in bitbucket and be able to push straight to my VPS via sourcetree rather than having to SSH in.

Is this:

a) able to be done

b) smart?

c) how do i do it - I worked out how to create a new remote in sourcetree but I can't for the life of me work out what the path is for my VPS. I'm using https because I couldn't get bitbucket and my VPS to be friends with SSH keys.

I appreciate any help, thanks,

Anthony


Answers:
author: KieranA
body: Hi Anthony,

Provided your VPS has got git installed then you can do the following:

1. git push <ssh://username@host/path/to/repo> (making sure SSH is set up)
2. Set up a path to push to in your config so when pushing via SourceTree you can select that remote from the list

Figuring out the path to your VPS will be figuring out what your hostname is, to do this just enter "hostname" at the terminal. Then from there just enter "cd /" and then go through your directory structure until you find the folder where your repository is. You say you need to SSH in, but actually the push will manage the SSH as you're doing it through the SSH protocol.

When you set up the VPS git directory you want to do "git --bare init" because if it's already initialised and not bare then you can't just push to it. Then from your client you can just push to that repo and the changes will be in there. You'll also be able to make changes from there too if needs be.

So to summarise:

1. Set up SSH on your VPS, there's a load of guides out there to do so for just setting up public/private keys (<https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git>)
2. Do "git --bare init" on the directory you'll want to be pushing to on your VPS. The --bare is so you can push to it initially.
3. Find out the hostname of your VPS by doing "hostname" at the terminal/command line.
4. Figure out the path to your repository when you've created it. You can do this by typing "pwd" (for present working directory) in the terminal
5. Use the hostname and the "pwd" output to set up a remote in the format of <ssh://username@hostname/path/to/repo>
6. Push to that remote!

I hope I've covered everything needed. The only thing I should note is you can't push to multiple remotes at once in SourceTree at the moment (I think this is logged in JIRA as a feature request) so you'd have to push to both remotes. Also, I wonder why your VPS hasn't got access to pull from the default remote on BB?

Have a try at completing some of the steps, if you get stuck feel free to ask more questions.

comments: author: anthubbard
body: Hi Kieren,

Thanks so much, that was an epic answer - It made perfect sense and I got the path fixed. I have two further questions now:

1) Is the correct path - ssh://username@hostname/path/to/repo OR ssh://username@hostname/path/to/repo/repo.git ??

I.e. is it the folder the repo is in I'm targeting or the .git folder itself inside the folder my repo is in.

2) With my VPS I had ssh-ed in previously and cloned the repo from origin (bitbucket) and then run 'git pull origin' on that repo in my VPS via ssh. If I then setup a remote in Sourcetree and run the 'push' command, will it just push the changes to my already cloned repo, or will it try to push a whole new clone version to the path I specified?

Thanks so much again,

Anthony


author: KieranA
body: Hi Anthony,

1) The former will do it, it detects if there's a .git folder to determine if it's a valid git repository

2) It should push all changes since the changes from your last pull. This is why you need to do git init --bare on the VPS because it's a remote. When it goes to push there it won't know precisely what to push so you'll likely get an error. You could set up a test repo on the VPS to test this by just a) copying your existing repo into a test repo to see if the push works or b) do git init --bare. This way, you shouldn't worry about damaging your repo on the VPS (that shouldn't happen, but I always play it safe). If you look at your log view in ST you'll see it says something like "4 ahead" - it knows how many commits have happened since the last pull, so that's what it's pushing. Mercurial push actually shows you what's being pushed. There's a much detailed explanation of this here: <http://stackoverflow.com/questions/4179842/predict-how-much-data-will-be-pushed-in-a-git-push>

Hope that helps :)