git repository sync between computers, when moving around?

本小妞迷上赌 提交于 2019-12-17 21:38:21

问题


Let's say that I have a desktop pc and a laptop, and sometimes I work on the desktop and sometimes I work on the laptop.

What is the easiest way to move a git repository back and forth?

I want the git repositories to be identical, so that I can continue where I left of at the other computer.

I would like to make sure that I have the same branches and tags on both of the computers.

Thanks Johan

Note: I know how to do this with SubVersion, but I'm curious on how this would work with git. If it is easier, I can use a third pc as classical server that the two pc:s can sync against.

Note: Both computers are running Linux.


Update:

So let's try XANI:s idea with a bare git repo on a server, and the push command syntax from KingCrunch. In this example there is two clients and one server.

So let's create the server part first.

ssh user@server
mkdir -p ~/git_test/workspace
cd ~/git_test/workspace
git --bare init

So then from one of the other computers I try to get a copy of the repo with clone:

git clone user@server:~/git_test/workspace/
Initialized empty Git repository in /home/user/git_test/repo1/workspace/.git/
warning: You appear to have cloned an empty repository.

Then go into that repo and add a file:

cd workspace/
echo "test1" > testfile1.txt
git add testfile1.txt
git commit testfile1.txt -m "Added file testfile1.txt"
git push origin master

Now the server is updated with testfile1.txt.

Anyway, let's see if we can get this file from the other computer.

mkdir -p ~/git_test/repo2
cd ~/git_test/repo2
git clone user@server:~/git_test/workspace/
cd workspace/
git pull

And now we can see the testfile.

At this point we can edit it with some more content and update the server again.

echo "test2" >> testfile1.txt
git add testfile1.txt
git commit -m "Test2"
git push origin master

Then we go back to the first client and do a git pull to see the updated file. And now I can move back and forth between the two computers, and add a third if I like to.


回答1:


I think, there are multiple approaches. I will just describe, how I handle this

I have one netbook as a 24/7 server, that holds multiple git-repositories. From/To there I push and pull changes via SSH. For access from outside I use dyndns.org. It works fine, especially because I have more than two systems, that needs access to some of the repositories.

Update: A little example. Lets say my netbook is called "netbook". I create a repository there

$ ssh username@netbook.local
$ cd ~/git
$ mkdir newThing
$ cd newThing
$ git init --bare

On my desktop I will than create a clone of it. Maybe I will add some files also

$ git clone username@netbook.local:/home/username/git/newThing
$ git add .
$ git commit -m "Initial"
$ git push origin master

On my portables I will (first) do the same, but for remote access (from outside my LAN), I will also add the external address.

$ git clone username@netbook.local:/home/username/git/newThing
$ git remote add externalName username@mydyndns.home-ip.org:/home/username/git/newThing
$ git pull externalName master

Its just the way git (/git workflows) works. You can add as many remote repositories as you like. It doesnt matters, if two or more refers to the same "physical" repositories. You dont need an own local "server", you can use any public server, to which you have ssh access. And of course you dont need a public server at all, if you dont need access from outside. The bare repository can also be on the desktop system and you can then create a working-copy-repository within the local filesystem.

$ mkdir myRepo; cd myRepo
$ git init --bare
$ cd /path/to/myProject
$ git remote add origin /path/to/myRepo
$ git add .; git commit -m "Initial"; git push origin master

This is the way, how I handle this, and I for me it works quite fine (if not perfect ;))

Something to read: http://progit.org/ Really good book.-




回答2:


I would clone the repo from one box to the other, and then set up the two repos so that I can just git fetch from the other box.

Renaming the remote from origin to the name of the other box makes the remote branches easier to read.

Note that by just using git fetch (and not git push), this works well with non-bare repositories:

[user@foo repo]$ git fetch -v bar

[user@bar repo]$ git fetch -v foo



回答3:


Easiest way: central repo created with --bare (so no checked out files, only .git stuff), or github

"Distributed" will look like that:

Setup:

  1. On laptop: git remote add desktop ssh://user@desktop/home/user/repo/path
  2. On desktop: git remote add laptop ssh://user@laptop/home/user/repo/path

Syncing:

git pull laptop/desktop (push won't work very well on non-bare repos because git won't modify checked out files when pushing to remote repo)

Or, make repo on pendrive ;)




回答4:


How about simply using rsync?




回答5:


Couldn't you just create a remote repository on GitHub, BitBucket or GitLab? (The latter two companies offer unlimited free private repositories). When you finish the day at work, simply use git push to push your changes to the remote repo. When you get home, just do git pull to pull your changes from work onto your home machine. Likewise, when you finish at home, do git push and then when you return to work, do git pull.




回答6:


Well, you can push and pull (via Git) to the server you could potentially set up. Or you could store your repos at GitHub and use that as a syncing bridge.




回答7:


You could make the repository on any of your computers, probably the desktop one and push/pull to it from both laptop and itself.



来源:https://stackoverflow.com/questions/4948190/git-repository-sync-between-computers-when-moving-around

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!