git repository sync between computers, when moving around?

GitVersion Control

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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 [email protected]
$ 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 [email protected]:/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 [email protected]:/home/username/git/newThing
$ git remote add externalName [email protected]:/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.-

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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

  1. Full setup and installation instructions here:
    https://stackoverflow.com/questions/4216822/work-on-a-remote-project-with-eclipse-via-ssh/60315754#60315754
  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: [email protected]:ElectricRCAircraftGuy/eRCaGuy_dotfiles.git or https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git.

  4. Clone the project onto PC1. Ex:

     [email protected]: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.

Solution 6 - Git

How about simply using rsync?

Solution 7 - Git

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.

Solution 8 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohanView Question on Stackoverflow
Solution 1 - GitKingCrunchView Answer on Stackoverflow
Solution 2 - GitXANiView Answer on Stackoverflow
Solution 3 - GitndimView Answer on Stackoverflow
Solution 4 - GitCollege StudentView Answer on Stackoverflow
Solution 5 - GitGabriel StaplesView Answer on Stackoverflow
Solution 6 - GitseriyPSView Answer on Stackoverflow
Solution 7 - GitmipadiView Answer on Stackoverflow
Solution 8 - GitKerhongView Answer on Stackoverflow