Question

Similar to:

I'm trying to figure out the workflow steps to accomplish the following:

  1. Having worked locally on "home", I want to start a repository in W:\DEV\proj1
    • git init W:\DEV\proj1
    • cd W:\DEV\proj1
    • git add *
    • git commit -m"1st home"
  2. I then want to clone this repo to "portable" somewhere else (i.e. a usbkey), lets say P:\DEV\roam1
    • git clone . P:\DEV\roam1
  3. Then I want to be able to do work in either location ("home" or "portable") and sync changes back and forth.
    • (in portable)
      • // new file f1.txt
      • git add *
      • git commit -m"1st portable"
      • git ??? -- sync f1.txt > "home"?
    • (in home)
      • // new file f2.txt
      • git add *
      • git commit -m"2nd home"
      • git ??? -- sync f2.txt > "portable"
    • repeat

Part A) I think I understand how to clone and sync to a "centralized hub", i.e. github or putting a bare repo on a usb stick and cloning from it whenever I'm at a new location, but I'd rather not have to clone from the portable repo every time I want to get work done in a new place. Also, in the case where I just want to look at the file on a computer that doesn't have git installed.

Part B) Another applicable scenario is that I want to use git to basically backup a directory to an external harddrive (which pushing to a bare repo would normally be fine) but I want to access the files on the harddrive on another computer without git installed.

Was it helpful?

Solution 2

but I'd rather not have to clone from the portable repo every time I want to get work done.

You won't have to, except to initialize your repo in a new location (in which case, you would clone the bare repo of your usb stick on your local environment)

Every time you want to get work done, you would:

  • make sure the remote named 'origin' of your local repo points to the bare repo on the usb stick
  • git pull (or git pull --rebase) in order to get potential changes from usb back to local
  • work
  • git push (back to the usb key)

You need some kind of "centralized/portable" repo to pull from/push to.


not wanting the "centralized hub" to be a bare repo is that, let's say I go another computer without git and I want to just show someone a file

I would still recommend a bare repo on the usb stick, but I would add a post-receive hook on that bare repo, in order to update a separate working tree (still on the usb stick)

See "Want to setup a hook that copies committed files to a particular folder" as an example of such a hook.

That way, I always have, on my "centralized and portable" git repo hosting environment (ie the usb key!):

  • a bare repo (I can clone/pull from/push to)
  • a full working tree, up-to-date with the latest commits.

OTHER TIPS

Concrete example based on @VonC's answer.

DEVICES

  • LOCAL = your local machine, i.e. C:\MyDocuments\Whatever
  • PORTABLE = something else, like a USB key

REPOS

  • LOCAL/myproject/ = the "day-to-day" repo where you do your work, push to github, etc
  • PORTABL/myproject.git = the bare "centralized hub" (on the usb key)
  • PORTABLE/myproject-preview = a non-git folder containing the latest code from the repo
  • PORTABLE/myproject-working = a git repo you can work from when not at home (basically the same as LOCAL/myproject

File Structure

NOTE: I'm testing this all within a single folder on my computer rather than actual separate drives, YMMV

-LOCAL/
   -myproject/
      -.git/
      - other files

-PORTABLE/
   -myproject.git/
       -hooks/
       -info/
       <etc>
   -myproject-preview/
      - other files
   -myproject-working/
      -.git
      - other files

CONFIG

approx commands...assumes you were working locally first before this brainstorm

# start at home
cd LOCAL
git init myproject
<do some work>
# suddenly you realize you want a portable hub
cd PORTABLE
# ready the dump locations (depending on what you want)
mkdir myproject-preview
mkdir myproject-working
# start the hub
git init myproject.git --bare
<make the post-receive hook, see below *not cool enough to do it from the command line>
# backup home
cd LOCAL/myproject
git remote add origin PORTABLE/myproject.git
git push origin master #this shows up in ...preview and ...working
<do more work>

Post-Receive Hook

Lovingly copied from @VonC's other answer and a random coderwall.

You can do both, or choose just "preview mode" or "portable work".

Note the use of relative paths (since we're in PORTABLE/myproject.git/hooks/).

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # preview mode
        git --git-dir=../myproject.git --work-tree=../myproject-preview checkout -f
        # portable working mode (https://coderwall.com/p/oj5smw)
        GIT_WORK_TREE=../myproject-portable git checkout -f $branch
    fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top