Learn You A Git For Great Good!*

Chapter 1

*Apologies to Miran Lipovača

Using this presentation

Detailed discussion/

"section content"

Overview information/

"section headers"

Git is not centralized version control

  • One source of truth
  • All checkins merged in a single location

This is not a picture of git.

Git is distributed version control

  • No single point of truth
  • Flow of changesets between users is defined by the users

The Rest Of This Talk

  1. Using one git repo
  2. Using more than one git repo
  3. Great Good

Using one git repo

Using more than one git repo

Great Good

Make a repo!

# Make a new folder to hold your git repo.

cd /someplace/on/your/computer/
mkdir myGitRepo
cd myGitRepo

# Now make your repo.

git init

# Bask in your brilliance.

Put something in the repo!

# Create a new file in your git repo! Use your imagination!

# No imagination? That's okay. Use this:

echo "# This is my git repo." >> readme.md
echo "Pretty sweet, huh?" >> readme.md

# Now check on the status of your repo:

git status

Using one git repo

Using more than one git repo

Great Good

Finish putting that thing you made in the repo!

Using one git repo

Using more than one git repo

Great Good

# Add your files to the git repository's index with `git add`.
# The index points to the set of changes that you are ready to commit.

# If you didn't have an imagination, run this:

git add readme.md
git status

# Make your first commit!

git commit
git status

Branch your work!

Using one git repo

Using more than one git repo

Great Good

# Whoa! You were on a branch this whole time! Check it out:

git branch

# Make a new branch!

git branch myFirstBranch
git checkout myFirstBranch

# Make a new commit!

touch blarneyStone.txt
git add blarneyStone.txt
git commit

Compare some branches!

Using one git repo

Using more than one git repo

Great Good

# You can compare branches!

git diff master myFirstBranch

# Branches are discrete versions of your repo's content!

git checkout master
git branch mySecondBranch
git checkout mySecondBranch

echo "A string in a file." >> fileWithAString.md
git add fileWithAString.md
git commit

Merge some branches!

Using one git repo

Using more than one git repo

Great Good

# You can also merge branches! Way cool!

git checkout myFirstBranch
git log

git checkout mySecondBranch
git log

git merge myFirstBranch mySecondBranch
ls
git log

Commits

Using one git repo

Using more than one git repo

Great Good

are sets of code changes

Branches

are sequences of commits

Repos

are sets of branches

Using one git repo

Using more than one git repo

Great Good

You should share your code!

Passing code between repositories is easy, safe and there are many online tools that leverage this feature to do amazing things. [1][2]

If you don't, you are boring. Please don't be boring.

Using one git repo

Using more than one git repo

Great Good

This is remotely important

Remotes are how git repositories talk about other git repositories. A git repo will maintain a list of remotes as well as a list of all the branches it has learned about at the locations the remotes point to.

Server, 1 repo please

Using one git repo

Using more than one git repo

Great Good

  1. In your favorite IDE (Visual Studio 2012+) connect to osu-tfs.oregonstate.edu
  2. Map and clone the CASSTrainingCollection's "PracticeGitProject".
  3. Open the Command Prompt from the Team Explorer

TFS server repo

git connected

Using one git repo

Using more than one git repo

Great Good

TFS server repo

Local repo

# Check out your repo's remotes

git remote    # You should only see one remote named "origin"

"origin"

Using one git repo

Using more than one git repo

Great Good

# 1) Make a new branch and add a new quote to the Quotes project!
# 2) Backup your work on your new remote repo!

git push origin your-new-branch

Be pushy!

TFS server repo

Local repo

Go check out your work on the TFS server!

Using one git repo

Using more than one git repo

Great Good

git push origin some-branch

# this says: "Hey, local repo, copy some-branch over to the repo origin points to."

So... what just happened?

Hmm. So does that remote remember the branch you just sent?

# Let's see what our repo knows about remote branches:

git branch -a

# The output of this command reads:
#
# <remote name>/<branch name>

Using one git repo

Using more than one git repo

Great Good

Pull Requests

Pull Requests are how git repos manage the code review process.

In the TFS web portal, find the branch you just pushed and make a pull request against the master branch.

Using one git repo

Using more than one git repo

Great Good

What is a pull request, really?

Pull requests are how Github repos say: "Hey there! I have a branch that could update one of your branches! Please review my branch and then perform a git-pull."

git pull <my branch>

# for more info: http://git-scm.com/docs/git-pull

Using one git repo

Using more than one git repo

Great Good

Pull Request Accepted!

Once your pull requests (or anybody else's) are accepted, you still need to go get the new state of the project.

Using one git repo

Using more than one git repo

Great Good

git latest

To keep tabs on how other git repositories in your project are changing, you will need to get their changes regularly.

# "fetch" updates your repo's records of the remote branches

git fetch <remote name> # all branches on the remote
git fetch <remote name> <branch name> # just the indicated branch

# "pull" is a shortcut[2] that combines fetching and merging.

git pull <remote name> # fetch and merge all branches
git pull <remote name> # fetch and merge the indicated branch

Remotes

are pointers to other repos

Using one git repo

Using more than one git repo

Great Good

Your team's workflow is as rigid or as flexible as you make it.

Using one git repo

Using more than one git repo

Great Good

If you are feeling liberated and/or overwhelmed by the power to design your own source control practices, I have done my job.

The power/flexibility that git gives you is simultaneously its most frustrating foil. Don't worry, there are many many CASS students and staff who will always be here to help you.

Using one git repo

Using more than one git repo

Great Good

Workflows that... work

Understanding that you have complete control over your own workflow and repo structure, the rest of this section is a list of workflows that have been used extensively and are endorsed by the Github community.

Using one git repo

Using more than one git repo

Great Good

Shared Repository [3]

  • Defined at the repository level
  • Emulates centralized version control
  • All users push changes to a "central" repo
  • Code review, merges, conflicts are handled once they arrive on the central repo

Using one git repo

Using more than one git repo

Great Good

Fork-and-Pull [4]

  • Defined at the repository level
  • All repos are members of discrete, unique local-remote pairs
  • Each pair was created by creating a "fork" from an initial repo
  • Pull requests occur between forked repos

Using one git repo

Using more than one git repo

Great Good

git-flow [5]

  • Defined at the branch level and the repo level
  • Models software development phases in branches
  • Has all the benefits and overhead of the enterprise software development process

Using one git repo

Using more than one git repo

Great Good

github-flow [6]

  • Defined at the branch level
  • Simplistic and lightweight
  • Relies heavily on good commit practices
  • Lacks safeguards/redundancy of git-flow

Next Episode

Git Graph Theory

Addendum

Off the top of my head: Github, Bitbucket, Bettercodes, Heroku, Travis CI, Phonegap Build ...

[1]

[2]

A note about shortcuts and 3rd party tools:

While useful, these tools tend to assume that you are using your git repos with a certain workflow. If this assumption is false you may accidentally do very bad things* to your work.

* utterly and irreversibly destroy

Addendum

[3]

[4]

[5]

[6]

Learn You A Git

By Justin C

Learn You A Git

For Great Good

  • 932