$30 off During Our Annual Pro Sale. View Details »

Working with GIT

ONOS Project
February 11, 2015

Working with GIT

Working with GIT - presented at ONF Member Workdays
By Brian O'Connor, ON.Lab

ONOS Project

February 11, 2015
Tweet

More Decks by ONOS Project

Other Decks in Technology

Transcript

  1. Brian O'Connor
    Open Networking Lab
    2/11/2015
    Working with

    View Slide

  2. What this talk will cover
    Git Basics
    - Getting started
    - Basic workflow and commands
    GitHub
    - Getting started
    - Anatomy of a project
    - How to contribute

    View Slide

  3. Disclaimer
    This will be a bit of a whirlwind tour,
    and I will borrow some of the materials from
    git-scm.com and github.com
    There are plenty of in-depth (and better)
    tutorials and videos that get into a lot of the
    details that I may gloss over.

    View Slide

  4. Why Version Control?
    ● For Version History
    ○ Allows you to go back and revert changes and
    recover or compare against old version
    ○ Makes experimentation easier (using branches)
    ● To Facilitate Collaboration
    ○ Allows you to make modifications to files in parallel
    as a team and merge the result

    View Slide

  5. What is Git?
    vs.
    CVS, Subversion, Perforce Git, Mercurial, Bazaar

    View Slide

  6. How do I start?
    ● A brand new project
    ○ git init # in the project's top level directory
    ● An existing project (more common)
    ○ git clone [URL]
    ○ git clone https://github.com/mininet/mininet.git
    Note: Projects will contain a .git directory that will track will contain all
    changes and metadata. Also, if you don't have git installed, do this first:
    https://help.github.com/articles/set-up-git/

    View Slide

  7. Branching
    ● master
    ○ default branch when you check out a project
    ○ usually "stable" and "deployable"
    ● topic branches
    ○ best practice for changes
    ○ local only, unless you push them somewhere
    git checkout -b my-feature master # to create
    git checkout master # to switch branches

    View Slide

  8. Committing Changes
    1. Make a change to a file
    2. Look at what's changed (optional)
    ○ git status
    3. Stage your files for commit
    ○ git add [file name]
    4. Commit!
    ○ git commit # opens the default editor
    Shortcut: git commit -a -m"Commit Message"

    View Slide

  9. Anatomy of a commit
    commit ea9ae214f9bca97537378f205910d62b7aa4be47
    tree de130618eaf06592df72b43b06e88ea87160d8b6
    parent 0cc189e6814cb0bf5af049fd6074212a2599a571
    Author: Brian O'Connor
    Date: Fri Feb 6 16:41:49 2015 -0800
    Using CommandSession console instead of System.*
    in AbstractShellCommand print and error
    fixes ONOS-986
    diff --git a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
    b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
    index e6a62d0..b404db6 100644
    --- a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
    +++ b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
    @@ -62,7 +62,7 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport
    {
    * @param args arguments
    */
    public void print(String format, Object... args) {
    - System.out.println(String.format(format, args));
    + session.getConsole().println(String.format(format, args));
    }
    /**
    commit hash (SHA1)
    author information
    commit message
    commit diff
    git show [commit hash | branch | HEAD]

    View Slide

  10. Looking at the past
    git log [branch | commit hash]
    git log --oneline --graph --decorate=short
    * 0094997 (tag: 2.2.0b3) fixing install-mininet-vm.sh
    * 7a411b6 Merge pull request #453 from cdburkard/master
    |\
    | * c2341cd update examples README with new examples
    |/
    * 4219b22 (tag: 2.2.0b2) 2.2.0b2
    * 08ab7e8 Merge pull request #452 from mininet/nat-cmd
    |\
    | * ab97dfa (origin/nat-cmd, nat-cmd) fixing no --nat issue
    | * af1ccf9 Updating NAT class to use gateway interface
    * | 3ef6bcf Additional info about --nat and LinuxRouter
    |/
    * 015cd9e Merge pull request #443 from cdburkard/devel/cluster
    |\
    | * bbf94cd use rcmd instead of quietRun when shutting down remote nodes
    * | 3d44bcd MiniNet -> Mininet
    * | 1817cbc Pass pyflakes
    * | e0bf8ec Minor code cleanup

    View Slide

  11. : the social network for code

    View Slide

  12. Getting Started on GitHub
    ● Create an Account
    ● Add your public key (strongly recommended)
    ○ makes pushing changes easier
    Get started @ https://github.com/

    View Slide

  13. Anatomy of a project page
    repository
    viewer
    commit
    log
    project forks
    issue tracker
    pending
    contributions
    project wiki
    download
    URL

    View Slide

  14. Pulling the latest changes
    ● git clone [URL]
    ○ create a new repository with the latest code
    ● git pull [remote] [branch]
    ○ gets the latest code for the specified branch on the
    remote and merges it into your current local branch
    ● git fetch [remote]
    ○ updates your local metadata with the latest
    information about the remote repository

    View Slide

  15. Dealing with merge conflicts
    ● Happens when two people change the same
    file in a conflicting way
    ○ Usually manifest themselves when you do git pull
    ● What you'll see
    ○ CONFLICT (modify/delete): ...
    # Automatic merge failed; fix conflicts and then commit the result.
    ● How to fix it
    ○ Update the offending file(s)
    ○ Stage the changes using git add [filename]
    ○ Commit using git commit

    View Slide

  16. Forking a project
    ● Your own copy of someone else's project
    ● Somewhere to push your local changes
    ○ And collaborate with others on them, too!
    ● Push your changes to GitHub
    ○ git push origin [branch name]
    ● To fork, click the "Fork" button on the
    upstream project page in GitHub
    ○ Creates a clone of the upstream in GitHub

    View Slide

  17. Keeping your fork up to date
    ● Set up remote
    ○ git remote add upstream [upstream URL]
    1. Fetch the latest from the remote
    ○ git fetch upstream
    2. Checkout your local master
    ○ git checkout master
    3. Merge the upstream changes
    ○ git merge upstream/master
    4. Push the changes to GitHub
    ○ git push origin master

    View Slide

  18. Pull requests
    ● Push the latest changes to your topic branch
    to GitHub
    ○ git push origin [branch]
    ● Click the "Compare & pull request" button
    on your fork's project page:

    View Slide

  19. Issue tracking and GitHub wiki
    ● Automatically built-in
    ● Issue tracker
    ○ Useful for reporting bugs, tracking new features, etc.
    ○ Automatically integrated using commit messages
    e.g. "Closes #843"
    ● Wiki
    ○ Great place to put project documentation
    e.g. installation instructions, tutorials, etc.
    ○ Editable by anyone with a GitHub account
    ○ Versioned

    View Slide

  20. Live Demo
    ● Fork a project
    ● Make a change and commit
    ● Pull and Merge conflicts
    ● Push to remote
    ● Create a pull request

    View Slide

  21. Other Topics
    ● .gitignore file
    ○ Tells git to ignore certain files or patterns, like build
    artifacts
    ● git stash
    ○ Useful for quickly stashing local changes while
    pulling/updating code
    ● git rebase
    ○ Replay your branch from a new parent
    ○ NEVER rebase code that's been pushed*
    ○ Used heavily in Gerrit workflow

    View Slide

  22. Merge vs. Rebase
    Note: NEVER rebase public (i.e. pushed to remote) branches
    Exception: Okay to rebase pending changes/commits in Gerrit
    Images from: https://www.atlassian.com/git/tutorials/merging-vs-rebasing/workflow-walkthrough
    git merge master git rebase master
    Feature Branch

    View Slide

  23. Resources
    Cheat Sheet
    https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf
    Collection of Tutorials
    https://help.github.com/articles/good-resources-for-learning-git-and-
    github/
    Fantastic (free!) eBook
    http://git-scm.com/book/en/v2
    Tons more easily found via Google

    View Slide