Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Git/Github - A beginner's guide


Published on

Introduction to Git/Github - A beginner's guide

Installing Git
Introduction to Version Control
Git Basics
Creating a new local Git repository
Cloning a Git repository
Making use of Git commit history
Reverting files to previous states
Creating a Github Repository
Adding, Committing & Pushing changes
Merging Branches
Sending Pull Requests
Conflict Resolution

and 3 Exercises

Published in: Software, Education
  • Login to see the comments

Introduction to Git/Github - A beginner's guide

  3. 3. '1. -- J‘ - J; -""“v‘}'. » K " ta 2 ' . . V‘ . 1», C5 ‘ :7-‘ ‘. _4 , ;-‘. ~ _ -, ‘V'_‘ A, 1. Installing Git 2. Introduction to Version Control 3. Git Basics :0? AGENDA ('SL' . (‘l. .l'l’P Stmlvnt ('|1upu-r
  4. 4. Installing Git Introduction to Version Control Git Basics Creating a new local Git repository Cloning a Git repository Making use of Git commit history Reverting files to previous states 9950:" >'. °°5"'. 'P‘ N(‘SL7 . i(Ilfi_| 'l'| ’ Stmlvnr ('|1uptc-r
  5. 5. WORKGHOP AGENDA Installing Git Introduction to Version Control Git Basics Creating a new local Git repository Cloning a Git repository Making use of Git commit history 9°55.“ v-I‘- -I C3 ‘ox Reverting tiles to previous states Creating a Github Repository Adding, Committing & Pushing changes 10. Branching 1 1. Merging Branches 12. Sending Pull Requests 13. Conflict Resolution 9° 5° 5. ' .1‘ . ('. 'l' , ("l , |’I'l’ Silltivilt ('l1;l[)I('I‘ and L5 EXCICISCSI H
  6. 6. ( ’. INSTALLING GIT Windows: git—scm. com/ download/ win Mac: git—scm. com/ mac Linux: sudo apt-get install git (Ubuntu) or sudo yum install git (Fedora) Once installed check Git version: git ——version H I
  7. 7. FIR5T TIME GIT ‘SETUP Your Identity git config —-global user. name “Your Name” git conflg —-global user. email example@ncsu. edu Your editor git config —-global core. editor <editor like vim, emacs etc> Enable color in git git conflg —-global color. ui auto Checking your settings git config ——list (’. u ! ‘. l1I"~, ‘ u¢. .u-, .n,
  8. 8. INTRODUCTION TO VERSION CONTROL “Git is an example of Version Control” Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to: Revert files to previous state Revert entire project back to previous state Compare changes over time See who modified what? And much more. .. It means if you screw things up or lose files, you can easily l‘€COV€ I‘. “il , l l". ‘ i: "l(" Vii:
  9. 9. OTHER POPULAR VERSION CONTROL SYSTEMS Subversion (SVN) Concurrent Versions System (CVS) Perforce Mercurial Bazaar and the ones you know. .. 2 1 ~ 4
  10. 10. GIT! THE BIG PICTURE Use Cases: ° Individual Development ° Collaborative Development ° Ofiline Usage Wmom ' Everything is local (full history tree available oflline) ° Everything is fast ° Snapshots, not difls ° It is distributed not centralized ° Great for those who hate CVS/ SVN xxx’. x: I . MI*~. .,. I: ':lI_‘m, i|i.
  11. 11. GET %. %JOR§sTL0W1 THE TTTREE STATES “Something very important” In a Git repository your file can reside in three main states: ° Modified * Staged ° Committed What does this mean?
  12. 12. 6 E T 0E KFLO W: T H E T H E E ET E E Local Operations working staging git directory directory area (repository) You modify files in your working directory.
  13. 13. 6 E T 0E KFLO W: T H E T H E E ET E a Local Operations working staging git directory directory area (repository) stage liles You stage the files, adding snapshots of them to your staging area.
  14. 14. E i T 0 H F L O H: T H E T H ET E E Local Operations working staging git directory ll») directory area K (repository) / stage liles commit You do a commit that stores snapshot permanently to your Git directory.
  15. 15. EiT THE T H E E T ET E E Local Operations I I checkout the project stage lites commit Then, you can checkout any existing version, make changes, stage them and commit.
  17. 17. CREATE A NEW REPOSITORV Create a new directory, open it and perform a git init to create a new local git repository
  18. 18. CHECKOUT A REPOSITORV In a new folder create a working copy of local repository git clone / path/ to/ repository git clone https: //github. com/ rprataps/ Exerl. git
  19. 19. GET GIT COMMIT HISTORY You can study repository history using git log you can see last ‘p’ (a number) commits git log —n p
  20. 20. GET GIT COMMIT HISTOR‘r' You can also see git history with affected files git log --stat to see a compressed log where each commit is one line git log -—pretty= oneline
  21. 21. COMPARE CHANGES OVER TIME See changes in a commit git show commitID and compare two commits by git diff commit1ID commit2ID
  22. 22. REVERT FILES TO PREVIOUS STATES Revert to previous state git checkout commitID and return to current state git checkout master
  23. 23. EXERCISE-I 1. Clone the repository (if you haven’t): git clone https: //github. com/ rprataps/ Exerl. git 2. You have to find a bug in “intro_to_git1.html” (Hint: Open the web page in browser and select all text). 3. After identifying the bug you have use “git dif ” to identify the commit in which this bug was introduced.
  24. 24. LOGI N NSCU GITHUB ACCOUNT Go to https: //github. ncsu. edu Login with your unity id and password
  25. 25. CREATE A GITHUB REPOSITORV Click on ‘+’ sign on top right corner of github homepage and select ‘New Repository’
  26. 26. Owner Reposiloty name 1 1' talolalv HCSU Euefil V n - 1 , . . meat 'e; >os: (or~. ‘ ntwes av-3 sham and menworame he-ed msor. a'. ro'1' Haw about animaledoclorbatman k’, ___ , __ __, ,7 T, ,_" T _, / Description Initialize this repository with a README ‘ None - N I DO NOT check ‘initialize this repository with a README.
  27. 27. raroradi NCSU_Event <DUnwmch- 1 ester 0 Quick setup — it you've done this kind of thing before 'So1upinUest! op of HTTP! SSH httoswg1thaD. nc: u.er1i. t*arcra4«N{St. _€. ‘ent.319 we recommend evervrepositorvincludea wt»? ~‘. 'i ‘ i'. ‘~1E and mtnn W, . ..or create I new repository on the command line git init git remote add origin https: //github. ncsu. edu/ rarora4/N(SU_Event. git . ..or push an existing repository from the command line git remote add origin https: I/github. ncsu. edu/ rarorad/ H(SU_Event. git git push -u origin master
  28. 28. .*‘. °°5"': ’3‘. “°! °’. " Create a new directory (folder) Add a new file git_exercise. txt Write any 3 git commands Save this file. Move this file to Staging Area Commit this file And, push this file to your NCSU Github repository.
  29. 29. APP é C0iiii'WiiT Add new files to staging area git add <filename> git add * then commit changes git commit —m “Commit Message”
  31. 31. Ptiétiiiiié Ciriflitié-%%E§ To send these changes to your remote repository execute git push origin master you can also push to a branch other than ‘master’ git push origin branch_name
  32. 32. BUT WAIT. .. WHAT Ifi Dfiiéihi’? WHAT I5 IiM6TER? WHAT I5 §RAiiCi*i‘?
  33. 33. ORIGIN When you clone a repository for the first time Origin is a default name given to the original remote repository that you clone, from where you want to pull and push changes. git clone / path/ to/ repository By saying git push origin branch name you're saying to push to the origin repository.
  34. 34. When you initialize a new repository By convention you name your new Github (remote) repository as Origin. git init git remote add origin / path/ to/ repository There's no requirement to name the remote repository origin , and there can be multiple remote repositories.
  35. 35. BRANCHING Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion. Repository i’/ xii/ T‘) git may " "“’ fit merge Q <>. ~’i> —> Q—>Q —> O ~><j; ’ git commit "’
  36. 36. To create a new branch git branch new_branch_name switch to a different branch git checkout other_branch_name
  37. 37. A iii C int i G To create a new branch and switch to that branch git checkout —b new_branch_name
  38. 38. A EA C int i A To show all the branches git branch to delete a branch git branch —d branch_name
  39. 39. BRANCHING Remember, a branch is not available to others unless you push the branch to your remote repository git push origin branch_name
  40. 40. M E 6 1 E% G R A N C %= *% E 5 Checkout the branch you want to update git checkout branch_name Then merge branches by executing git merge this_branch_name that_branch_name
  41. 41. MERGING BRANCHE5 Git tries to auto—merge changes. Unfortunately, this is not always possible and results in c07y‘Iz'cts. You are responsible to merge those conflicts manually by editing the files shown by git. Useful tip: Before merging changes, preview changes git diff this_branch_name that_branch_name
  42. 42. EXERC|6E'5 . Form a team of 2 1 Member will Fork this repository: https: / / github. ncsu. edu/ rarora4~/ NCSU_Event . The same member will add other member as collaborator to this forked repository.
  43. 43. rarorad J NCSU_Event 1 m p mastetv NCSU_Event + Fa’)! Contmtt ? mrorad ‘in! --7' ‘E E Clone in Desluop 1;: Download ZIP Go to Repository you want to fork and click on ‘Fork’ button
  44. 44. rps| ngh3 NCSU_Event <3: Unwalch - Description I B P masaeu NCSU_Event + Ii! we recommend adding a README lo "115 reposxtory to help give people an overvvew or your N56 3 “NW5 proyecl 0 Clone in Desktop Q Download ZIP Then click on ‘Settings’ link to add collaborators.
  45. 45. rpsangh3 / NCSU_Evenl a>unwmcn- Collaborators Enter the Unity ID of the collaborators, then click ‘Add Collaborator’ button
  46. 46. 5-“ EXERCISE‘? . Form a team of 2 1 Member will Fork this repository: https: / / github. ncsu. edu/ rarora4/NCSU_Event . The same member will add other member as collaborator to this forked repository Both the members will clone this repository 1 Member will work on master branch and the other will create a new branch Open ‘student_web_page. html’, and add more student tasks ‘<li> this is a new task </ li>’
  47. 47. git clone https: //github. ncsu. edu/ rarora4/NCSU_Event. git Person working git branch new branch name on branch: ‘ ‘ git checkout new_branch_name Person working You work on Master Branch on master Complete all assignments on Line Participate in ACH/ AITP Hack Nights Enjoy Pig Picking Event Eat Pizzas Perform American Line Dance
  48. 48. 7. Save changes, stage changes, commit them. git add * git commit —m “Commit Message” 8. Push changes to remote repository by executing: Person working on master: git push origin master Person working git push origin branch_name on branch:
  49. 49. iiitaltulh tn-.1 rl»-I‘. (33 Unwntch v Doscnphon W: -bsrte “ Switch bran: hes mgr. I 'V ‘- i ' '-- nrws Bunches Q Clone in Desktop You can see all the branches by Clicking on ‘branch’ dropdown. To switch to a branch select that branch.
  50. 50. rpsingh3 / NCSU_Event G>Unwatch- 1 Description . HJVI C5U_EV°"‘ * Add ‘Jon: Tas-5 rpsingtifl O , . , we recommend adding a README to this repository to help give people an overview ot your _ » — V p, o,e(, 3 Clone in Desktop Now you are in different branch. Contents of a branch (if not merged) will be different then that of master branch. To marge this branch with master, click on ‘Pull Request’.
  51. 51. 5? UlIWdlCh - ipsinghllflt‘. SU_EveriI - » ipsingti'. l/NC SU_[ VI. ’Ill - i, rioor. -e .3 Base Repository ». "‘ P additions 3"‘ 0 dI"l(‘lI0ll§ Click on ‘Edit’ above ‘View Pull Request’. Select repository and name of the branch with which you want to merge your branch.
  52. 52. rpsingh3 I NCSU_Event ® Unwatch ~ I Add More Tasks ~ i" w ». ' '— ’.1' . a;isiil. ni| Add More Tasks ””‘e ’ ’ we can't automatically merge than branches. gipsolanlv Please add more tasks to’ the UCSI I students . “ . .«i. -. -i , i. - Create pull request You will notice if you branch can be merged 0* not. In above scenario there is a merge Conflict. Still you can create a pull re uest.
  53. 53. Add Mote Tasks ‘Vine ' .1 AD‘! KC mO'ge ‘ I have added new tasksi < n, -M9 pun vequefil 1 This is a scenario when there is no merge Conflict, and Github can itself merge branches.
  54. 54. rpsnngh3 I NC$U_Event ® Unwmch - I Add More Tasks 2; rpsmgh3 4.‘ ‘ O Comersatron o O vpsinghl Qpsolank Piease add more tasks for the NCSU students ' ‘rpsmgn)MCSU_EvenI utomaticelly merge we pull request - . . W ; 9|-'rMu . ‘.? win'. 1- Unsubsuibe ! p.m r : p.lM When there is a merge conflict. Pull request disables “Merge Pull Request" button and suggests you to use your machine to resolve it.
  55. 55. 9. To resolve the conflict - collaborator working on the new branch has to switch to master branch, pull latest code from repository’s master branch. git checkout master git pull origin master 10. Then switch back to the branch, merge master’s new code with this branch’s code. git checkout new_branch_name git merge new_branch_name master
  56. 56. EXERCISE’? 1 1. You will receive a merge conflict message RAVIPRATAP (ra. i} NCSU_Event S git merge ravi master Auto-merging student_web_page. html CONFLICT (content) : Merge conflict in student_web_page. html ed; fix conflicts and then commit the result. Automatic merge fail DAI‘l’nDA‘l’Afl i’. - I-rnr7ir urcli tun. -0 C 12. Open ‘student_web_page. html’ to determine the conflicting portion and resolve them manually. (AA) IILLBDU ¢aA Canllul x/4:2 <lz> Coplete all aaaionuenta on tine </ li> <1;> Participate in ACN/ AITP Each night: </11> <1i> Enjoy Pig Picking Event </1;> <<<<<<< HEAD .11) >>>>>>) Ill [8 I‘ -ance </ li> <ii> attend at; ciaaaes </ iz> <1;> Couplete all aaaiqnnents on tine </ li> <i1> Participate in ACH/ AITP Back Nights </1i> <1i> Enjoy Pig Picking Event </ ii) <li> lat Pizzas </ l;> <11> Register for Course: </11> <li> Pertorn American Line Dance </11> <11> Attend Git Workshop </11> <1i> Code </11> </ u1>
  57. 57. 13. 14. 15. EXERCI5E'3 After resolving the conflict save changes, stage them, commit them and push code to the branch on Github. git push origin branch_name On Github check your pull request, it will automatically update, now you will see the message “This pull request can be automatically merged. ” The collaborator working on master branch has to marge the pull request into master by clicking on “Merge Pull Request” button.
  58. 58. Add More Tasks :1 rpsmghtl . :ri'. -.' - O Comusauon -: - rpsinghl Qpsolank Please add more tasks tor the NCSU students [3 rpsinghl rpsingnl-flCSU_Event Plullllt . )l'-». 'l. 1- Unsubscribe This pull request can be automatically merged. 1 1i. )|'T: ix; v.): wl Pull Request page updated, “Merge Pull Request” button becomes active.
  59. 59. rpsmgh3 NCSU_Event ®UnwaIch- 7 Add More Task rpsmgh3 gpsolanli Please add niore tasks 13' the NCSU students Q. lpsmg| ‘|3 “ rpsmghl »'«v]crt«3ns seems peric-ct rpsinghil Q rnuvuihl Message showing pull request has been merged. Now collaborator can also discard the extra branch created.
  60. 60. EXERC|5E'5 16. Now both the collaborators can pull the latest merged from Github on master branch. git checkout master git pull origin master
  61. 61. “When in doubt, do exactly the opposite of CVS” - Linus Torvalds
  62. 62. :, -z*—, ‘T. rETm. s:‘<l’= .t: _ie_i_Lins. !. '3I. '§. l_i'; «. «2si_’r_l. l *«‘2§; W: ig*e+i. r':1ltI. m‘E. i=. i.: '.. gf‘r* 'l. Z,LlrlI_. l.. l6 . Eu L 5;‘: l. E:l*3:. l'?5L. T f ‘:3 ‘~92-*ii. i,ua. t1r: ir: tji. :i. v:. :.u-. t’ir1}, t-3:1,. ]-, i.-. a:. t 1'” *1 11-: I Eta; :si5‘fi6‘%, i;i_t': ,i; r=s: .L»2Qi. ..-H «:7! i. t.r'§“= :». .- iv’-ta? ‘ ' it: »‘*-is 53:’. if G wt-'; '«*. .ivi . ;fZii, l zsi; i_? i,ia. i;r~'e. .1: a; ,~: l;wi, u; tig, ;iJji_l: ir. i:_bi. . T q , nil, ‘ , VIMiill: l;l. i_. lBil_l. t1éHl{lE. ‘ -; ‘ , v.vv~wii. :'. *€.1'-: : l.1I_l%-. ' * 1. {*1 1: Q ‘« ea Tc‘: l£; _r. _ue+»: ?i‘t: : is-»-' . . -‘. ‘l’. i': —- iii In‘) igm «, :"; 'r. .'*-. .I: i, i.ElI€= i»_‘t- -‘ in T,