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

GDI Git

David Baumgold
September 16, 2016

GDI Git

An introduction to Git and GitHub

David Baumgold

September 16, 2016
Tweet

More Decks by David Baumgold

Other Decks in Technology

Transcript

  1. GIT & GITHUB
    David Baumgold
    @singingwolfboy
    INTRO TO VERSION CONTROL WITH

    View Slide

  2. WELCOME!
    ➤ We are here for you!
    ➤ Every question is important
    ➤ Help each other
    ➤ Have fun!
    Girl Develop It is here to provide affordable and
    accessible programs to learn software through
    mentorship and hands-on instruction.

    View Slide

  3. INTRODUCTIONS
    ➤ Instructor: David Baumgold (call me “DB”!)

    @singingwolfboy on GitHub
    ➤ TA: Christine Chapman

    @czchapma on GitHub
    ➤ TA: Shannon Coyne

    @shannoncoyne on GitHub
    ➤ Slides: https://speakerdeck.com/singingwolfboy/gdi-git

    View Slide

  4. PRE-WORK
    ➤ Install Git
    ➤ Install a text editor with shell commands (Atom is a good choice)
    ➤ Optional: Install zsh and oh-my-zsh
    ➤ Learn how to use the command line
    ➤ Make an account with GitHub.com
    ➤ Optional: Make an account on GitLab.com
    If you have not already done these things, you should go to
    https://github.com/singingwolfboy/git-tutorial-prework
    and do them!
    (Scroll down to see links to specific instructions for

    Windows, Mac, or Linux computers)

    View Slide

  5. WHAT IS VERSION CONTROL?
    ➤ Ability to always go back to an old version
    ➤ Track metadata about changes: who, when, and why?
    ➤ Allow multiple editors to make changes simultaneously
    without stepping on each others’ toes
    You know how you save

    multiple copies of a file as you edit it,

    so you can go back to an earlier version?

    Version control is kind of like that, but a lot better.

    View Slide

  6. AGENDA
    ➤ Install and configure Git
    ➤ Learn basic shell commands (bash or zsh)
    ➤ Create a Git repository on your computer
    ➤ Make commits to track changes in your repo
    ➤ Go back to an earlier version of your project
    ➤ Host your Git repository on GitHub
    ➤ Suggest changes to other repositories on GitHub
    ➤ Accept other people’s suggested changes to your repo

    View Slide

  7. NON-AGENDA: WHAT WE’RE NOT DOING
    ➤ Git branches
    ➤ Merge conflicts
    ➤ Rebase and changing history
    ➤ Code reviews
    ➤ Automated testing (Travis CI)
    ➤ Anything super complicated :)
    Let’s just stick to the basics!

    View Slide

  8. Elisabeth Robson & Eric Freeman
    A learner’s guide to Git
    Head First
    Git
    David Baumgold
    A Brain-Friendly Guide
    Learn how
    to travel
    through
    time
    Editing has never
    been so enlightening
    Compatible
    with all kinds
    of text, not
    just software
    Track down issues
    at their source
    Discover the
    freedom in
    branching,
    forking,
    & merging
    Change history without
    tying yourself in knots

    View Slide

  9. SETUP
    Install Git, learn shell

    View Slide

  10. WINDOWS
    ➤ Download and install Babun: babun.github.io
    ➤ Babun is a bundle of several different pieces of software,
    including Git
    ➤ You will run Babun instead of the default Windows command
    prompt

    View Slide

  11. MAC
    ➤ Download and install Homebrew: http://brew.sh
    ➤ Homebrew is a package manager that will help you install
    other software easily
    ➤ Use Homebrew to install Git: brew install git
    ➤ You can use Homebrew to install lots of other software as
    well, in the future

    View Slide

  12. LINUX
    ➤ Install Git from your system package manager
    ➤ Ubuntu: sudo apt-get install git

    View Slide

  13. ALL: TEXT EDITOR
    ➤ Make sure you have a text editor installed — one that can be
    invoked using shell commands
    ➤ I recommend Atom: atom.io
    ➤ Free, cross-platform
    ➤ Installing shell commands is easy, and can be done from
    the menu
    ➤ Can also use Sublime Text: sublimetext.com
    ➤ Unlimited free trial, costs $70, cross-platform
    ➤ Installing shell commands is more complicated

    View Slide

  14. SHELL
    ➤ The “shell” is the user interface to the raw power of your
    computer
    ➤ Runs inside of a terminal program: Babun on Windows,
    Terminal.app on Mac, Terminal or Konsole on Linux
    ➤ Two popular shells, very similar: bash and zsh
    ➤ bash is more widely used, zsh is more powerful
    ➤ Both can integrate with Git (zsh does by default)

    View Slide

  15. LOOKING AROUND THE SHELL
    ➤ First shell command: ls (that’s a lowercase L, not a one)
    ➤ Stands for “list” — used to list files on your computer
    ➤ Open your terminal program, type ls, press enter

    View Slide

  16. FINDING WHERE YOU ARE
    ➤ Second shell command: pwd
    ➤ Stands for “print working directory”: shows you where you
    are in your computer
    ➤ In your terminal program, type pwd, press enter
    Open that directory in the Windows Explorer or Finder.

    The files are the same!

    View Slide

  17. MOVING AROUND
    ➤ Second shell command: cd
    ➤ Stands for “change directory”: used to move to a different
    folder/directory on your computer
    ➤ In your terminal program, type cd Desktop, press enter
    No response, but try running ls again, and see what happens!

    View Slide

  18. COMMAND ARGUMENTS
    ➤ Some commands take arguments, which specify how a
    command should work
    ➤ cd takes an argument for where to move to
    ➤ ls takes an optional argument, to look into a directory
    without moving into it
    ➤ When run without arguments, cd will move you to your home
    directory
    cd Desktop
    command argument

    View Slide

  19. COMMAND FLAGS
    ➤ Some commands take flags, which modify the way a
    command works, usually in smaller ways than an argument
    ➤ Flags start with a dash
    ➤ ls takes an -a flag, which displays all files, even hidden ones
    ➤ Also takes lots of other flags, as do other commands

    View Slide

  20. THE BASICS
    Create a repo, make some commits

    View Slide

  21. MAKE A REPO
    $ git init cookbook
    ➤ git init creates a new, empty repo
    ➤ Optional argument is the name of the directory to create;

    otherwise the repo will be created in the current directory
    ➤ Creates a .git directory that stores all the repo information

    View Slide

  22. EXPLORE THE REPO
    $ ls .git
    HEAD info/
    config objects/
    description refs/
    hooks/
    Mostly, you shouldn’t need to touch the contents of the

    .git directory, but there is one very useful file: .git/config

    View Slide

  23. GIT CONFIG
    $ git config []
    ➤ git config allows you to read and write values

    to Git’s config files
    ➤ By default, it uses .git/config

    Use the --global flag to use ~/.gitconfig
    ➤ You’ll need to set your name and email address:
    $ git config --global user.name "Alice Developer"

    $ git config --global user.email "[email protected]"

    View Slide

  24. GIT CONFIG
    $ git config --global core.editor "atom --wait"
    You should also tell Git what text editor you want to use:
    Atom:
    $ git config --global core.editor "subl --wait"
    Sublime Text:
    This relies on shell commands to launch your editor from

    the shell. Try running atom or subl in the shell, and make

    sure it causes your editor to pop up!

    View Slide

  25. MAKE A FILE
    ➤ Find a delicious recipe, copy the text, paste it into a file

    in your “cooking” Git repo. For example:
    S'mores Recipe
    Ingredients:
    1 large marshmallow
    1 graham cracker
    1 bar of chocolate
    Directions:
    1. Heat the marshmallow over an open flame

    until it begins to brown and melt.
    2. Break the graham cracker in half.

    Sandwich the chocolate between the cracker

    and the hot marshmallow. Allow the marshmallow

    to cool a moment before eating.
    smores.txt

    View Slide

  26. GIT STATUS
    $ git status
    ➤ git status tells you the status of the files in your repo:

    what is new, what has changed, what’s been deleted
    ➤ git status is always safe to run, and will never change

    or break anything in your repo
    ➤ My most frequently used Git command!

    View Slide

  27. GIT ADD
    $ git add smores.txt
    ➤ git add tells Git that you want a certain file added to the

    repository in an upcoming commit
    ➤ Marks the file to be included as it is at that point in time.

    Picks up on changes since the last commit, but won’t pick

    up on changes you make after running git add — you’ll

    need to re-add the file to include those changes!
    ➤ Adding a directory will add all files in that directory.

    Use git add . to add all files at once!

    View Slide

  28. GIT COMMIT
    $ git commit
    ➤ git commit creates a new commit based on the files

    you’ve marked using git add
    ➤ A commit message is required: Git will open your text

    editor so you can write one
    ➤ You can use the -m flag to provide a message on the

    command line, instead

    View Slide

  29. COMMITTING CHANGES
    To commit a change to the repo, do the following:
    ➤ Edit files and save changes
    ➤ git add
    ➤ git commit
    Special case: to delete a file from the repo, use
    $ git rm
    exactly like how you would use git add

    View Slide

  30. COMMANDS FOR MAKING COMMITS
    ➤ git init
    ➤ git status
    ➤ git add
    ➤ git rm
    ➤ git commit

    View Slide

  31. VIEWING THE COMMIT LOG
    $ git log
    ➤ git log displays a log of all the commits,

    from most recent to least recent
    ➤ Use the arrow keys to scroll, press “Q” to quit

    View Slide

  32. GIT SHOW
    $ git show
    ➤ git show displays detailed information about a particular commit
    ➤ The hash argument is optional, defaults to current commit
    ➤ Use the arrow keys to scroll, press “Q” to quit

    View Slide

  33. VIEWING A DIFF
    $ git diff
    ➤ git diff shows the changes you’ve made that haven’t yet

    been committed
    ➤ Useful for checking to be sure that you’ve made all the

    changes you intend, and none of the ones you didn’t
    ➤ Use the --staged argument to view changes that you’ve

    already added using git add
    ➤ Use the arrow keys to scroll, press “Q” to quit

    View Slide

  34. BROWSING HISTORY
    $ git checkout
    ➤ git checkout allows you to view your project

    as it was at a particular commit
    ➤ You will get a big scary “detached HEAD” warning:

    ignore it. (This is related to branches in Git.)
    ➤ To get back to your most recent commit, use master

    instead of a hash, like this:
    $ git checkout master
    Do this before making any new commits!

    View Slide

  35. COMMANDS FOR VIEWING COMMITS
    ➤ git log
    ➤ git show
    ➤ git diff
    ➤ git checkout

    View Slide

  36. ALL THE GIT COMMANDS WE KNOW SO FAR
    git log
    git show
    git diff
    git checkout
    git init
    git config
    git status
    git add
    git rm
    git commit
    making commits viewing commits

    View Slide

  37. COLLABORATION
    Sharing commits across repos

    View Slide

  38. CREATE REPO ON GITHUB
    ➤ Visit GitHub.com, log in, and click on the plus sign at the top
    to make a new repository

    View Slide

  39. View Slide

  40. View Slide

  41. GIT REMOTES
    my
    repo
    https://github.com/
    singingwolfboy/cookbook.git
    https://yoursite.com/
    cookbook.git
    https://github.com/
    example/cookbook.git
    https://gitlab.com/
    singingwolfboy/
    cookbook.git

    View Slide

  42. GIT REMOTES
    my
    repo
    origin
    yoursite
    example
    gitlab

    View Slide

  43. GIT REMOTES
    $ git remote add
    ➤ git remote creates short nicknames for URLs that point to

    other Git repos that you want to share commits with
    ➤ Run git remote -v to list all the remotes that the repo knows about
    ➤ Remotes are saved in the .git/config file: you can edit it, if you want

    View Slide

  44. GIT PUSH
    $ git push
    ➤ git push sends commits from your repo to another repo
    ➤ Can specify a remote; if not, pushes to whatever remote is set as default
    $ git push -u origin master
    This is what GitHub tells you to run
    ➤ -u origin sets the “origin” remote as the default remote
    ➤ master is a branch, and it’s the same thing we use to get back to the

    most recent commit. Branches are too complicated to cover in this class.

    View Slide

  45. CREATING COMMITS ON GITHUB
    Step 1
    Step 2
    Step 3

    View Slide

  46. GIT PULL
    $ git pull
    ➤ git pull gets commits from another repo to your repo, and updates

    your files with those changes
    ➤ Can specify a remote; if not, pulls from whatever remote is set as default

    View Slide

  47. GIT CLONE
    $ git clone
    ➤ git clone makes a local copy of a repo

    that is available at the given URL
    ➤ The local copy is a full, complete repo,

    with all commits and history
    ➤ Local copy has a remote named origin,

    which is set to the original URL
    ➤ The local copy is yours: you can change it

    however you want!
    Taking an existing project and making your own changes

    is called “forking”, and it happens all the time with Git

    View Slide

  48. COMMANDS FOR COLLABORATION
    ➤ git clone
    ➤ git remote
    ➤ git push
    ➤ git pull

    View Slide

  49. ALL THE GIT COMMANDS WE KNOW SO FAR
    git clone
    git remote
    git push
    git pull
    git log
    git show
    git diff
    git checkout
    git init
    git config
    git status
    git add
    git rm
    git commit
    making commits viewing commits collaborating

    View Slide

  50. RUNNING INTO PROBLEMS
    ➤ Clone the repo at

    https://github.com/singingwolfboy/tutorial-cookbook
    ➤ Create a new file in the repo with a new recipe. Write out
    your favorite recipe, or find a random one on Google and
    copy-paste it in.
    ➤ Commit your new recipe, and try to push it up to the GitHub
    repo you that cloned from!
    (Spoiler alert: it won’t work. Why not? What is the error message?)

    View Slide

  51. PULL REQUESTS
    1. Fork the repo, so you have control over your fork
    2. Make commits in your fork to change it however you want
    3. Contact the owner of the upstream repo, show your changes,
    ask the owner to pull your changes into upstream repo
    GitHub has a special interface for managing these requests,

    which they call Pull Requests

    View Slide

  52. View Slide

  53. View Slide

  54. MAKING PULL REQUESTS
    ➤ Fork the repo at

    https://github.com/singingwolfboy/tutorial-cookbook
    ➤ Clone your forked repo to your computer
    ➤ In your forked repo, create a new file with a new recipe, and
    commit it
    ➤ Push your commit to your fork on GitHub
    ➤ Make a pull request from your fork to my repo (known as the
    “upstream” repo)

    View Slide

  55. MERGING PULL REQUESTS
    ➤ If someone makes a pull request into your repo, you can
    review and merge it!
    ➤ Just click the green “Merge pull request” button, and you’re
    done!
    ➤ The suggested changes will become actual changes, and will
    show up in the repo — both on GitHub, and in Git clones
    (after they pull the latest changes)

    View Slide

  56. OTHER GIT HOSTING
    ➤ GitHub is not the only website you can use to host repos
    ➤ GitLab, BitBucket: free private repos
    ➤ GitLab is open source: can run it on your own server
    ➤ It’s all Git, so you can clone from GitHub and push to GitLab
    (or vice versa)

    View Slide

  57. WORKFLOWS
    Reviewing what you’ve learned

    View Slide

  58. CREATE A NEW REPO ON GITHUB
    ➤ git init to create a new repo on your computer
    ➤ Create some files
    ➤ git add to mark those files as ready to be committed
    ➤ git commit to write a commit message
    ➤ Create a repo on GitHub with the same name
    ➤ git remote add to link your local repo with the GitHub repo
    ➤ git push to send your commits to the linked GitHub repo

    View Slide

  59. EDIT YOUR OWN REPO ON GITHUB
    ➤ git clone to pull the repo down to your computer
    ➤ Edit some files
    ➤ git diff to check your changes
    ➤ git add to mark those changes as ready to be committed
    ➤ git commit to write a commit message
    ➤ git push to send your commits back to GitHub

    View Slide

  60. EDIT SOMEONE ELSE’S REPO ON GITHUB
    ➤ Click the “Fork” button on the GitHub repo
    ➤ Now you have your own copy of that repo! Make changes to
    your own copy of the repo, and push them back to GitHub
    ➤ Click the “New pull request” button on GitHub (on your copy
    of the repo)
    ➤ Write a description explaining why they should accept your
    changes
    ➤ Respond to comments/feedback, and hope that the other
    person decides to merge your pull request!

    View Slide

  61. CONGRATS!
    YOU KNOW THE BASICS
    OF GIT AND GITHUB!

    View Slide

  62. STILL LOTS MORE TO LEARN
    ➤ Git branches allow you to have multiple edits in progress
    simultaneously
    ➤ Allows you to create multiple pull requests between repos
    on GitHub!
    ➤ Merge conflicts occur when two people try to make two
    incompatible changes to the same file
    ➤ Who’s right? Git lets you decide!
    ➤ GitHub can run automated tests for you on your pull requests,
    so you know if a proposed change will break your code
    ➤ The green checkmark gives you confidence!

    View Slide

  63. FURTHER HELP AND DOCUMENTATION
    ➤ GitHub has some great guides: help.github.com
    ➤ Git’s documentation is thorough, but dense: git-scm.com/doc
    ➤ Git: The Simple Guide: rogerdudler.github.io/git-guide
    Questions? Email me:

    [email protected]
    Want to get my book?

    davidbaumgold.com/book

    View Slide