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

Getting Started with Git

Getting Started with Git

A presentation I put together for the class at Betamore. Slides include speaker notes. License: CC BY-NC-SA

Jared Koumentis

February 17, 2013
Tweet

More Decks by Jared Koumentis

Other Decks in Programming

Transcript

  1. ➜ ~ awesome terminal code ➜ ~ type it in

    to do stuff This is how I show code you can type in your terminal. I use Oh-My-ZSH, which is where the little pink arrow comes from. The ~ (tilde) is where my current directory is shown. ~ is “home” in unix like systems.
  2. Your First Repository Getting to your first repo. Quick overview

    of installing git and getting to your first repo.
  3. Sharing Your Repository Sharing your repository with others. Setting up

    a git remote. Making your first push. Collaborating.
  4. Your First Repository Now, let’s dive straight in. Let’s start

    with what Git is and how to get it. Then we’ll make your first repo.
  5. Your Favorite Package Manager Homebrew Apt or... First, let’s install

    git. If you use a Mac or Linux machine, it’s pretty straightforward. “brew install git” or “sudo apt-get install git” should get you up and running.
  6. git-scm.org/download If you don’t use a package manager (though you

    should), just grab git from the download page at git-scm.org. They have packages for Mac, Linux, and Windows.
  7. ~/git ➜ ~ mkdir git ➜ ~ cd git Git

    directory in your home folder. You really don’t want a mess of repos all over your machine, or having to search for a repo when you need to work on a project.
  8. Initialize the Repository ➜ git git init myproject or ➜

    git cd myproject ➜ myproject git init Two ways. New project or existing project. The top one creates an empty directory with a .git folder in it. The bottom one creates a .git folder in the current directory.
  9. What... That’s it? The .git folder is where all the

    magic happens. Remember how everything in a DVCS is available to every repository. It’s all stored in that .git folder.
  10. Undoing the Initialization If you’d like to remove a project

    on your system from git version control, just delete the .git folder inside your project. You may have to show hidden folder to see it, but that’s all there is to it.
  11. What is a commit? I like to think of a

    commit as a snapshot. It saves your code at a particular point in time. You can then go back and view your code at any committed point in time. There are more technical details behind it, but you don’t need to know about those to use git. (Talk to me after if you’d like.)
  12. Initial Commit ➜ myproject git status ➜ myproject git add

    . ➜ myproject git commit -m “Initial Commit” “git status” is a command you should use a lot. It let’s you see what state git is in. “git add .” adds all the new files. The “.” is kinda like a wildcard. You can also specify a particular file. “git commit -m ‘message’” is a short hand for making a commit and including the message inline. Without -m and a message, it will pop up a text editor and ask for the commit message.
  13. Making More Commits ➜ myproject git status ➜ myproject git

    add . ➜ myproject git commit -m “Fix issue #1337 with awesome code” You essentially repeat the same loop for each commit.
  14. What Makes a Good Commit Message? The convention is to

    use present tense. Ideally, since most people work with git in a terminal, it’s best to keep the title short. (72 characters, if you’re counting.) If you need a really long message, just do...
  15. ➜ myproject git commit Unless you’ve setup a separate editor

    to use with git, you’ll be met by vim. Don’t worry. It doesn’t bite. Press “I” to go into insert mode, type out your title in the first line, then type a longer message on the lines below that. Don’t worry about all the commented stuff, git ignores it.
  16. But... I Don’t Want to Use Vim. ➜ myproject git

    config --global core.editor mate You can use a config option to set some other text editor. Any plain text editor will do. Here, I’ve indicated mate, or textmate2. You can use whatever you’d like though.
  17. Reviewing History The whole point of keeping things in version

    control is so that you can review history. The key part of git you’ll use for that is the “git log”.
  18. Git Log ➜ myproject git log commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared

    Koumentis <[email protected]> Date: Mon Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color When you do a git log on a project with any significant amount of history, you’ll see a bunch of entries like this. Let’s break this down.
  19. Git Log ➜ myproject git log commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared

    Koumentis <[email protected]> Date: Mon Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color This long string of characters is the commit hash. It’s a unique identifier for this particular commit.
  20. Git Log ➜ myproject git log commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared

    Koumentis <[email protected]> Date: Mon Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color Here, we see the person who made this commit.
  21. Git Log ➜ myproject git log commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared

    Koumentis <[email protected]> Date: Mon Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color When the commit was made...
  22. Git Log ➜ myproject git log commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared

    Koumentis <[email protected]> Date: Mon Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color And the commit message...
  23. ➜ myproject git config --global color.ui true This little command

    colorizes more than just your logs, but it’s a good thing to have on if you’d like colors.
  24. Turns This... commit 594ea872e95326dfe4c1f9412043af1d9860ac1c Author: Jared Koumentis <[email protected]> Date: Mon

    Jan 7 01:56:54 2013 -0500 finish copy and standardize email button color It turns this...
  25. ➜ myproject git log --online Gives Us... 594ea87 finish copy

    and standardize email button color We have a --online option, if we’d like something a little less verbose.
  26. It’s pretty simple. Red is a removal. Green is an

    addition. However, it shows whole lines that changed... I wonder if we can make this even easier to read.
  27. When Should I Branch? I’ve been asked this many times

    by people. If you’re coming from a centralized version control system, like SVN, branching isn’t something you’re used to doing. However, in git, it’s a common practice.
  28. You Should Branch... ALL the Time For ALL the Things

    Any time you’re adding a feature, fixing a bug, updating copy, fixing a spelling error... Always branch. Why?
  29. Pull Requests Pull Requests are kinda like asynchronous awesome code

    review. Supported by both GitHub and BitBucket, you make a branch, do your changes, and then publish your branch. I’m going to make you wait though. We’ll cover this right after branching.
  30. ➜ myproject git branch my_branch ➜ myproject git checkout my_branch

    Or... ➜ myproject git checkout -b my_branch To create a new branch, use the “git branch” command. You can then checkout that branch. If you’re wanting to create and checkout a new branch right away, doing a “checkout -b” will get that done immediately. Checkout can be used to move between branches.
  31. Do Your Work Make Small Commits Since you’ve made a

    branch, you’re essentially working in your own sandbox, separate from the “Master” branch of your copy of the code. Feel free to experiment. Commit early and often. Preferable, commit with every little “unit” of work that you do with your code. That way, you’ve got a nice history explaining why you’ve made the changes you have.
  32. Sharing Your Repository Sharing your repository with others. Setting up

    a git remote. Making your first push. Collaborating with Pull Requests.
  33. GitHub BitBucket github.com bitbucket.org The two big players in git

    hosting. There are numerous smaller ones, however, these two are the ones most people use. Their main differences are in their pricing models. If you’re looking to do open source work, GitHub is probably the best option. If you’re looking to integrate into other Atlassian tools you use, BitBucket is probably better.
  34. Setting Up A Git Remote Regardless of which service you

    use for hosting your git repositories, adding a git remote to your local git repo is easy.
  35. ➜ myproject git remote add <name> <url> Anything You Want

    You can set the name to be anything you want. I’d suggest it be a short and easy to remember name, as you’ll be using it a lot.
  36. ➜ myproject git remote add <name> <url> Your Host Gives

    You This The URL will be something you get from your host.
  37. ➜ myproject git remote add origin <url> Origin is the

    standard name used for your “point of truth” repo. Most git hosting sites will ask you to set them up as “origin”, though you can make it any name you’d like.
  38. Forking Many people have heard of forking. Forking, when talking

    about open source projects, was often a bad thing, in the past. It meant that the group working on the project had decided to go in different directions. Forking sometimes corresponded with the death of the original project.
  39. Forking No Longer A Bad Thing Many people have heard

    of forking. Forking, when talking about open source projects, was often a bad thing, in the past. It meant that the group working on the project had decided to go in different directions. Forking sometimes corresponded with the death of the original project.
  40. What is a Fork? A fork is like a server

    side copy of a repo. It’s used when a person would like their own sandbox that they can push changes to.
  41. Fork + Pull Request = Awesome You see, with a

    fork, you can make your changes on a branch, like we talked about before. Then, you can submit a pull request back to the original project. The key is that this make Forking a project a positive thing. Forking is good when you contribute back to the original project.
  42. Get Your Code Online. Share. Whether you use GitHub or

    BitBucket, getting your code online and letting people fork and submit pull requests will help you develop a community. Also, as they say, many eyes make for shallow bugs.
  43. ➜ myproject git remote add origin <url> ➜ myproject git

    push -u origin master --all This is all you need to publish your code. Each service out there will help lead you through these steps. Let’s break down what’s happening.
  44. ➜ myproject git remote add origin <url> ➜ myproject git

    push -u origin master --all The -u option tells git to “track the upstream” branch. Basically, in the future, when you do a plain “git push” and you’re on the master branch, it knows you want to push to the master branch.
  45. ➜ myproject git remote add origin <url> ➜ myproject git

    push -u origin master --all We’ve already seen the origin name, as that’s the short hand for our server. This “master” bit here is saying we want to push to the master branch.
  46. ➜ myproject git remote add origin <url> ➜ myproject git

    push -u origin master --all Last but not least, this --all tells git to push everything we’ve got up there. This makes sure that your initial push includes all the stuff you might want to share with other people. If you don’t want ALL the things pushed, you can leave this out and it will only push your master branch.
  47. ➜ myproject git push origin <branch_name> Publishing a Branch If

    you’ve been working on a feature branch (like you should be doing), you can then push that branch up to your server with this.
  48. But... Why Publish A Branch? Why would you want to

    do this? Once you publish a branch, you can submit a pull request from that branch to the master branch.
  49. Forking + Branch + Pull Request Branch + Pull Request

    Do this if you don’t have push access to the original repo.
  50. Forking + Branch + Pull Request Branch + Pull Request

    Do this if you do have access to the original repo.