What this talk will cover Git Basics - Getting started - Basic workflow and commands GitHub - Getting started - Anatomy of a project - How to contribute
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.
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
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/
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
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"
Getting Started on GitHub ● Create an Account ● Add your public key (strongly recommended) ○ makes pushing changes easier Get started @ https://github.com/
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
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
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
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
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:
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
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
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
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