Matthew Hutchinson edited this page Dec 10, 2010 · 4 revisions

Handy tips and tricks on this awesome SCM.

Installing on Leopard

Various installing guides.

Quick tips

# push new branch to remote
git push origin branch_name

# show logs diff between 2.0.2 and HEAD
git log --pretty=oneline 2.0.2..HEAD

# delete remote branch/tag
git push origin :branch_tag_name

# delete local tag
git tag -d tag_name

# delete local branch
git br -d branch_name

# pull remote branch
git co -b rightscale_current_1.0 origin/rightscale_current_1.0

# remote rebase from master (reapply your work on top of the incoming changes , rather than merging)
git pull --rebase origin master

Creating New Repository

on remote use; newgit command from above tutorial which does this;

  if [ -z $1 ]; then
  echo "usage: $FUNCNAME project-name.git"
  mkdir $gitdir
  pushd $gitdir
  chgrp -R gitgroup $gitdir
  git --bare init
  git --bare update-server-info
  touch git-daemon-export-ok

Then locally do;

mkdir repo-name.git

cd repo-name.git
git init
touch .gitignore
git add .gitignore
git commit -m "just gitignore"
git remote add origin ssh://
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git push --all
git pull    

Clone (via ssh) an existing repository

git clone ssh://

Set TextMate as editor

git config --global core.editor "mate -w"

Looking at logs

git log
git log --stat
git log -p  (spacebar to page)
gitk --all &  (launches gitk UI, use File->Update to refresh)

Git tagging

git tag -a -m "tagging version 1.0" 1.0
Use git-push with the --tags option to push them to the remote (github in this case).
git push --tags  

Git Commiting

git commit -v

Branching and Merging (local)

git branch branch_name (creates a new branch)
git co branch_name (switches to branch_name)
git branch (shows current selected branch)
gba (shows all branches and current selected one)
git rebase master (will merge changes in from master to current selected branch)
git diff master human | mate (see diff between human and master branches in text mate)
git merge branch_name (merge branch_name back into current selected branch) - also commits to current branch
git branch -d branch_name (or -D to delete an unmerged branch) (delete a branch)

revert the merge ?

git reset --hard ORIG_HEAD (which returns current selected branch back to state before the merge)

Stashing Branches

  • (see minute 30 in tutorial screencast)
  • In branch with changes you want to stash away without commiting, use git stash "Started some work comment"
  • You can then work away on other branches and master and commit on them without committing whats been stashed
git stash list (shows whats been stashed)
git stash apply (unstash stuff ready to keep working from previously)
git stash clear (delete/remove stash)

Remote Repositories (36:00 in tutorial)

See cat .git/config for location of remote repos or git remote show origin

git push
git pull - will fetch from all branches on remote repos - with a merge of current repos with whats in master
git fetch - just does a fetch - will fetch from all branches on remote repos
pull from another repos - add as a remote source; with ssh url and path to repos eg. to add laptop or another machine; git remote add laptop alderann.local:/somedir/repos_name
then git remote show laptop - will show info about this remote
git fetch laptop (will pull without merge) - all branches from laptop come down
git pull laptop dog_branch - will pull and merge a branch from remote
git push laptop dog_branch - will push from current working branch laptop repos
git push origin dog_branch - will push dog branch to remote origin server as branch

Tracking Branches

To set this dog_branch as a tracking branch - so you can push and pull from it easily on the branch) - then edit .git/config and add new branch config for it;

  [branch "dog_branch"]
  remote = origin
  merge = refs/heads/dog_branch  
  • now - local copy of dog branch is a tracking branch that will push and pull from remote server (nice to do with all local branches, unless purely local work)
  • now - git push and pull inside working dog_branch will just work on this branch - pull will still fetch ALL branches, but will only MERGE inside dog branch
  • OR to do a tracking branch all at once during creating use; git branch --track dog_branch origin/dog_branch

SVN integration

git-svn clone svn+ssh:\\etc.
git-svn dcommit (will  take git commits, package as svn commits and sends to svn)
git needs all svn perl bindings installed (svn from source with perl bindings)
git-svn rebase (will pull from svn repos)

Useful links