Reputation: 285
I'm really new to git and on my laptop (running windows, let's call it Laptop A) I set up a git repository in my project folder following this guide. Now on my Laptop B I'd like to have access to that repository too so that I can check out files, work with them and then upload them back to the repository. My understanding is that I have a repository local to Laptop A, so what would I need to do to get access to it (if that's possible)? I found a few posts with a similar issue but I believe they were addressing the issue on linux machines - which I will have to do at some point. Laptop A and B are on the same network, but is that relevant? Sometimes they might be on different networks. I'm still looking into git, so, sorry if I said something completely off-the-wall
Upvotes: 3
Views: 3741
Reputation: 4528
In addition to Jeremy Fortune's excellent answer I would like to add that Git has an additional mechanism of sharing repositories between machines on the same network, and that is the git daemon
command.
You can read a reasonable tutorial here to get you going. There are plenty more examples online and questions on this site to help you set it up if you decide to go this way. Note that it's not meant for hosting your repositories as a server but is most used to quickly share a repository, but that wasn't your question to begin with so it should be ok.
Edit 1: Git Server Recommendation
If anyone is looking at this question and is interested in hosting their own Git server then I recommend looking up GitLab.
Edit 2: Git over SSH
I would also like to add that Git understands a few protocols but my personal favourite is SSH. If you have an SSH daemon running on a remote machine you can simply do a fetch straight over SSH without anything else being setup (there are SSH daemons for Windows as well).
i.e.
git fetch <username>@<hostname>:<path-to-repository> 'refs/heads/:refs/remotes/<repo-B>'
replace <username>
, <hostname>
, <path-to-repository>
, and <repo-B>
with the appropriate values and it should work. So if we use terminology from your example it would be:
git fetch antobbo@laptop-B:/repos/repo-B 'refs/heads/:refs/remotes/repo-B'
Note that you may need to prefix it with ssh://
but from personal experience Git just figures it out anyway.
Now, you can save that repository's network location as a remote which simplifies the commands, like Jeremy Fortune mentions in his answer.
i.e.
git remote add repo-b antobbo@laptop-B:/repos/repo-B
git fetch repo-b # note that you don't need a refspec now
For more on remotes: Atlassian has an excelent tutorial on this, so does GitHub, and so does Git itself. Those 3 are pretty much the goto places for Git tutorials.
Using git fetch
like this will prompt you for your password, unless you've set up the authorized_keys
file.
This is fairly easy to setup if the machine you want to fetch
from (the one that needs the SSH daemon) is a Linux machine but I haven't tried it with a Windows or Mac OS X machine so I can't say how easy or hard it would be. I have used Git daemon on both Linux & Windows (but not on Mac) and know that it is easy so my recommendation is to use it if at all unsure.
Most of the information I'm finding on Google seems to be geared towards setting up a fully-fledged Git server and I only managed to find this example suggesting the same use case as I am above.
Upvotes: 6
Reputation: 2499
Git communicates with other copies of the same repository via the remote
subcommand. When setting up a remote, you give the full path a pseudonym (the most common of which is "origin").
git remote add <pseudonym> <actual-path>
You can have remotes communicate over a variety of protocols, including simple file paths. For example:
git remote add laptop-a //laptop-b/path/to/repository
When using this method, syncing laptop B with changes made on laptop A requires that you issue this command on laptop B:
git pull laptop-a <branch-name>
But if that path isn't always available, it's a good idea to have some other copy of the repository on the web so that both can always see it and sync with it when necessary. So instead of making a direct link to laptop-a
on laptop B, we can make another copy somewhere on the web and create links on B to it instead.
git remote add origin http://domain.com/path/to/repository
And on A, use the same command.
Then when you want to synchronize between laptop A and laptop B after making a change on A, issue this on A:
git push origin <branch-name>
And on B:
git pull origin <branch-name>
Note that if you're tracking the same branch on the origin repository on both laptop A and laptop B, the above commands can be simplified to git push
and git pull
, respectively.
Upvotes: 5