22
Git In a Nutshell By: Pranesh Vittal CG http://in.linkedin.com/in/praneshvittal

Git in a nutshell

Embed Size (px)

Citation preview

Page 1: Git in a nutshell

Git  In  a  Nutshell  

By: Pranesh Vittal CG http://in.linkedin.com/in/praneshvittal

Page 2: Git in a nutshell

Key  takeaways  from  this  session  

•  Differences  between  Centralized  &  Distributed  Version  Control  System.  

•  Unlearning  some  of  the  concepts  from  CVS  /  SVN  World.  •  What  is  Git?  •  Git  IniEalizaEon  •  Git  Config  /  Git  Ignore  •  Most  Frequently  Used  Git  Commands  •  Merging  Conflicts  •  Git  Mergetool  •  Git  Help  •  Git  CollaboraEon  Methods  •  What  is  a  PR  and  the  steps  associated  with  it?  

Page 3: Git in a nutshell

What  is  SCM  ???  

SCM  -­‐>  Source  Control  Management    VCS  -­‐>  Version  Control  System    Centralized  VCS  

•  CVS,  Perforce,  SVN    Distributed  VCS  

•  Git,  Mercurial    

Page 4: Git in a nutshell

Unlearning  CVS  &  SVN  

git  equivalent  for  “svn  checkout  url”  is  “git  clone  url”  git  equivalent  for  “svn  update”  is  “git  pull”  git  equivalent  for  “svn  commit”  is  “git  commit  -­‐a  &&  git  push”    Some  of  the  features  available  in  Git:    •  Cheap  &  Easy  Branching.  •  Disconnected  Use  wherein  the  user  is  not  connected  to  

the  Network.  •  Staging  just  what  is  required  for  a  parEcular  feature.  •  BeYer  collaboraEon  with  other  developers.  

Page 5: Git in a nutshell

What  is  Git  ???  

•  Distributed  VCS  •  Everything  is  check-­‐summed  (40  characters  sha-­‐1  hash.  Hexadecimal  unique  value  

calculated  based  on  author’s  name  &  email-­‐id,  contents  of  the  file  &  few  other  parameters).  

•  Snapshots  and  not  differences.  •  Everything  related  to  the  git  project  can  be  found  in  .git  directory  at  the  root  level  of  

the  project.      

Page 6: Git in a nutshell

What  is  Git  ???  

•  Nearly  Every  AcEon  Is  Local.  •  Consists  of  following  areas:  

–  Working  Directory  –  Staging  Area  –  Git  Repository  

 

Page 7: Git in a nutshell

In  a  nut  shell  

Page 8: Git in a nutshell

Git  Config  

•  git  config  -­‐-­‐global  user.name  “Pranesh  ViYal”  •  git  config  -­‐-­‐global  user.email  “[email protected]”  

 OR    

•  ~/.gitconfig  [user]  

 name  =  Pranesh  ViYal    email  =  [email protected]  

[credenEal]    helper  =  cache  -­‐-­‐Emeout=3600  

[core]    editor  =  vim  

[merge]    tool  =  vimdiff  

[alias]    b  =  branch  

 

•  More  such  examples  :  hYps://gist.github.com/pksunkara/988716    

 

Page 9: Git in a nutshell

.giAgnore  

#  Compiled  source  #  *.com  *.class  *.dll  *.exe  *.o#  Logs  and  databases  #  *.log  *.sql  *.sqlite      #  OS  generated  files  #  .DS_Store*  ehthumbs.db  Icon?  Thumbs.db        

*.so      #  Temporary  files  #  *.swp  *.swo  *~      #  Packages  #  *.7z  *.dmg  *.gz  *.iso  *.jar  *.rar  *.tar  *.zip        

https://github.com/sethvargo/chefspec/blob/master/.gitignore

Page 10: Git in a nutshell

Most  Frequently  Used  OperaAons  

init  add  branch  commit  checkout  diff  fetch  log  merge  

prune  push  pull  rebase  reset  remote  status  stash  show  tag  

Page 11: Git in a nutshell

Most  Frequently  Used  Commands  

•  git  init  (Arer  creaEng  the  directory  and  cd  into  into  it).  •  git  clone  <git-­‐repo>  •  git  add  .  (Add  untracked  files)  •  git  status  (Current  status  of  the  local  git  directory).  •  git  commit  -­‐m  “First  Commit  of  the  file.”  (Commit  the  files).  

•  git  push  <remote-­‐short-­‐name>  <branch-­‐name>  (Push  the  changes  to  the  repository).  

•  git  log  (Log  of  checkins  performed  on  the  branch).  •  git  log  -­‐p  -­‐2  (Details  of  the  last  2  commits).  •  git  log  -­‐-­‐preYy=oneline  (Log  info  in  oneline  format).    *remote-­‐short-­‐name  -­‐  default  value  is  origin      

Page 12: Git in a nutshell

