git repository sync between computers, when moving around?

前端 未结 8 786
萌比男神i
萌比男神i 2020-12-07 07:18

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 repo

相关标签:
8条回答
  • 2020-12-07 08:09

    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.

    0 讨论(0)
  • 2020-12-07 08:10

    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.

    0 讨论(0)
提交回复
热议问题