Skip to content

Instantly share code, notes, and snippets.

@niftynei
Last active July 25, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftynei/70d51eff7d91aad9678eb63c8f6ad95f to your computer and use it in GitHub Desktop.
Save niftynei/70d51eff7d91aad9678eb63c8f6ad95f to your computer and use it in GitHub Desktop.
Git 101

Git 101

Hi, welcome to Git 101. Here's what we're going to learn today.

  • Making a new repo (and remotes)
  • Committing new changes
  • Branching out
  • Resolving conflicts
  • Merging vs Rebasing

This workshop will run about an hour, so we have about 10 minutes for each of these topics. I'll spend 5 minutes going over a topic, and then you'll spend 5 minutes solving a challenge with your partner.

You'll need two things for this workshop: a Github account & a partner. (If someone doesn't have a github account, have them pair with a partner first)

First task -- find a partner to pair with!

Second task -- open up Github and make a new repo (give it a different name than your partner does)

*** DON'T ADD A README!

Everyone paired up? Great.

Let's get started!

What is Git?

Very simply, git is a light-weight record of changes that you've made to a file or set of files in a directory.

Making a git commit is like creating a savepoint in a video game. You can look at your history and go back or forward to a change point at any time.

Getting Started: initializing a repository

git init  # Make a new repo
touch newfile.txt # Create a new file in your git repo
# Create a new repo on Github.com
git add remote ...` && `git push origin HEAD # Push your repo up to Github.com

TASKS:

  1. Create a new repo and push it up to Github.
  2. Clone your neighbor's new github repo.

Making some changes

Right now we have an empty file and we're associated to a remote repo. Awesome.

Let's make some changes and push (and pull) them down to our respective repos.

vim newfile.txt
** Add some text to this file **
touch secondfile.txt
git add -p  # Explain interactive mode
git status # Note that new file is missing from commit. Explain why.
git commit  # add message manually, explain what you're doing and why.
git log # Show commit
git add .  # Add remaining stuff
git status
git commit -m 'Add new file!' # add message manually
git log
git push origin master

TASKS:

  1. Add new file & make a change to existing file
  2. Commit changes
  3. Push changes up to the repo
  4. Pull down your partner's changes from their repo

Branching Out

Let's say that you want to add a new feature. You're going to need to make a branch.

git checkout -b new_feature #this makes a branch
vim newfile.txt # add a new 'feature' to this file
git add -p # add the the new things.
git commit -m 'This is a new feature'
git lol  #Show off the fancy new stuff
git push -u origin HEAD  # explain HEAD && -u aka `git push -u origin new_feature`

git fetch origin  # this pulls the things down
git checkout --track origin/new_feature # Checkout the correct branch!

** Note that if you're not a collaborator on a project, your workflow will be a bit different for submitting patches (but not too much)

TASKS:

  1. Make your partner a collaborator on your repo (https://github.com/niftynei/bloggit/settings/collaboration)
  2. Make a branch in your partner's repo
  3. Make some changes & push those up to their Github repo (in the new branch)
  4. Pull down your partner's new branch & check it out!

Merge those Changes!

Let's merge your partner's new feature into your master branch!

git checkout new_feature
git merge master  # be my guinea pigs... all of you
git checkout master
git merge new_feature

### Are there any merge conflicts?  Let's assume yes.

git status # let's see what file the problem is in.
vim <<problem_filename>>
// search for >>> and <<< explain what it means. resolve problem in file
git status
git commit
git log  # show the merge history

TASKS:

  1. Merge your partner's branch into master.

Merging vs Rebasing

Ok, now we're going to have a little fun with Rebasing. This isn't the best example, as usually rebases become very complex when you have a lot of commits that you're rebasing. But we should be able to have some fun with our existing project.

git checkout master
git checkout -b second_feature
// Make a few changes & commits
git lol # show history
// Make a second commit in master, so that branch is now behind
git checkout second_feature
git rebase master
// If there's a conflict...
// fix conflict
git add .
git rebase --continue
// when finished, show history. compare this to merge history
git lol
// finish merge into master
git checkout master
git merge second_feature  # should be seamless

TASKS:

  1. In your partner's repo, create a new branch off of master # git checkout master && git checkout -b second_feature
  2. Make 2 new changes & commits in this branch.
  3. Push new branch up to your partner's repo
  4. In your repo, pull down the new branch and switch to it. # It should be behind master since it doesn't have the new_feature branch included
  5. Rebase master
  6. Fix any conflicts
  7. Merge second_feature branch into master

Final Notes:

git lol is an alias that I love. You can add it to your config by running the following command:

git config --global --add alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"  # source: http://uberblo.gs/2010/12/git-lol-the-other-git-log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment