Git - Get Ready To Use It

2,886 views
2,632 views

Published on

A workshop I'm holding to school my colleages in the usage of git

Published in: Technology
1 Comment
20 Likes
Statistics
Notes
No Downloads
Views
Total views
2,886
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
0
Comments
1
Likes
20
Embeds 0
No embeds

No notes for slide

Git - Get Ready To Use It

  1. 1. GIT GET READY TO USE IT DANIEL KUMMER SENIOR SOFTWARE ENGINEER NAMICS AG ZÜRICH WWW.NAMICS.COM
  2. 2. 1 FORGET WHAT YOU KNOW ABOUT CVS/SVN GIT IS DIFFERENT EMPTY YOUR MIND – A FULL ONE CAN’T LEARN ANYTHING…
  3. 3. THE BASICS (NEARLY) EVERY OPERATION IS LOCAL YOU HAVE YOUR OWN REPO GIT STORES SNAPSHOTS, NOT DIFFERENCES GIT ONLY ADDS DATA
  4. 4. STANs REPO YOUR REPO ORIGIN CENTRAL REPO IRONs REPO DISTRIBUTED VCS
  5. 5. ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH
  6. 6. ENVIRONMENT SETUP INSTALL GIT www.git-scm.com INSTALL SOURCETREE GUI - OR A GUI OF YOUR CHOICE www.sourcetreeapp.com
  7. 7. INITIAL CONFIG YOUR IDENITY – COMMIT AUTHOR INFORMATION $ git config --global user.name “Darth Vader” $ git config --global user.email darth@deathstar.emp ADDITIONAL USEFUL GLOBAL SETTINGS $ git config --global core.filemode false $ git config --global core.autocrlf true $ git config --global core.ignorecase false $ git config --global rerere.enabled true $ git config --global color.ui true $ git config --global format.pretty oneline
  8. 8. JOIN A PROJECT git.namics.com TELL YOUR MASTER TO ADD YOU TO THE PROJECT
  9. 9. IN 3 STEPS GENERATE SSH KEYS $ ssh-keygen –t rsa –C darth.vader@deathstar.emp UPLOAD YOUR SSH PUBLIC KEY TO YOUR ACCOUNT https://git.namics.com/keys CLONE THE REPOSITORY $ git clone git@git.namics.com:deathstar-v3.git
  10. 10. YOUR CLONE IS A WORKING COPY OF THE ORIGINS’ MASTER BRANCH – NONE OTHER! $ git status # On branch master nothing to commit, working directory clean CONTAINS EVERY VERSION OF EVERY FILE CONTAINS A .GIT FOLDER IN THE ROOT DIRECTORY WITH EVERYTHING GIT NEEDS
  11. 11. LET’S GET STARTED! 1. RECORDING CHANGES
  12. 12. ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH
  13. 13. FILE STATUS LIFECYCLE REMOVE UNMODIFIED STAGE UNTRACKED ADD MODIFIED STAGED
  14. 14. CHECK THE WORKING COPY STATUS $ git status # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # exhaust_port_plans
  15. 15. LETS ADD A FILE TO THE STAGE – READY FOR COMMIT $ git add exhaust_port_plans $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: exhaust_port_plans # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README
  16. 16. MORE INFO THAN STATUS WITH DIFF $ git diff diff --git a/README b/README index e69de29..18ef097 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +hello my empire minions - let's build a death star that works this time!
  17. 17. NOW, COMMIT EVERYTHING ADDED TO THE STAGE $ git commit -m 'add exhaust port plans’ [master e8c1e22] add exhaust port plans 1 file changed, 1 insertion(+) create mode 100644 exhaust_port_plans SKIP THE STAGE AREA IF YOU DON’T NEED IT $ git commit -a -m 'add hello message to readme’ [master 73ceaf6] add hello message to readme 1 file changed, 1 insertion(+) REMOVE/MOVE FILES WITH $ git rm $ git mv
  18. 18. IGNORE THINGS YOU CAN’T IGNORE ALREADY TRACKED FILES AN EXAMPLE OF THE .GITIGNORE TEXTFILE # a comment - this is ignored *.log .DS_store .idea/**/* **.classpath **.project **.settings (INSIDE THE PROJECTS ROOT DIRECTORY)
  19. 19. RECAP RECORDING CHANGES CHECK CURRENT WORKING COPY STATUS $ git status ADDING FILES TO THE INDEX $ git add <pathspec> SHOW CHANGES $ git diff COMMIT TO REPOSITORY $ git commit [-a] [-m] (WORKING COPY INDEX)
  20. 20. MESSED UP? 2. UNDOING THINGS
  21. 21. CHANGE YOUR LAST COMMIT –EX: ADD FORGOTTEN FILES $ git commit --amend NEVER AMEND AFTER YOU PUSHED YOUR CHANGES! UNSTAGE ADDED FILES $ git status # Changes to be committed: # # new file: alimony_for_luke $ git reset HEAD alimony_for_luke $ git reset HEAD alimony_for_luke # Untracked files: # # alimony_for_luke
  22. 22. REVERT CHANGES IN YOUR WORKING DIRECTORY $ git status # Changes not staged for commit: # # modified: README # $ git checkout –- README $ git status # On branch master # nothing to commit, working directory clean
  23. 23. REVERSE COMMIT $ git revert <commit> CREATES NEW COMMITS RECORDING THE REVERT CHANGES IF YOU GET MERGE CONFLICTS, RESOLVE AND CONTINUE $ git revert --continue CANCEL REVERT OPERATION $ git revert --abort
  24. 24. RECAP UNDOING THINGS ALTER LAST COMMIT $ git commit --amend UNSTAGE FILES $ git reset HEAD <file> REVERT CHANGES $ git checkout -- <file> REVERSE COMMIT $ git revert <commit>
  25. 25. JOIN THE TEAM EFFORT 3. WORK WITH REMOTES
  26. 26. ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH
  27. 27. YOUR CLONED REPO ALREADY HAS A REMOTE - ORIGIN $ git remote -v origin git@git.namics.com:deathstar-v3.git (fetch) origin git@git.namics.com:deathstar-v3.git (push) ADDING A REMOTE TO YOUR LOCAL REPOSITORY $ git remote add origin git@git.namics.com:deathstar-v3.git YOU COULD HAVE MULTIPLE REMOTES
  28. 28. GETTING INFORMATION ABOUT A REMOTE $ git remote show origin * remote origin Fetch URL: git@git.namics.com:deathstar-v3.git Push URL: git@git.namics.com:deathstar-v3.git HEAD branch: master Remote branch: master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (fast-forwardable)
  29. 29. FETCH VS PULL GET DATA FROM THE REMOTE REPOSITORY $ git fetch <remote-name> FETCH ONLY MANUAL MERGING FETCH $ git pull FETCH AND MERGE TRACKED BRANCHES PULL
  30. 30. PUSH CHANGES PUSH BRANCH TO REMOTE $ git push <remote-name> <branch-name> ONLY WORKS IF NOBODY HAS PUSHED IN THE MEANTIME $ git push origin master Counting objects: 8, done. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 650 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) To git@git.namics.com:deathstar-v3.git 5d31566..73ceaf6 master -> master
  31. 31. EXPLICITLY PUSH BRANCHES YOU WANT TO SHARE PUSH IS REJECTED IF CURRENT BRACH IS BEHIND $ git push origin master To git@git.namics.com:deathstar-v3.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@git.namics.com:deathstar-v3.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.
  32. 32. RECAP REMOTES LIST REMOTES $ git remove -v ADDING REMOTES $ git remote add <shortname> <url> INFORMATION ABOUT REMOTE $ git remote show <shortname>
  33. 33. RECAP REMOTES FETCH FROM REMOTES $ git fetch <remote-name> PULL FROM REMOTES $ git pull PUSH CHANGES TO REMOTES $ git push <remote-name> <branch-name>
  34. 34. LEAVE YOUR MARK 4. TAGGING
  35. 35. LIGHTWEIGHT VS ANNOTATED $ git tag v0.1 $ git tag –a v1.0 –m ‘first rel’ CHECKSUM IN A FILE FULL OBJECT INTERNAL PROJECTS CHECKSUM AUTHOR INFO GPG SIGNED (-s) OPEN SOURCE REPOSITORIES
  36. 36. TAGS MUST BE PUSHED SEPARATELY $ git push origin --tags Total 0 (delta 0), reused 0 (delta 0) To git@git.namics.com:deathstar-v3.git * [new tag] 1.0 -> 1.0 LIST TAGS WITH $ git tag 1.0 1.1
  37. 37. BREAK TIME BACK IN 15
  38. 38. THE KILLER FEATURE 5.BRANCHING
  39. 39. COMMITS AND BRANCHES COMMIT - OBJECT THAT CONTAINS A POINTER TO THE SNAPSHOT OF THE CONTENT THE GIT HISTORY IS A LINKED LIST OF POINTERS 98ca9 34ac2 f30ab Snapshot A Snapshot B Snapshot C
  40. 40. A BRANCH IS A LIGHTWEIGHT POINTER TO A COMMIT LOCAL POINTER TO YOUR CURRENT BRANCH MASTER BRANCH POINTER 98ca9 34ac2 TESTING BRANCH POINTER HEAD master f30ab testing
  41. 41. CREATING A BRANCH IS WRITING 41 BYTES TO A FILE (SHA-1 HASH PLUS NEWLINE) THAT’S WHY BRANCHING IS SUPER FAST
  42. 42. CREATE A NEW BRANCH $ git branch testing CHECKOUT EXISTING BRANCH $ git checkout testing SWITCHING TAKES A BLINK OF THE EYE EVERYTHING HAPPENS IN YOUR WORKING COPY 98ca9 34ac2 NOW YOU’RE ON THE TESTING BRANCH SHORT FORM FOR BRANCH AND CHECKOUT $ git checkout –b testing master f30ab testing HEAD
  43. 43. BASIC MERGING C0 I WANT TO MERGE ISS53 INTO MASTER Snapshot to Merge Into Common Ancestor C1 CHECKOUT THE TARGET BRANCH C2 master C4 C3 C5 $ git checkout master MERGE THE ISSUE BRANCH $ git merge iss53 iss53 Snapshot to Merge In
  44. 44. master MERGE MAGIC HAPPENED: C0 C1 C2 THE RESULT $ git checkout master $ git merge iss53 C4 C3 C6 C5 iss53 Auto-merging README Merge made by the 'recursive' strategy. README | 1 + 1 file changed, 1 insertion(+) FEATURE BRANCH NO LONGER NEEDED, DELETE IT $ git branch –d iss53
  45. 45. RESOLVING CONFLICTS WHAT IF MAGIC DOESN’T HAPPEN? CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. USE A MERGETOOL TO RESOLVE THE CONFLICTS $ git mergetool (DIFFMERGE FOR EXAMPLE)
  46. 46. COMMIT CHANGES AFTER RESOLVING THE CONFLICTS $ git commit Merge branch 'testing' Conflicts: README # # It looks like you may be committing a merge. # If this is not correct, please remove the file # .git/MERGE_HEAD # and try again. THE MESSAGE IS AUTOGENERATED YOU CAN STILL EDIT IT OF COURSE
  47. 47. RECAP MERGING CREATE BRANCH $ git branch <branchname> CHECKOUT BRANCH $ git checkout <branchname> MERGE BRANCHES $ git merge <commit> RESOLVE CONFLICTS $ git mergetool
  48. 48. BRANCH MANAGEMENT LIST BRANCHES -a: remote branches $ git branch [--merged] [-a] DELETE BRANCHES $ git branch [-d] [-D] -D: force delete unmerged
  49. 49. REMOTE BRANCH PUSHING TO REMOTE BRANCHES $ git push [remote] [branch] TRACKING REMOTE BRANCHES $ git checkout –b <branch> <remotename>/<branch> ALLOWS GIT PULL INSTEAD OF GIT FETCH DELETING REMOTE BRANCHES (NOTE THE : ) $ git push <remotename> :<branch>
  50. 50. RECAP BRANCH MANAGEMENT LIST ALL BRANCHES $ git branch -a TRACKING REMOTE BRANCHES $ git checkout –b <branch> <remotename>/<branch> PUSHING TO REMOTE BRANCHES $ git push [remote] [branch] DELETING REMOTE BRANCHES (NOTE THE : ) $ git push <remotename> :<branch>
  51. 51. BRANCHING WORKFLOWS GIT FLOW STRUCTURE FOR LARGE PROJECTS FEATURE BRANCH NEW BRANCH FOR EVERY FEATURE CENTRALIZED SVN LIKE FORKING USED BY GITLAB
  52. 52. GIT FLOW MASTER OFFICIAL RELEASE HISTORY HOTFIX PRODUCTION RELASE PATCHING RELEASE POLISHING – NO NEW FEATURES DEVELOP FEATURE INTEGRATION FEATURE FEATURE DEVELOPMENT, CHILD OF DEVELOP
  53. 53. GIT FLOW
  54. 54. REBASE A MERGING ALTERNATIVE TAKE ALL COMMITED CHANGES ON ONE BRANCH AND REPLAY THEM ON ANOTHER ONE
  55. 55. SAME RESULT AS MERGE BUT CLEANER HISTORY IT APPEARS THAT ALL WORK HAPPENED IN SERIES B E F master C BEFORE A D experiment master AFTER A B E F C' D' experiment
  56. 56. DO NOT REBASE PUSHED COMMITS REBASING ABANDONS EXISTING COMMITS AND CREATES SIMILAR NEW ONES USE REBASE IF YOU KNOW WHAT YOU’RE DOING NEWBIES SHOULD USE MERGE – IT’S SIMPLER REBASE A BRANCH ONTOP OF THE CURRENT ONE $ git rebase <branch> REBASE ONTO TARGET BRANCHES $ git rebase --onto <branches>
  57. 57. MASHUP COMBINATIONS 6. REPOSITORY WITHIN REPO
  58. 58. SUBMODULES TRACKED BY THE EXACT COMMIT SPECIFIED IN THE PARENT REPOSITORY – LOCKED TO COMMIT GOOD FOR NOT TOO FREQUENT UPDATES THE WHOLE REPOSITORY IS INCLUDED AS SUBMODULE PARTIAL INCLUDE IS NOT POSSIBLE
  59. 59. MANY BUTS’ LOTS OF DISADVANTGES AND PITFALLS YOU CAN’T JUST CLONE THE PARENT REPOSITORY SUBMODULE NEEDS TO BE INITIALIZED AND UPDATED NEVER AUTOMATICALLY UPDATED WHEN THE SUBMODULE REPOSITORY IS UPDATES YOU CAN’T BRANCH EASILY ACCIDENTIAL SUBMODULE REVERT HAPPENS EASILY LOTS OF THINKING INVOLVED WHEN USING SUBMODULES
  60. 60. SUBTREE MERGE IT’S A MERGE STRATEGY (LIKE RECURSIVE FOR BRANCHES) NO NEW METADATA SUB-PROJECT CONTRIBUTION IS SLIGHTLY MORE COMPLICATED YOU ARE RESPONSIBLE FOR NOT MIXING SUPER AND SUB-PROJECT CODE
  61. 61. SOME CAKE 7. USEFUL TOOLS
  62. 62. INTERACTIVE MODE ENHANCED COMMAND LINE ADD –i TO COMMAND LINE; FOR EXAMPLE $ git add –i staged unstaged path 1: unchanged +0/-1 TODO *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>
  63. 63. THE STASH A “DIRTY LITTLE HELPER” STORE YOUR LOCAL MODIFICATIONS AWAY AND REVERT WORKING COPY TO HEAD COMMIT SAVE CHANGES TO STASH $ git stash APPLY STASH $ git stash pop
  64. 64. ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH
  65. 65. CHERRY PICK APPLY CHANGES FROM ANOTHER COMMIT $ git cherry-pick <commit> BEFORE C0 C1 AFTER HEAD C2 C4 C0 C1 HEAD C2 C4 C6 git cherry pick C3 C3 C5 APPLY CHANGES FROM C3 AS NEW COMMIT ON TOP OF C4 C3 C5
  66. 66. BISECT YOUR CODE FIND THE CHANGE WHICH INTRODUCED THE BUG $ git bisect start CURRENT VESION (HEAD) IS BAD $ git bisect bad LAST TESTED VERSION WHICH WAS GOOD $ git bisect good v0.4
  67. 67. GIT SVN BIDIRECTIONAL OPERATIONS BETWEEN GIT AND SVN TRACK STANDARD SVN REPOSITORY “TRUNK/BRANCHES/TAGS” UPDATE GIT WITH FETCH $ git fetch UPDATE SVN WITH DCOMMIT $ git dcommit
  68. 68. MOST COMMON PROBLEMS 8. AND SOLUTIONS
  69. 69. I CAN’T CLONE CHECK YOUR SSH CONNECTION $ ssh -T git@git.namics.com Welcome to GitLab, Daniel Kummer! VERIFY YOUR PUBLIC-KEY IN YOUR GITLAB PROFILE VERIFY YOUR PROJECT ACCESS IN GITLAB TRY RE-ADDING YOUR KEY TO SSH-AGENT $ ssh-add –D $ ssh-add ~/.ssh/id_rsa # deletes all identities in agent # add key to agent
  70. 70. I CAN’T PULL VERIFY THAT THERE ARE NO UNCOMMITED CHANGES VERIFY THAT YOUR LOCAL BRANCH TRACKS A REMOTE ONE (TRACKED ONES ARE BLUE) $ git branch -vv * master 4658814 [origin/master] SUBSEQUENT TRACKING OF REMOTE BRANCHES $ git branch --set-upstream-to <local-branch> origin/ <remote-branch>
  71. 71. I CAN’T PUSH CHECK IF YOUR LOCAL BRANCH IS BEHIND $ git push # Your branch is behind 'origin/master' by 5 commits CHECK IF YOUR LOCAL BRANCH IS TRACKING A REMOTE ONE
  72. 72. I CAN’T DELETE A FILE FROM REPO TRY DELETING THE CACHED FILE ONLY (LEAVES WORKING COPY INTACT) $ git rm --cached eclipse.properties IF THE FILE NEEDS TO BE REMOVED FROM THE HISTORY $ git filter-branch … https://help.github.com/articles/remove-sensitive-data
  73. 73. MORE MATERIAL GIT-PRO BOOK / REFERENCE http://git-scm.com/ TUTORIALS https://www.atlassian.com/git GIT FLOW CHEATSHEET http://danielkummer.github.io/git-flow-cheatsheet 15 MINUTES INTERACTIVE TUTORIAL http://try.github.io
  74. 74. GETTING HELP $ git help <command> ASK YOUR LOCAL GURU GOOGLE IT CONT@CT ME
  75. 75. CONGRATULATION YOU MADE IT !

×