Most  Frequently  Used  Commands  •  git  diff  <old-­‐tag>  <new-­‐tag>  (Show  changes  between  commits,  

commit  and  working  tree  etc...)  •  git  diff  -­‐-­‐cached  (Diff  between  the  working  and  the  staged  

area)  •  git  diff  HEAD  (Diff  between  the  working  and  the  repository)  •  git  rm  -­‐f  <filename>  (Delete  the  file  from  git).  •  git  reset  HEAD  <file-­‐name>  (To  reset  a  file  that  has  been  

staged  but  not  checked-­‐in).  •  git  checkout  <file-­‐name>  (To  reset  a  file  that  has  been  been  

modified  in  working  area).  •  git  stash  (Temporary  check-­‐in  while  switching  between  

branches).  •  git  stash  list  (List  of  stash  items).  •  git  stash  apply  @stash@{n}  (Add  the  stashed  items  back  to  the  

branch).      

Page 13: Git in a nutshell

Most  Frequently  Used  Commands  •  git  branch  (List  the  branches  on  the  local  repository.  Can  be  found  

in  .git/refs/heads/).  •  git  branch  new-­‐branch  (Create  new  branch).  •  git  checkout  new-­‐branch  (Switch  to  the  new  branch).  •  git  checkout  -­‐b  <new-­‐branch>  <remote-­‐short-­‐name>/<branch-­‐

name>  (To  check  out  the  new  branch  from  the  repository).    •  git  diff  (Diff  of  the  current  state  with  the  repository).  •  git  tag  <tag-­‐name>  (To  create  a  tag).  •  git  checkout  -­‐-­‐track  <remote>/<branch>  

•  git  branch  -­‐r  (List  the  branches  on  the  remote  repository.  Can  be  found  in  .git/refs/remotes/).  

•  git  branch  -­‐v  (List  out  the  recent  commits  performed  in  all  the  branches)  

Page 14: Git in a nutshell

Most  Frequently  Used  Commands  

•  git  remote  (Display  list  of  remote  repositories).  •  git  remote  add  <remote-­‐short-­‐name>  (To  create  a  remote  

repository).  •  git  remote  fetch  <remote-­‐short-­‐name>  (To  fetch  the  changes  from  

other  repository.)  •  git  fetch  <remote-­‐short-­‐name>  (To  get  files  from  remote  projects).  •  git  pull  <remote-­‐short-­‐name>  <branch-­‐name>  (AutomaEcally  fetch  

and  then  merge  a  remote  branch  into  your  current  branch).  •  git  merge  <branch-­‐to-­‐be-­‐merged>  (To  merge  the  changes.  Before  

execuEng  this,  make  sure  that  you  are  in  the  target  branch).  •  git  rebase  <branch-­‐name>  (FuncEons  very  similar  to  merge,  but  good  

at  keeping  the  history  of  changes  in  a  clean  way.)  •  git  branch  -­‐d  <branch-­‐name>  •  git  reset  -­‐-­‐hard  HEAD  (When  SHIT  happens  and  you  messed  with  too  

many  merges  and  you  don’t  care  about  the  changes  you  did).    

Page 15: Git in a nutshell

Merging  Conflicts  

git  merge  <branch-­‐to-­‐be-­‐merged>  Auto-­‐merging  index.html  CONFLICT  (content):  Merge  conflict  in  index.html  AutomaEc  merge  failed;  fix  conflicts  and  then  commit  the  result.    

git  status  You  have  unmerged  paths.    <<<<<<<  HEAD:index.html  <div  id="footer">contact  :  [email protected]</div>  =======  <div  id="footer">    please  contact  us  at  [email protected]  </div>  >>>>>>>  iss53:index.html    

git  mergetool:    

Page 16: Git in a nutshell

Git  mergetool  

Page 17: Git in a nutshell

Help  !!!  

git  help  <command>  git  help  commit  git  help  branch  git  help    

Page 18: Git in a nutshell

Git  Pull  Requests  

What  is  a  pull  request?    Most  important  step  wherein  a  collaborator  sends  across  his  changes  to  be  merged  into  the  master  branch.  The  changes  will  be  pulled  by  the  commiYer  and  merged  into  the  master.    

Page 19: Git in a nutshell

Popular  CollaboraAve  Development  Methods  

•  Fork  &  Pull  (Followed  in  most  of  the  open  source  projects  on  GitHub).  

•  Shared  Repository  Model  (The  one  followed  in  in  house  projects)  

Page 20: Git in a nutshell

Some  of  the  popular  models  

Page 21: Git in a nutshell

References  

hYp://nvie.com/posts/a-­‐successful-­‐git-­‐branching-­‐model/    hYp://scoYchacon.com/2011/08/31/github-­‐flow.html    hYp://git-­‐scm.com/doc    hYp://cleanercode.com/introducEon-­‐to-­‐git-­‐talk/introducEon-­‐to-­‐git.pdf    hYp://nvie.com/files/Git-­‐branching-­‐model.pdf    

Page 22: Git in a nutshell

Q & A