Git basics


Published on

A git training I gave at my workplace an year back.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Git basics

  1. 1. GitAn intro to Git Source Control Management
  2. 2. What is Git?Distributed version control systemLinus Torvalds - to track linux kernel developmentFree and open sourceDesigned to handle small/large projects with speed and efficiencyFast branching and merging
  3. 3. Why source control?Faster developmentArchive of all code changes over timeCompare changes and revert to old releaseAccountabilityConserve disk spaceGood for you
  4. 4. Distributed vs Centralized
  5. 5. The Basics
  6. 6. Installing git• To initialize git directory• $ git init• Start version controlling existing files• $ git add *.php *.inc *.html *.tpl• $ git commit -m ‘Initial project version’• Cloning an existing repository• $ git clone ssh://clickindia@
  7. 7. Basic git workflow• You modify files in your working directory• You stage files adding snapshot of them to staging area• You do a commit which takes the files in staging area and stores the snapshot permanently
  8. 8. Changes in repositoryTo check status of your filesModifying filesTracking new filesRemoving filesStaging modified filesCommitting filesIgnoring files
  9. 9. Example of gitignore$ cat .gitignore *.[oa] *~ *.a # no .a files !lib.a # but do track lib.a, even though youre ignoring .a files above /TODO # only ignore the root TODO file, not subdir/TODO build/ # ignore all files in the build/ directory doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
  10. 10. Viewing staged and unstaged changesgit statusgit diff (Compares working directory with staging area)git diff --stagedgit diff HEADgit diff v1.0 v1.1git diff master adposting
  11. 11. Branching$ git add README test.rb LICENSE$ git commit -m initial commit of my project Created on commit Created on add
  12. 12. Branching
  13. 13. Branching
  14. 14. Creating new branch$ git branch testing
  15. 15. Current branch
  16. 16. Switching branch$ git checkout testing
  17. 17. Significance of branch$ vim test.php$ git commit -am “made a change”
  18. 18. Significance of branch$ git checkout master
  19. 19. Significance of branch$ vim test.php$ git commit -am “made a change”
  20. 20. Basic branching and merging• Create a new feature branch• Do some work in that branchCritical issue in production• Revert back to production branch• Create a new branch to add the hotfix• After testing, merge the hotfix branch and push to production• Switch back to your original issue and continue
  21. 21. Basic branching and merging$ git checkout -b iss53Switched to a new branch "iss53"
  22. 22. Basic branching and merging$ vi index.html$ git commit -a -m added a new footer [issue 53]
  23. 23. Basic branching and mergingIssue in production$ git checkout masterSwitched to branch “master”$ git checkout -b ‘hotfix’Switched to a new branch “hotfix”$ vim index.html$ git commit -am ‘fixed a broken email address’[hotfix]: created 3a0874c: "fixed the broken email address" 1 files changed, 0 insertions(+), 1 deletions(-)
  24. 24. Basic branching and merging$ git checkout master$ git merge --no-ff hotfixUpdating f42c576..3a0874c README | 1 - 1 files changed, 0 insertions(+), 1 deletions(-) $ git branch -d hotfix Deleted branch hotfix (3a0874c).
  25. 25. Basic branching and merging$ git checkout iss53Switched to branch "iss53"$ vim index.html$ git commit -a -m finished the new footer [issue 53][iss53]: created ad82d7a: "finished the new footer [issue 53]" 1 files changed, 1 insertions(+), 0 deletions(-)
  26. 26. Basic branching and merging$ git checkout master$ git merge iss53Merge made by recursive. README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  27. 27. Basic branching and merging$ git branch -d iss53
  28. 28. Branch management$ git branch # Shows all the active branches$ git branch -v # Shows last commit on each branch$ git branch --merged # Shows which branches are merged with the branch you are on$ git branch --no-merged # Shows all branches that contain work you haven’t merged$ git branch -d testing # To delete a branch. Will fail if the branch is not yet merged
  29. 29. Remote branches• Remote branches are references to the state of branches on your remote repositories
  30. 30. Remote branches
  31. 31. Remote branches$ git fetch origin # Fetches any data you don’t have from remote ‘origin’ repository$ git fetch origin master # Fetches data only from ‘master’ branch$ git pull origin # Fetches any data you don’t have and merges it with local branch$ git pull origin master # Fetches data from master and merges it with local master branch$ git push origin develop # Pushes data from local ‘develop’ branch to remote ‘develop’ branch$ git push origin develop:release # Pushes data from local ‘develop’ branch to remote ‘release’ branch git fetch origin
  32. 32. Remotes$ git remoteOrigin$ git remote -vOrigin ssh://clickindia@$ git remote add test ssh://$ git remote rm origin
  33. 33. Gist of git
  34. 34. Git branching model
  35. 35. How will we work? pull master master master pull develop push release Development Test server Production 1 Developer 2 Developer 3
  36. 36. Tagging and releaseOnce the code is merged into master production $ git tag -a v1.0.1 # applies v1.0.1 tag to current commit $ git tag # To list all tags $ git tag -1 ‘v1.4.2*‘ # List all tags starting with 1.4.2 $ git tag -a v1.0.1 -m ‘my version 1.0.1’ $ git show v1.4 tag v1.4 Tagger: Scott Chacon <> Date: Mon Feb 9 14:45:11 2009 -0800 my version 1.4 commit 15027957951b64cf874c3557a0f3547bd83b3ff6 Merge: 4a447f7... a6b4c97... Author: Scott Chacon <> Date: Sun Feb 8 19:02:46 2009 -0800 Merge branch experiment
  37. 37. TaggingTagging later $ git log --pretty=oneline 15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch experiment a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support 0d52aaab4479697da7686c15f77a3d64d9165190 one more thing 6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch experiment 0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function 4682c3261057305bdd616e23b64b0857d832627b added a todo file 166ae0c4d3f420721acbb115cc33848dfcc2121a started write support 9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile 964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo 8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme $ git tag -a v1.2 9fceb02
  38. 38. Tagging
  39. 39. Everyday commandsWorking on a new feature$ git pull origin develop$ git checkout -b adposting.....did development work.....$ git commit -am ‘Created new ad posting form’$ git push origin adposting......tested feature on development server.....$ git checkout develop$ git merge --no-ff adposting$ git push origin develop:release-v1.1.2 (push to test server as release-v1.1.2.........fix testing bugs on release v1.1.2.......$ git fetch origin release-v1.1.2 ( on production only done by sysadmin )$ git checkout master$ git merge --no-ff release-v.1.1.2$ git tag -am v1.1.2 ‘released version v1.1.2’
  40. 40. Everyday commandsWorking on a hotfix$ git pull origin master$ git checkout -b hotfixv1.1.1 master.....fixed bug on test server.....$ git commit -am ‘fixed e-mail address on contact page’....... Tested by deepak ........$ git fetch origin hotfixv1.1.1 ( on production only done by sysadmin )...... Check if already on master....if not $ git checkout master$ git merge --no-ff hotfixv1.1.1$ git tag -am v1.1.1 ‘released version v1.1.1’$ git pull origin master ( on test and development servers)
  41. 41. Version numbering v0.0.0Major release Hot fixes Minor releases
  42. 42. References• -- a complete book on git• -- A very simple reference of all commands