Issue
I might be overcomplicating this a lot, but I have a git repo on my machine, and I've got another machine at work I usually connect to for work. Every once in a while I need to get some code that I mainly develop in my machine to this work machine, and I would like to just use git instead of copying the code with scp or rsync so that I can keep track of versions and so on.
So to clone the repo, I thought I would have to reverse ssh into the work machine, and then again clone the git repo through the reversed ssh connection.
A test could be
mkdir testgit && cd testgit
touch file
git init
git add file
git commit -m "test commit"
This in the local machine. Now what I was trying (but doesn't work) is
localmachine $ ssh -R 43022:localhost:22 user@remote
remotemachine $ git clone ssh://user@localhost:43022/home/user/testgit/
This however always gives me Connection reset by peer
, so I don't think I really understand what this reversed connection is doing. What would be the proper way to do this?
Solution
You don't need to clone to remote, especially in such a overcomplicated way, you just need to push.
On the remote side there have to be 2 repositories — bare and non-bare. Bare repo is where you push to from the local repo. Non-bare repo is a repo with working tree (checked out files) where you pull from bare.
Create a new empty bare and non-bare repositories; push to bare from the local one:
ssh user@remote -p 43022 "
cd /home/user &&
git init --bare testgit.git &&
git init testgit &&
cd testgit &&
git remote add bare ../testgit.git"
git remote add work ssh://user@remote:43022/home/user/testgit.git
git push -u work master # or how do call your master branch
Pull from bare to non-bare:
ssh user@remote -p 43022 "cd /home/user/testgit && git pull bare master"
You can automate the last step using a post-receive
or post-update
hook in the bare repo.
Answered By - phd Answer Checked By - Marilyn (WPSolving Volunteer)