Git @ Intent Media

Agenda

Git Basics

What is Git?

Git Basics

Checking out the code

$ git clone git@github.com:intentmedia/code.git
Initialized empty Git repository in /home/pj/Code/IM/data/.git/
remote: Counting objects: 738, done.
remote: Compressing objects: 100% (396/396), done.
remote: Total 738 (delta 336), reused 680 (delta 279)
Receiving objects: 100% (738/738), 177.44 KiB, done.
Resolving deltas: 100% (336/336), done.
$

Git Basics

Checking out the code – Intellij

Git Basics

Checking in your changes

$ echo 'Adding this to the README' >> README
$ git add README
$ git commit --message 'This is an example of a commit'
$ git push
Counting objects: 5, done.
Delta compression using up to 6 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 313 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:pgroudas/git_preso.git
   23cc04d..e076226  gh-pages -> gh-pages

Git Basics

Checking in your changes – Intellij ⌘+k

Git Basics

Checking in your changes – Intellij ⌘+k

Git Basics

Pushing your changes – Intellij

Git Basics

Pulling the latest upstream changes

Working tree must be clean!

$ git pull --rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to db46daeff68ab4d58283cc5f504baceb7193a4fa.

Git Basics

Pulling the latest upstream changes – Intelij ⌘+t

Handles your unclean working tree for you!

Git Basics

Rebasing1

Before After

1 http://book.git-scm.com/4_rebasing.html

Git Basics

Merge2

Before After

2 http://book.git-scm.com/3_basic_branching_and_merging.html

Everyday Git with Intellij

Update code frequently with ⌘+t and select Rebase update type

Commit with ⌘+k and you generally want to push at the same time

What to do if something goes wrong?

Most common problems occur while pulling down upstream changes

What to do if something goes wrong?

Just like any version control system, if multiple users are editing the same files, occasionally you have to resolve conflicts. Intellij is actually pretty sane about dealing with conflicts and prompts you to resolve them when you update.

What to do if something goes wrong?

You can always revert to the state prior to the rebase operation if you mess up the merge.

You can also use Intellij’s merge tool to solve problems you get yourself into on the command line

pj@onslaught:~/Code/IM/code$ git pull --rebase
First, rewinding head to replay your work on top of it...
Applying: PJ:  This is a test commit
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging adServer/config/production.properties
CONFLICT (content): Merge conflict in adServer/config/production.properties
Failed to merge in the changes.
Patch failed at 0001 PJ:  This is a test commit

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

Merge tool is accessible via Version Control > Git > Merge Tool

Agenda

Advanced Usages

“With great power comes great responsibility.” -Stan Lee

Advanced Usages

Seriously, you should probably skim the git community book if you want to use the more advanced features.

Advanced Usages

Feature branches – Idea

  1. Branch when you start a feature
  2. Check in locally as you develop it.
  3. When finished, merge back into master and push upstream

Advanced Usages

Feature branches – Benefits

  1. master branch remains pristine locally.
  2. Can checkin frequently even if just a wip without breaking the build.
  3. Can work on multiple features in separate branches independently.
  4. All check-ins for a given feature are pushed at once (easier to review)

Advanced Usages

Feature branches – Downsides

  1. More complex (but still possible) to maintain linear history in master
  2. Promotes longer development ‘in a vacuum’

Advanced Usages

Feature branches – Commands

create a branch
git branch <branchname>
switch into branch
git checkout <branchname>
create and switch into branch
git checkout -b <branchname>
pull in new changes from master
git rebase master <branchname>
merge changes back to master
git checkout master; git merge --ff-only <branchname>

Advanced Usages

Interactive Rebase – Idea

You may occasionally accumulate a small number of commits locally before you share them. Interactive rebasing allows you the oppurtunity to clean up the commits in a number of ways before you push them up stream.

Advanced Usages

Interactive Rebase – Benefits

  1. Squash multiple commits into one
  2. Rewrite your commit messages
  3. Group the changed files differently

Advanced Usages

Interactive Rebase – Example

Advanced Usages

Interactive Rebase – Example

If you ever need help