Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git internals + Git flow

Git internals + Git flow

Review of the main Git commands and how to mix them together to effectively work with people

Serchinastico

April 06, 2016
Tweet

More Decks by Serchinastico

Other Decks in Programming

Transcript

  1. Pedro Vicente Gómez Alberto Gragera Jorge Barroso Davide Mendolia Senior

    Mobile Developer Technical Director Senior Android Developer Senior Full Stack Engineer
  2. > git init • Creates a new repository • #

    just a .git folder describing your new filesystem • Only in your computer • Not tracking anything (yet) LET THERE BE GIT
  3. .git !"" HEAD !"" objects #"" refs !"" heads #""

    tags > git init LET THERE BE GIT
  4. > git add • Saves file contents in a blob

    object • # SHA-1 + contents • Tracks the file including it in the index • # A file saving other files metadata SNAPSHOTTING
  5. .git !"" HEAD !"" index !"" objects $ #"" a1

    $ #"" 9abfea0f… #"" refs !"" heads #"" tags > git add • index contains a list of files metadata: • permissions • SHA-1 • name • the name of blob files are SHA-1s • Their content is a binary with the contents of the file they are storing SNAPSHOTTING
  6. > git add > git ls-files --stage 100644 a19abfea0… 0

    file.txt > git show a19abfea0 Hola GIT SNAPSHOTTING
  7. > git commit • Takes every file listed in the

    index and builds a tree object • Creates a commit object and points it to the tree object SNAPSHOTTING
  8. > git commit .git !"" HEAD !"" index !"" objects

    $ !"" 18 $ $ #"" 611beb72… $ !"" 87 $ $ #"" 1e25a902… $ #"" a1 $ #"" 9abfea0f #"" refs !"" heads #"" tags • trees are regular git objects containing file references and metadata • commits are also objects holding: • tree reference • author • committer • parent • message SNAPSHOTTING
  9. > git commit > git ls-tree 18611beb72 100644 blob a19abfea0…

    file.txt > git show -s --pretty=raw 871e25a902 commit 871e25a902ed4309e94c2f39aa7102557a380f49 tree 18611beb7265e1af0b87190179be4d78ef80862a author Sergio Gutierrez <[email protected]> 1458666886 +0100 committer Sergio Gutierrez <[email protected]> 1458666886 +0100 First commit SNAPSHOTTING
  10. > git commit a19abfea0f blob Hola GIT 18611beb72 tree blob

    a19abfea0f file.txt 871e25a902 commit tree 18611beb72 author Sergio committer Sergio parent - SNAPSHOTTING
  11. > git commit a19abfea0f blob Hola GIT 18611beb72 tree blob

    a19abfea0f file.txt 871e25a902 commit tree 18611beb72 author Sergio committer Sergio parent - 2c492ae0a4 blob Adios GIT 779c601774 tree blob a19abfea0f file.txt blob 2c492ae0a4 other_file.txt c96b22adb6 commit tree 779c601774 author Sergio committer Sergio parent 871e25a902 SNAPSHOTTING
  12. > git commit a19abfea0f blob Hola GIT 18611beb72 tree blob

    a19abfea0f file.txt 871e25a902 commit tree 18611beb72 author Sergio committer Sergio parent - 2c492ae0a4 blob Adios GIT 779c601774 tree blob a19abfea0f file.txt blob 2c492ae0a4 other_file.txt c96b22adb6 commit tree 779c601774 author Sergio committer Sergio parent 871e25a902 SNAPSHOTTING f0eaf19baa blob Hola GIT v2 779c601774 tree blob f0eaf19baa file.txt blob 2c492ae0a4 other_file.txt 7fbc204b8f
  13. > git commit 871e25a902 commit tree 18611beb72 author Sergio committer

    Sergio parent - c96b22adb6 commit tree 779c601774 author Sergio committer Sergio parent 871e25a902 7fbc204b8f commit tree 22d693324f author Sergio committer Sergio parent c96b22adb6 snapshot 1 snapshot 2 snapshot 3 SNAPSHOTTING
  14. > index a.k.a. stage a.k.a. cache • Stores what is

    going to be snapshotted for the next commit • One of the three trees git modify with every command SNAPSHOTTING
  15. > git status • Shows which files are different •

    Working directory - Index (ì) • Index - Current commit (ì) SNAPSHOTTING
  16. > git diff • Shows all the changes in detail

    • Working directory - Index (default) • Index - Current commit (--staged) SNAPSHOTTING
  17. > git branch • Creates a branch pointing to the

    current commit • By default, Git creates a branch called master BRANCHING
  18. > git branch BRANCHING .git !"" HEAD !"" index !""

    objects $ #"" … !"" refs $ !"" heads $ $ !"" master $ $ #"" my-branch $ #"" tags • A branch is just a text file with a reference to a commit • Branches are automatically updated when creating a new commit
  19. > git branch BRANCHING > cat .git/refs/heads/master 7fbc204b8f3c0f85d7e86f5982d2bffe27ea742b > cat

    .git/refs/heads/my-branch 7fbc204b8f3c0f85d7e86f5982d2bffe27ea742b
  20. > git checkout • Changes the working branch • The

    working branch is identified by a pointer called HEAD BRANCHING
  21. > git checkout + git commit BRANCHING master my-branch 7fbc204b8f

    c96b22adb6 e25a902 HEAD 0612faad12 24dd971a23
  22. > git tag • Very similar to branches but are

    not updated automatically • There are two types: • lightweight • annotated BRANCHING
  23. > git tag (lightweight) DIVERGING .git !"" HEAD !"" index

    !"" objects $ !"" … !"" refs $ !"" heads $ !"" !"" … $ #"" tags $ #"" my-lightweight-tag • A lightweight tag is a file text with a reference to a commit
  24. > git tag (annotated) DIVERGING .git !"" HEAD !"" index

    !"" objects $ !"" … $ !"" 3e $ #"" 38efa5ea1f !"" refs $ !"" heads $ !"" !"" … $ #"" tags $ !"" my-annotated-tag $ #"" my-lightweight-tag • An annotated tag creates a tag object (very similar to a commit object) and points to it
  25. > git reset • Reverts the state of the code

    to a previous version • There are 3 versions, depending on the scope of the revert: • soft • mixed • hard REVERTING
  26. > git reset • Think of git reset as a

    three step process: 1. Move the current branch to REF 2. Revert index files to look like REF 3. Revert working directory files to look like REF REVERTING
  27. > git reset -- soft (step 1) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index index_file.txt index_other_file.txt working directory new_work.txt new_work_other_file.txt index_file.txt index_other_file.txt
  28. > git reset -- soft (step 1) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index index_file.txt index_other_file.txt working directory new_work.txt new_work_other_file.txt index_file.txt index_other_file.txt
  29. > git reset -- mixed (step 2) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index index_file.txt index_other_file.txt working directory new_work.txt new_work_other_file.txt index_file.txt index_other_file.txt
  30. > git reset -- mixed (step 2) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index file_in_919fbbc1d2.txt working directory new_work.txt new_work_other_file.txt index_file.txt index_other_file.txt
  31. > git reset -- hard (step 3) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index index_file.txt index_other_file.txt working directory new_work.txt new_work_other_file.txt index_file.txt index_other_file.txt
  32. > git reset -- hard (step 3) REVERTING my-branch 7fbc204b8f

    HEAD 919fbbc1d2 index file_in_919fbbc1d2.txt working directory file_in_919fbbc1d2.txt
  33. > git merge • If one of the branches is

    a subhistory of the other: • apply fast-forward • else: • creates a new commit with two parents COMBINING
  34. > git rebase • Finds the most recent common ancestor

    • Replays all the commits from one branch in the tip of the other • The operation rewrites history COMBINING v2
  35. > git rebase • Interactive rebase let’s you rewrite history

    as you want • pick • reword • squash • … COMBINING v2
  36. > git remote add • Adds yet another reference to

    find other copies of the repository • When using git clone, it will automatically add a default remote called origin SHARING
  37. > git remote add SHARING .git !"" HEAD !"" index

    !"" config !"" objects $ !"" … !"" refs $ !"" heads $ !"" !"" … $ !"" remotes $ #"" tags [remote "origin"] url = [email protected]:Karumi/MyRepo.git fetch = +refs/heads/*:refs/remotes/origin/*
  38. > git fetch • Brings everything contained in a remote

    to your local copy, including: • branches • commits • trees • blobs • … SHARING
  39. > git fetch SHARING .git !"" HEAD !"" index !""

    config !"" objects $ !"" … !"" refs $ !"" heads $ !"" !"" … $ !"" remotes $ $ !"" origin $ $ $ !"" master $ $ $ #"" my-branch $ #"" tags • A remote is nothing but a directory with a file for each branch • Remote branches are text files with the SHA-1 of the commit they are pointing to
  40. > git fetch SHARING > cat .git/refs/remotes/origin/master 7fbc204b8f3c0f85d7e86f5982d2bffe27ea742b > cat

    .git/refs/remotes/origin/my-branch 7fbc204b8f3c0f85d7e86f5982d2bffe27ea742b
  41. > git pull • git fetch + git merge SHARING

    > git branch -u • Links a local branch with a remote branch • It will automatically pull and push to that remote branch without further specification
  42. SHARING > git push • Sends all local objects that

    are not in the remote • Syncs the branch or branches using fast- forward
  43. EXTRA > git revert • Creates a reverse patch from

    the specified commit and applies on HEAD • Use it when you mistakenly pushed something on a branch shared with others
  44. EXTRA > git cherry-pick • Replays the specified commits on

    HEAD • Use it when you want to pick a single commit (a fix) and have it in your branch • Use it if you messed up real bad your history and want to recreate a branch
  45. EXTRA > git stash • Saves your index and working

    directory • You can apply those changes anytime you want • There are multiple options available: • create • apply • list • pop
  46. WORKING WITH A TEAM • Several ways of working together:

    • Centralised workflow • Feature branch workflow • Gitflow workflow • Forking workflow • Whatsoever workflow
  47. WORKING WITH A TEAM • Several ways of working together:

    • Centralised workflow • Feature branch workflow • Gitflow workflow • Forking workflow • Whatsoever workflow
  48. WORKING WITH A TEAM • Several ways of working together:

    • Centralised workflow • Feature branch workflow • Gitflow workflow • Forking workflow • Whatsoever workflow
  49. WORKING WITH A TEAM • master • A history of

    all the releases • develop • Contains everything is going to be released • feature • New stuff always comes in brand new branches
  50. WORKING WITH A TEAM • release • Prepare all the

    release wise operations • bugfix • Same as a regular feature (might be merged to release branches) • hotfix • Branched from master, merges in master and develop
  51. FAQ > How do I solve merge conflicts? • You

    will see the code of your current branch (HEAD) vs the code of the branch you are bringing • Use your favourite text editor or your favourite tool
  52. FAQ > How do I solve merge conflicts? <<<<<<< HEAD

    public void myOtherMethod() { ======= public bool myMethod() { >>>>>>> master My code vs Others code
  53. FAQ > How do I solve merge conflicts? • While

    rebasing, my code is the branch you are rebasing to!
  54. FAQ > How do I see the differences between two

    branches? • git diff branch1..branch2
  55. FAQ > How do I revert a non-pushed commit but

    keeping its contents? • If you only want to add files and/or change the commit message: • git commit --amend • else: • git reset --soft HEAD^
  56. FAQ > How do I revert a pushed commit? •

    If you can rewrite history: • git reset --hard HEAD^ • git push -f • else: • git revert HEAD
  57. FAQ > How do I change my current branch keeping

    my work? • Use one of the tools you learned so far: • git commit • git stash
  58. FAQ > How do I revert a merge? • If

    you can rewrite history: • git reset --hard HEAD^ • git push -f • else: • git revert HEAD -m1
  59. > git revert -m1 HEAD FAQ master my-branch commit A

    commit C commit B commit M commit ~M
  60. > git revert -m1 HEAD (caveats) FAQ master commit M

    commit ~M my-branch commit C commit B
  61. > git revert -m1 HEAD (caveats) FAQ master commit M

    commit ~M my-branch commit C > git merge my-branch Already up-to-date. commit B
  62. > git revert -m1 HEAD (caveats) FAQ master commit M

    commit ~M commit ~~M my-branch commit C commit D
  63. > git revert -m1 HEAD (caveats) FAQ master commit M

    commit ~M commit ~~M my-branch commit C commit D commit M2
  64. I NEED MORE (BOOKS) • Pro Git #Book with an

    in-depth explanation about how Git works • Git community book #A collaborative online book about Git, complementary of the first
  65. I NEED MORE (ONLINE) • try.github.io #Really basic tutorial with

    a console to interact with git • pcottle.github.io/learnGitBranching #A complete tutorial of Git branches, includes rebasing and an awesome visualization • git-it # Online workshop from your terminal
  66. I NEED MORE (TOOLS) • SourceTree #Standard VCS desktop client

    • GitUp #Best visualization tool • GitHub Desktop #Simple client for GitHub • GitKraken #Desktop client focused on fancy visuals