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

A brief introduction to git and Github

Kiko
March 31, 2016

A brief introduction to git and Github

This presentation covers an introduction to distributed version control systems, moves on to explain the basics of git: how to track files, perform commits and fix merge conflicts. Finally, it provides links to different distributed workflows and focuses on the Central repository workflow. All of this is explained to work at the command line level but links to graphical interfaces are provided at the end of the presentation, together with some other useful resources.

Kiko

March 31, 2016
Tweet

More Decks by Kiko

Other Decks in Programming

Transcript

  1. Distributed Version Control System • Remote and local copies •

    Keep track of file changes • Knows what you did last summer • Remembers authorship Git: 2
  2. Distributed Version Control System • Remote and local copies •

    Keep track of file changes • Knows what you did last summer • Remembers authorship Git: 2
  3. Distributed Version Control System • Remote and local copies •

    Keep track of file changes • Knows what you did last summer • Remembers authorship Git: 2
  4. Distributed Version Control System • Remote and local copies •

    Keep track of file changes • Knows what you did last summer • Remembers authorship Git: 2
  5. Github & Git • Set up an account • Create

    a repository (repo) • Add things to your repo • Branch workflow • Share changes with your team 3
  6. • Create Github account • Set up Git (link Github)

    5 Linux & OS X $ git config --global push.default simple $ git config --global core.editor “subl -n -w” link config editors
  7. • Create Github account • Set up Git (link Github)

    5 Windows $ git config --global push.default simple $ git config --global core.editor “subl -n -w” link config editors
  8. Create a repository (repo) Add collaborators to repo Clone repo

    on local machine $ git clone https://github.com/kikofernandez/git-intro.git $ git clone <address-repo> Remote Remote Local = the remote server, Github = your local machine Local Initial Steps: Remote
  9. Create a repository (repo) Add collaborators to repo Clone repo

    on local machine $ git clone https://github.com/kikofernandez/git-intro.git Remote Remote Local = the remote server, Github = your local machine Local Initial Steps: Remote
  10. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> Changes that make up a commit Commands: $ git commit -m “message” Local
  11. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md Changes that make up a commit Commands: $ git commit -m “message” Local
  12. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” Local
  13. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” Local
  14. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” Local
  15. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” Local
  16. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” this is an introduction to the course … intro.md Local
  17. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” this is an introduction to the course … intro.md Local
  18. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” this is an introduction to the course … intro.md this is an introduction to the course … intro.md Local
  19. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” this is an introduction to the course … intro.md this is an introduction to the course … intro.md Local
  20. Git: Basics 8 Working Directory Staging Area Folder of your

    repository, e.g. ~/git-intro Commands: $ git add <file> this is an introduction to the course … intro.md $ git add intro.md Changes that make up a commit Commands: $ git commit -m “message” $ git commit -m “add intro.md file” this is an introduction to the course … intro.md this is an introduction to the course … intro.md Local
  21. Git: Logs 9 $ git log -p How do I

    see the changes? Remote Local
  22. Git: Logs 9 $ git log -p How do I

    see the changes? Remote Local
  23. Git: Logs 9 $ git log -p How do I

    see the changes? Remote Local
  24. Git: Logs 9 $ git log -p How do I

    see the changes? Remote Local
  25. Commit early, commit often A tip for version controlling -

    not for relationships ( ) 10 Best tip you will ever receive:
  26. Git: Branching Local 3ddf4 master Branches: they are pointers to

    commits / versions Default branch: master $ git add intro.md; git commit -m “update” 12
  27. Git: Branching Local 3ddf4 master Branches: they are pointers to

    commits / versions Default branch: master $ git add intro.md; git commit -m “update” 12ab56 12
  28. Git: Branching Local 3ddf4 master Branches: they are pointers to

    commits / versions Default branch: master $ git add intro.md; git commit -m “update” 12ab56 3eee8 12
  29. Git: Branching Local 3ddf4 master Branches: they are pointers to

    commits / versions Default branch: master 13
  30. Git: Branching Local 3ddf4 master Branches: they are pointers to

    commits / versions Default branch: master $ git branch frontend frontend 13
  31. Git: Branching Local 3ddf4 HEAD master Branches: they are pointers

    to commits / versions Default branch: master $ git branch frontend frontend HEAD: pointer to the current branch 13
  32. Git: Branching Local 3ddf4 HEAD master Branches: they are pointers

    to commits / versions Default branch: master $ git branch frontend frontend HEAD: pointer to the current branch $ git checkout frontend 13
  33. Git: Branching Local 3ddf4 HEAD Branches: they are pointers to

    commits / versions Default branch: master $ git branch frontend HEAD: pointer to the current branch $ git checkout frontend master frontend 13
  34. Git: Branching Local 3ddf4 HEAD Branches: they are pointers to

    commits / versions Default branch: master $ git branch frontend 12ab56 HEAD: pointer to the current branch $ git checkout frontend master frontend $ git commit -a -m “commit everything” 13
  35. Git: Branching Local 3ddf4 HEAD Branches: they are pointers to

    commits / versions Default branch: master $ git branch frontend 12ab56 3eee8 HEAD: pointer to the current branch $ git checkout frontend master frontend $ git commit -a -m “commit everything” 13
  36. Git: Branching Local 3ddf4 HEAD Branches: they are pointers to

    commits / versions Default branch: master 12ab56 3eee8 HEAD: pointer to the current branch master frontend 14
  37. Git: Branching Local 3ddf4 HEAD Branches: they are pointers to

    commits / versions Default branch: master $ git checkout master 12ab56 3eee8 HEAD: pointer to the current branch master frontend 14
  38. Git: Branching Local 3ddf4 HEAD master Branches: they are pointers

    to commits / versions Default branch: master $ git checkout master frontend 12ab56 3eee8 HEAD: pointer to the current branch 14
  39. Git: Branching Local 3ddf4 HEAD master How to merge master

    with the changes in frontend? $ git merge frontend frontend 12ab56 3eee8 HEAD: pointer to the current branch 15
  40. Git: Branching Local 3ddf4 HEAD master How to merge master

    with the changes in frontend? $ git merge frontend frontend 12ab56 3eee8 HEAD: pointer to the current branch 15
  41. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 HEAD master Commits: 12ab56 and 3eee8 add a line in same file
  42. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Commits: 12ab56 and 3eee8 add a line in same file
  43. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Conflicts: 12ab56 and 3eee8 update same line Commits: 12ab56 and 3eee8 add a line in same file
  44. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Conflicts: 12ab56 and 3eee8 update same line Commits: 12ab56 and 3eee8 add a line in same file
  45. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Conflicts: 12ab56 and 3eee8 update same line Commits: 12ab56 and 3eee8 add a line in same file
  46. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Conflicts: 12ab56 and 3eee8 update same line Commits: 12ab56 and 3eee8 add a line in same file
  47. + Test test + This is my first line Git:

    Conflicts Local 3ddf4 $ git merge frontend frontend 12ab56 3eee8 17 6664f HEAD master Conflicts: 12ab56 and 3eee8 update same line Commits: 12ab56 and 3eee8 add a line in same file
  48. Git conflicts: Recipe $ git add <name-of-files-with-conflicts> $ git commit

    When you get conflicts: 1. fix the conflicts 2. 3.
  49. Github ❤ Git • So far, your changes are in

    local computer • Problem: What if you drop the computer? Work in Teams? 20
  50. Github ❤ Git • So far, your changes are in

    local computer • Problem: What if you drop the computer? Work in Teams? • Solution: Github saves your work in the “cloud” 20
  51. Github ❤ Git • Push your local changes to your

    remote repository $ git push origin master Name of the remote url: by default, it’s always called origin Name of your branch: your initial branch is always called master 21 Remote $ git remote -v
  52. Github ❤ Git • Pull remote changes to your local

    repository $ git pull origin master Name of the remote url: by default, it’s always called origin Name of the local branch to pull into: your initial branch is always called master 22 Remote $ git remote -v
  53. 3ddf4 HEAD master The remote end is similar to a

    branch $ git commit -am “commit everything” origin/master HEAD: pointer to the current branch 23 Github ❤ Git Remote
  54. 3ddf4 HEAD master The remote end is similar to a

    branch $ git commit -am “commit everything” origin/master 12ab56 HEAD: pointer to the current branch 23 Github ❤ Git Remote
  55. 3ddf4 HEAD master The remote end is similar to a

    branch $ git commit -am “commit everything” origin/master 12ab56 3eee8 HEAD: pointer to the current branch 23 Github ❤ Git Remote
  56. 3ddf4 HEAD master The remote end is similar to a

    branch $ git commit -am “commit everything” origin/master 12ab56 3eee8 HEAD: pointer to the current branch 23 Github ❤ Git Remote $ git push origin master
  57. 3ddf4 HEAD master The remote end is similar to a

    branch $ git commit -am “commit everything” origin/master 12ab56 3eee8 HEAD: pointer to the current branch 23 Github ❤ Git Remote $ git push origin master
  58. Centralised recipe $ git pull origin master Easy start: everyone

    works on the same branch $ git add <files> Do some work $ git commit -m “<message>” $ git push origin master rinse and repeat Works until you get conflicts
  59. 3ddf4 HEAD master 26 Conflicts in Centralised recipe @kikofernandez 3ddf4

    origin/master Github 3ddf4 HEAD master origin/master origin/master @kikofernandez @supercooldave @supercooldave
  60. 3ddf4 HEAD master 26 Conflicts in Centralised recipe @kikofernandez 3ddf4

    origin/master Github 3ddf4 HEAD master origin/master origin/master $ git commit -am “stuff” @kikofernandez @supercooldave @supercooldave
  61. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master origin/master origin/master $ git commit -am “stuff” @kikofernandez @supercooldave @supercooldave
  62. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master origin/master origin/master $ git commit -am “stuff” $ git commit -am “work” @kikofernandez @supercooldave @supercooldave
  63. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git commit -am “stuff” $ git commit -am “work” @kikofernandez @supercooldave @supercooldave
  64. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” @kikofernandez @supercooldave @supercooldave
  65. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” 12ab56 @kikofernandez @supercooldave @supercooldave
  66. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave @supercooldave
  67. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave @supercooldave error: failed to push some refs to '/path/to/repo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  68. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave @supercooldave
  69. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave $ git pull origin master @supercooldave
  70. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave $ git pull origin master 12ab56 87abe @supercooldave
  71. 3ddf4 HEAD master 12ab56 26 Conflicts in Centralised recipe @kikofernandez

    3ddf4 origin/master Github 3ddf4 HEAD master 3eba4 origin/master origin/master $ git push origin master $ git commit -am “stuff” $ git commit -am “work” $ git push origin master 12ab56 @kikofernandez @supercooldave $ git pull origin master 87abe $ git push origin master 12ab56 87abe 3eba4 @supercooldave
  72. Useful links • Conflict example (for you to practice) •

    Git - the simple guide • Github: Bootcamp • Git: Cheatsheet • Learn enough git to be dangerous • CodeSchool: Try git 27