Git 仓库在计算机之间的同步,当移动?

假设我有一台台式电脑和一台笔记本电脑, 有时我在台式机上工作,有时我在笔记本电脑上工作。

来回移动 Git 存储库最简单的方法是什么?

我希望 git 仓库是一样的, 这样我就可以在另一台电脑上继续我的工作了。

我想确保我有相同的分支和两台计算机上的标签。

谢谢 约翰

注意: 我知道如何使用 SubVersion 实现这一点,但是我很好奇如何使用 git 实现这一点。如果方便的话,我可以使用第三台电脑作为经典服务器,两台电脑可以在其上进行同步。

注意: 两台计算机都在运行 Linux。


更新 :

因此,让我们尝试 XANI: 的想法与一个赤裸的 git 回购在服务器上, 以及来自 KingCrunch 的 push 命令语法。 在这个示例中,有两个客户机和一个服务器。

因此,让我们首先创建服务器部分。

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

然后我试着从另一台电脑上,复制一份回购文件:

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.

然后进入回购,并添加一个文件:

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

现在使用 testfile1.txt 更新服务器。

不管怎样,我们看看能不能从另一台电脑上找到这个文件。

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

现在我们可以看到 Testfile。

此时,我们可以用更多的内容编辑它,并再次更新服务器。

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

然后我们返回到第一个客户机并执行 git 提取以查看更新后的文件。 现在我可以在两台电脑之间来回移动, 如果我愿意再加三分之一。

55674 次浏览

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.

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.-

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

How about simply using rsync?

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

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 ;)

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.

What is the easiest way to move a git repository back and forth [between 2 computers]?

Scenario 1: I work (edit code and files) exclusively on PC1 but want to have a duplicate copy of the files (ex: to build the whole code base) also on PC2.

Sync from PC1 to PC2 in < 1 minute over a wifi hotspot while using < 25 MB of data:

I work on one weak computer I travel with (a laptop), but build on a more powerful computer located elsewhere. I use git all the time to sync from my laptop to the other computer using a script. I just type this command to run it:

sync_git_repo_from_pc1_to_pc2

That's it! It usually takes about 25 MB of data and ~30 sec to 1 min, even when using a cell phone wifi hotspot and working on a repo that is dozens of gigabytes in size. I'm ssh'ed into PC2, so I do git log -1 on PC2 to verify that the sync worked, then I run the build command. Works perfectly. Give it a shot. See the links below for details.

Note: the cloned repo on PC2 will be on a git branch named somename_SYNC. Modify the script appropriately if you'd like it to have the same branch name instead of always using a "SYNC branch". It is possible to modify the script to get an effect more like Scenario 2 below if desired. Nevertheless, doing Scenario 2 manually isn't hard, so you might just want to continue to do Scenario 2 manually. It's Scenario 1 where the automated script is the most beneficial and time-saving, as it allows an easy and rapid "modify, sync, build" workflow where "modify" takes place on PC1, "sync" is run from PC1 but affects also PC2, and "build" takes place on PC2.

Links:

  1. Full setup and installation instructions here:
    Work on a remote project with Eclipse via SSH
  2. Readme, documentation, & repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/README_git-sync_repo_from_pc1_to_pc2.md.
  3. Exact script in question is here:
    https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/sync_git_repo_from_pc1_to_pc2.sh

Scenario 2: I work (edit code and files) on multiple computers, and want to be able to edit the same code repository from any computer in the world:

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.

  1. Go to https://github.com and create an account and optionally (recommended) set up ssh keys.

  2. Now use their web interface to create a new repository.

    1. As of the year 2020 or earlier, GitHub allows free private repositories too, so this works well even for private, closed-source projects.
  3. Find the new repository ssh or https clone URL. Ex: git@github.com:ElectricRCAircraftGuy/eRCaGuy_dotfiles.git or https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git.

  4. Clone the project onto PC1. Ex:

     git@github.com:ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
    cd eRCaGuy_dotfiles
    
  5. And repeat that exact same clone command on PC2.

  6. Now on PC1, make some changes, commit them, and push them to your "remote" repository on github:

     # edit some files, then do the following
    git add -A  # stage ("add") all changed files to be committed
    git commit  # commit them
    git push    # push them to your remote github repo
    
  7. Now on PC2, pull in your changes:

     # pull all changes from github (which includes the changes
    # you just pushed from PC1) to PC2
    git pull
    
  8. Now you can edit files on PC2, commit them, and push them to github using the commands shown just 2 steps above, and then from PC1 you can run git pull to pull in those changes from PC2.

  9. Keep doing this process, as required, working on PC1 OR PC2, and easily sharing the files and splitting your work between the two computers. Just remember that all your changes must be committed and pushed to github on one PC before you can check them out (pull them) and continue working on the other PC.

  10. If you ever get into a situation where files are a bit out-of-sync between the two PCs, you'll have to maybe use some extra branches, do some merges, resolve conflicts, etc. Then, it becomes more similar to working with a small team where you are all working on the same repo. Google is your friend. Git is very very very powerful, and has a command, set of commands, or workflow for just about everything.