From popdata
Jump to: navigation, search

Using GIT


PDS/Portal Use:

  • Allow group write (see below for Permissions discussion)
 umask 002
  • update production (Sullivan) from the current master repo on Thompson2 (here nicknamed thompson):
    git fetch --dry-run thompson master [avoid surprises, see what's waiting on Thompson2]
    git pull --stat thompson master
    Note: option "--stat" gives for each file the number of changes made; "--dry-run" does nothing except list changes.

after changing the live production

  • check it in locally. "git diff" will list the files waiting to be committed. "git status" will list several categories.
git add
git diff --name-only --cached
git commit
  • then push it to master repo (if needed)
git push thompson
  • then on development server pull from master repository (if needed)
git pull origin master

After changing files on development Thompson2 (NORMAL)

  • check in changes locally
git add
git diff --name-only --cached
git commit
  • then push it to master repo on Thompson2
git push origin master
  • if needed update production as above on Sullivan Requires Thompson2 passphrase.
git fetch --dry-run thompson master
git pull --stat thompson master

Tracking changes, coping with errors

  • List recent commits
    git log --stat | head -100
    Omit "--stat" to see only the snapshot reference hash, author, date and comment. "--stat" adds a list of files and number of lines changed.
    "--names-only" lists all changed files without the stats.
  • List commits not yet pushed to repository
    git push --dry-run origin master
    Sample Output: e14874b..b1fb33e master -> master
    The second commit hash is the one waiting to be pushed. Use "git show --stat" to find out which files were changed.
  • git show --stat b1fb33e | head
  • List files added but not yet committed
  • git diff --name-only --cached | xargs ls -ld

Coping with errors

  • If error because of local changes to FILE are at risk of being overwritten, and error is
    error: Your local changes to the following files would be overwritten by merge
  • then overwrite local file from local GIT repository "git checkout FILE" (after making a backup copy).
  • Find commit hash for previous version of FILE
    git log FILE | head -12
  • Overwrite FILE with version from specified commit hash HASH
    git diff HASH FILE
    git checkout HASH FILE

Undo "git add"

  • List all files staged for commit by "git add"
    git diff --name-only --cached
  • Undo "git add FILE"
    git reset FILE"

Emacs magit utility

  • emacs command magit-status will run "git status" in current directory (or ask for a directory with a git repository)
  • select a file and press TAB to show/hide details; "+" / "-" to show more/fewer diff context lines.
  • Section "Staged changes" for files previously "add"ed for next "commit".
  • Section "Untracked files": "s" to add current file to staging area.
  • Section "Changes" or "Unstaged Changes": tracked files that are modified, but not queued 9staged) for commit. "s" to stage; "u" to unstage. Can select a particular "hunk" within a diff (as revealed by TAB key),
  • To commit to local GIT archive, press "c", and you will be offered a buffer to enter description of changes.
  • To push to other GIT server, "P P"
  • To fetch from other GIT server, "f f"
  • To pull, "F F"
  • To list history log: "l l"

For help, "info magit" (^H i m magit)

Initial Setup

  • On sullivan first set up a git repository:
cd /home/www/pds
git init
git add .
git commit
  • set up the master repo on thompson
mkdir /home/src/pds.git
cd /home/src/pds.git
git init --bare
  • push it all to the repo from sullivan:
git push thompson2:/home/src/pds.git master
  • tell sullivan that it is a remote from thompson edit sullivan:/home/www/pds/.git/config add:
[remote "thompson"]
        url = thompson2:/home/src/pds.git
        fetch = +refs/heads/*:refs/remotes/thompson/*
  • create development branch on thompson
mkdir /home/www/pds
cd /home/www/pds
git clone /home/src/pds.git .


All directories used by git (for commit, /home/src/pds.git/ ; for push: /home/src/pds.git/) need to be writeable by group "pds".

  • Sample error on commit: error: insufficient permission for adding an object to repository database .git/objects
  • Sample error on push or pull: error: failed to push some refs to '/home/src/pds.git'
  • FIX:
    • in ~/.profile or before "git": umask 002
    • sudo find /home/www/pds/.git /home/src/pds.git -type d -not -perm -20 -ls -exec chmod g+w \{} \;


  • interactive add: git add -i
  • interactive with new files too: git add -A -i
  • difference between remote commit and local files on Sullivan:
    Note that "git pull thompson master" is a combination of "fetch" followed by "merge".
    • "git fetch thompson" will pull commit from thompson2 via git-over-ssh. Then "git diff --stat ..thompson/master" will list changed files. Finally "git merge thompson/master" will update local files on Sullivan using the commit previously fetched.
  • Coping with conflicting change on both development (Thompson2 = and production (Sullivan =
    • After doing on Thompson2 "commit" and "push", then on Sullivan "pull", get message that "pull would overwrite local changes".
    • On Sullivan do "git stash" (This creates a temporary "commit" snapshot of all files; see man page "git-stash"). Then "git pull thompson master" (this copies changes previously pushed onto Thompson2 repository, and applies them to working files on Sullivan).
    • If all you wanted to do is force changes on Thompson2 to replace files on Sullivan, you're donw; you should do "git stash pop" to delete the temporary copy. Alternately ou could then do "git stash pop" to re-apply the Sullivan files on top of the Thompson2 versions, but probably you don't want to do that.