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!
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.
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:
- Create a new repo and push it up to Github.
- Clone your neighbor's new github repo.
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:
- Add new file & make a change to existing file
- Commit changes
- Push changes up to the repo
- Pull down your partner's changes from their repo
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:
- Make your partner a collaborator on your repo (https://github.com/niftynei/bloggit/settings/collaboration)
- Make a branch in your partner's repo
- Make some changes & push those up to their Github repo (in the new branch)
- Pull down your partner's new branch & check it out!
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:
- Merge your partner's branch into master.
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:
- In your partner's repo, create a new branch off of master #
git checkout master && git checkout -b second_feature
- Make 2 new changes & commits in this branch.
- Push new branch up to your partner's repo
- 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 - Rebase master
- Fix any conflicts
- Merge
second_feature
branch into master
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