Git Survival Guide

Ted Felix has a very nice guide about Conflict Resolution in Git.

Revert uncommited changes

Revert changes to last commit:

git reset --hard
git clean -fd     # '-f' = force, '-d' = remove directories

Update local branch with remote

git checkout branch-to-update
git pull origin branch-to-update

Delete branch

git push origin :card-XXXX    # delete on remote
git branch -D card-XXXX       # delete sandbox branch

Fetch and track branch from Dingobats (dingobat) repository

git remote add dingobat git@github.com:dingobat/eSignatur.git
git fetch dingobat
git checkout card-XXXX        # card-XXXX is now available to you

Switching between branches with uncommitted changes

git stash                   # save changes in this card
git checkout card-XXXX      # new card
.
do work
.
commit
git stash pop               # fetch previously stashed

Stash works independently from branches. However when you pop the stash, whatever popped must be able to merge.

If sandbox copy is f*cked up

git checkout master
git branch -D master
git checkout -b master --track origin/master
git checkout card-XXXX
git rebase -i master
.
.
.
git push -f origin card-XXXX

Ignore local files

echo "UpgradeLog*" >> .gitignore
echo "_UpgradeReport_Files/" >> .gitignore
echo ".gitignore" >> .gitignore
git update-index --assume-unchanged .gitignore

New branch

git checkout -b card-XXXX

If the new branch is dependent on other branch:

git checkout card-YYYY             # parent branch
git rebase master
git push -f origin card-YYYY       # card-YYY contains commits from master
git checkout -b card-XXXX          # new branch

git rebase -i master and remove unwanted commits in card-XXXX from master. Use gitk to verify the commit chain.

Push to master

git push -f origin card-XXXX       # omit -f on first push

See previous version of file

git show HEAD~1:./Index.cshmtl

The number (~1) after HEAD denotes which version. The file path after : must be full. However, to see version of a file in current directory full path is ./

Untrack file

git update-index --assume-unchanged [path] ...
git update-index --no-assume-unchanged [path]        # To start tracking the file again

Update from master

git checkout master                        # (if in branch)
git pull --ff-only rovsing master
git push origin master

Maintenance

git gc         # do once in a while (happens automatically in git gui)
git fsck       # lengthy process

Change commits

git checkout master
git pull
git checkout card-XXXX
git rebase -i master

Change the status from pick to edit on the commits you want to change.

do {
  git gui                        # unstage or commit what needs to be changed
  git rebase --continue
} while (commits_present)
git push -f origin card-XXXX

Use multiple SSH keys

  1. ssh-keygen, give a name which doesn't clash with whatever identities you have in .ssh. Move the keypair to ~/.ssh.
  2. Add the public key to your git account.
  3. Add to ~/.ssh/config:
    Host git-for-other
    HostName github.com
    User git
    IdentityFile "/path/to/.ssh/other-identity-key"
    IdentitiesOnly yes
  4. Remove existing origin from repository:
    cd $REPOSITORY
    git remote remove origin
  5. Add new origin:
    git remote add origin git@git-for-other:git-user/Repository.git

Installation on Windows

Install the Git SDK version. Then, after installation (in the prompt that opens):

pacman -S --needed --force rsync

(For rsync support :-)

Another way to add rsync in git bash

  1. Download latest rsync from http://repo.msys2.org/msys/x86_64/
  2. Open git bash, extract rsync archive with: xz -d rsync-x.y.z-x86_64.pkg.tar.xz
  3. Go to the root of your Git for Windows installation, mine is in /c/opt/git-sdk-64: cd /c/opt/git-sdk-64
  4. Extract the rsync archive: tar xf /c/Users/brian/Desktop/rsync-x.y.z-x86_64 --exclude=".*" - substitute the path with the correct path to your download.
  5. rsync is now installed. Enjoy!

Remote deploy website

Create the following on webserver:

cd $HOME
mkdir GitWebs.git
cd GitWebs.git
git init --bare

Copy this script to hooks/post-receive:

#!/bin/bash
while read old new ref; do
	echo "$ref" | grep "/master$"
	if test $? -eq 0; then
		echo "Deploying master branch to production ..."
		git --work-tree="/home/gitwebs" --git-dir="/home/bs/GitWebs.git" checkout -f
	else
		echo "Not deploying $ref - only master will be deployed!"
	fi
done
exit 0

Create Webs deployment directory (mkdir /home/gitwebs) and configure Apache to serve sites out of this directory.

On development pc, checkout the raw Webs.git (git clone bs@xxx:Webs.git) directory. Populate this with the content to be served.

Add remote: git remote add prod bs@${server}:/home/bs/GitWebs.git

To deploy: git push origin prod