Wednesday, 5 September 2012

An Intro to Git

Heres are some resources which helped me get started with Git. Should help point new comers in the right direction.

Why should you use Git?
Heres a tech talk from one of the creators of Git, Linus Torvalds on why you should use Git.
http://www.youtube.com/watch?v=4XpnKHJAok8

How to use Git?
Possibly the best and thorough introduction to Git that I have found, Its a brilliant starting point.
http://www-cs-students.stanford.edu/~blynn/gitmagic/ch01.html

Try Git
Interactive web based Git Shell
http://try.github.com/levels/1/challenges/1/

Below are my personal git notes.

Git config
git --global user.name "Riz" //set global username
git --global user.email ="riz@x.com" //set global email

leave out --global to add user name local to current repo

git config --global -e //edit config in vim
git config -e          //edit local config


git config -l //view all config

--------------------------------------------------------------------------------
Basic commands
git init                //makes a local repo

git add .               //recursively add files to repo
git add -u              // untrack deleted
git status              //list file statuses
git commit -m "Message" //apply the commit
git commit -a           //commit with changes in tracked files
git commit --append     //replace last commit 
git show                //show diff

git log       //show log
git clone git://some.url.com/dir/file.git  //clone (checkout) a repo 
git clone git://some.url.com/dir/file.git newName  //clone repo and give new name 

--------------------------------------------------------------------------------
Branches and Tags

git checkout -b {branchname} //create a new branch
git checkout -b fixes 1b6d   //create branch called fixes from commit sha 
git checkout {branchname}    //switch to branch

git merge {branchname}   //merge some branch into current working director

git branch -d {branchName} //delete branch if merged
git branch -D {branchName} //delete branch, ignore merge status

git branch      //list branches
git tag                     //list branches

git tag -l {tagName}         //create

Revert changes on branch
git reset --hard HEAD

git reset --hard HEAD~2 //revert back 2 versions

git reset HEAD          //unstage staged files

--------------------------------------------------------------------------------
Alias 

make alias command called unstage for unstaging

git config --global alias.unstage "reset HEAD"

use the alias
git unstage lib/foo.rb 

--------------------------------------------------------------------------------
Stash

git stash save  stashname  //store changes on stash stack 
git stash list         //list all stashes
git stash apply stashname  //apply stash (entry is not removed) 
git stash drop  stashname  //remove entry from stash list
git stash pop      //appy top of stash stack and remove entry 
git stash clear            //empty stash
git stash show stashname   //show stash

stash {apply | drop } also accept stack position. where 0 is always top of stack
ie:
git stash apply stash@{0}

--------------------------------------------------------------------------------
Remote

//set up remote repository
git remote [nameAlias] pat

  eg:
   git remote add github git@github.com:rizvn/ValidationHelper.git


git push -u origin master     //push to remote

git pull {repo} {remotebranchname}:{localbranchname} //pull from remote
    
    eg: git pull origin master:localBranch

if local branch is not named then will pull the changes into current
working branch


--------------------------------------------------------------------------------
Remove tracked files or folders from git repo

git rm -r --cached {folder or filename} 


--------------------------------------------------------------------------------
Revert a single file
git checkout -- fileName

the -- is not a typo


--------------------------------------------------------------------------------
Git log format 

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --

or make an alias

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"

usage: 
  git lg
  git lg -p //to see lines that changes
  
source: http://coderwall.com/p/euwpig?i=3&p=1&t=git

--------------------------------------------------------------------------------
Setting line endings 

Set the configuration variable core.autocrlf to false

source: 
http://stackoverflow.com/questions/783906/git-under-windows-msys-or-cygwin

--------------------------------------------------------------------------------
Git reflog (used for recovery)

git reflog //will list when the tip was change 
  //use --all for more detailed view including stash changes


--------------------------------------------------------------------------------
Git clean up 

git reflog expire --expire=1.minute refs/head/master
git fsck --unreachable
git prune
git gc

No comments:

Post a Comment