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. 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.
  2. 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
  3. 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)
  4. 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.
  5. 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
  6. 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!
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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)
  12. 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
  13. 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!
  14. 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!
  15. 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
  16. 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
  17. 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
  18. 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
  19. GIT CONFIG $ git config <key> [<value>] ➤ 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]"
  20. 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!
  21. 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
  22. 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!
  23. 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!
  24. 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
  25. COMMITTING CHANGES To commit a change to the repo, do

    the following: ➤ Edit files and save changes ➤ git add <files> ➤ git commit Special case: to delete a file from the repo, use $ git rm <file> exactly like how you would use git add
  26. COMMANDS FOR MAKING COMMITS ➤ git init ➤ git status

    ➤ git add <file> ➤ git rm <file> ➤ git commit
  27. 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
  28. GIT SHOW $ git show <hash> ➤ 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
  29. 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
  30. BROWSING HISTORY $ git checkout <hash> ➤ 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!
  31. 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
  32. CREATE REPO ON GITHUB ➤ Visit GitHub.com, log in, and

    click on the plus sign at the top to make a new repository
  33. GIT REMOTES $ git remote add <name> <url> ➤ 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
  34. 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.
  35. 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
  36. GIT CLONE $ git clone <url> ➤ 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
  37. 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
  38. 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?)
  39. 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
  40. 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)
  41. 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)
  42. 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)
  43. 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
  44. 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
  45. 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!
  46. 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!
  47. 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