31

I have local copies of a GitHub repo on Laptop and Desktop. The Desktop is ahead of the Laptop and the remote GitHub origin. I want to pull changes onto the Laptop, but don't want to push to the public origin. How do I set up a USB stick/external HDD as a remote?

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • 2
    There's an extensive guide in the Git Wikibook: https://en.wikibooks.org/wiki/Git/Repository_on_a_USB_stick – RAM Sep 19 '18 at 16:05

1 Answers1

44

Plug the USB drive into Desktop, and assuming it's showing up as J:

  1. Initialise a bare repo that will act as the remote:

    git init --bare J:\repo_name
    
  2. cd to the local repo and:

    git remote add usb J:\repo_name
    git checkout master
    git push usb master
    

The master branch is synced with the usb remote. Now plug the USB drive into Laptop, and assuming it's showing up as D:

git remote add usb D:\repo_name
git checkout master
git pull usb master

If you're trying to pull a branch that doesn't exist on Laptop but does on Desktop, you can just do git checkout the_branch and it will automatically pull it from usb (unless the_branch also exists in origin, in which case you have to do git checkout -b the_branch usb\the_branch)

You might have to git fetch if it doesn't find the remote usb branch.

If, later, you plug in the USB drive and it shows up as a different letter, e.g., K:, then do:

git remote set-url usb K:\repo_name
binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • Or from the USB drive (after `cd D:\ `), just `git pull file:///c:/src` to bypass the remote add step (I had to do this for multiple repos, and thus one less transient step). – Dwayne Robinson Nov 01 '21 at 07:47
  • If you are using Git 2.27.0+, you can use `git switch master` instead of `checkout`, which is generally easier and preferred (see [git-switch](https://git-scm.com/docs/git-switch)) – Chris Collett Mar 23 '23 at 19:41