3

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.

Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
  • For **Part B**, I'm guessing just cloning to the harddrive would be fine, and then periodically running `git pull` _from_ the harddrive to get recent changes? – drzaus Aug 07 '13 at 03:32
  • I'm going to look more at this post http://swoes.blogspot.com/2009/02/setting-up-git-offline-work-via-usb.html – drzaus Aug 07 '13 at 11:45
  • Points for trying to distinguish your question. – sshine Sep 09 '15 at 07:00

2 Answers2

2

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
Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
1

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.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • my point about 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 need the files available on the portable device, not packed away. I'll clarify my question. – drzaus Aug 07 '13 at 11:50
  • @drzaus I have updated my answer in order to address your last point. – VonC Aug 07 '13 at 12:00
  • so basically it's like a regular git repo _(project/ -> [.git/ + "working files"])_, but the `.git` folder is the bare repo in one folder, and the working files are post-hooked to another non-git folder _([project.git/ + project.working/])_? – drzaus Aug 08 '13 at 15:21
  • @drzaus yes, you have two folders on that USB key: one is the bare repo, the other is a working tree updated by the post-receive hook from the bare repo. – VonC Aug 08 '13 at 15:22
  • thanks for the explanation. just to explicitly confirm, the working tree folder is either: 1) a git repo that can commit back to the bare, or 2) just flat files for viewing only? if #1, isn't that duplicating the .git database? if #2, can i make changes on a non-git computer and commit them back into version control? – drzaus Aug 08 '13 at 15:28
  • 1
    @drzaus 2): you checkout from the bare repo into another folder: no duplication. This is what the command (from [this question](http://stackoverflow.com/a/16077399/6309)) `git --git-dir=/path/to/project_root.git --work-tree=/path/to/your/workingtree checkout -f` does: only *one* repo involved here. No duplication. – VonC Aug 08 '13 at 15:38
  • So I think I figured out both scenarios #amiright @VonC? http://stackoverflow.com/a/20132742/1037948 – drzaus Nov 21 '13 at 21:54