**NOTE: This slide deck contains many animations. You should download and view in PowerPoint if you can! ***
Git (and GitHub) have become pillars in how we manage our code workflow, particularly when we work with large teams. Yet developers tend to be afraid of Git, and only handle but the most basic of operations. This presentation will reveal the magic of Git, and help you understand how and why operations are carried out.
After this session, you'll never be afraid of a merge conflict again.
Topics covered: * Git under the hood * Git basics * Merging vs Rebasing * Conflict resolution * Triangular Workflow * Common error scenarios and how to solve them * Tips and Tricks
3. Setting up
Apple GIT gotchas:
Xcode ships with its custom version of git. You don’t want this:
$ git --version
git version 2.3.2 (Apple Git-55)
$ which git
/usr/bin/git
4. Setting Up
The right git:
Download and install:
http://git-scm.com/download/mac
Set up:
Modify ~/.profile
export PATH=/usr/local/bin:$PATH
$ git --version
git version 2.4.3
5. Setting Up
Make life easy for yourself: Autocomplete!
Download: https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
Save to -> ~/.git-completion.bash (Note the dot!)
Add to ~/.profile
# Run git autocomplete
source ~/.git-completion.bash
Verify ->
git check<tab> --autocompletes to--> git checkout
PRO TIP: Also autocompletes branch names!
6. Setting Up
Make life easy for yourself, PART 2: Change your terminal prompt!
Download: https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
Save to -> ~/.git-prompt.sh (Note the dot!)
Add to ~/.profile
# Run git prompt enhancements
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWCOLORHINTS=true
export PROMPT_COMMAND='__git_ps1 "w" "$ "'
7. GIT Init
GIT IS MISUNDERSTOOD.
IT JUST WANTS TO BE YOUR
FRIEND
9. Git tracks everything. Forever!
Files (Blobs) are tracked based on content, not on their name.
Git uses the SHA1 algorithm to create a unique hash (fingerprint) for each file.
foo.txt -> “Hello” -> e965047ad7c57865823c7d992b1d046ea66edf78
10. How is it calculated?
“blob”+space
Do it yourself! printf ”blob 6000Hellon" | shasum
Null char
Length of content File content
b l o b 6 0 0 0 H e l l o n
11. All versions of a file end up as blobs.
E9650
Hello
980a0
Hello World!
53627
Hello World! How are you?
7a27f
Hello World! How are you today?
557db
Hello World
12. Trees organize (and name) sets of files
E9650
Hello
a69b2
557db
Hello World
8984f
13. Commit ec445 Commit bd297
Commits give order to trees
E9650
Hello
Tree: a69b2
Author: lmarkus
Message: First Commit
557db
Hello World
bd297ec445
Tree: 8984f
Parent: ec445
Author: lmarkus
Message: Second Commit
14.
15. Git Recap
Git saves *everything*
Git keeps track of what your project looked like at a given time (tree + commit)
Git keeps a history of the various snapshots as a linked list. (commit -> parent)
20. The world is my stage!
HEAD STAGE Work Dir
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: miner.txt
no changes added to commit (use "git add" and/or "git commit -a")
21. The world is my stage!
HEAD STAGE Work Dir
$ git add miner.txt
23. The world is my stage!
HEAD STAGE Work Dir
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: miner.txt
26. The world is my stage!
HEAD STAGE Work Dir
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: miner.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
sun.txt
27. The world is my stage!
HEAD STAGE Work Dir
$ git commit -m “Mining is hard”
35. Git Rebase - Part I
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
36. Git Rebase - Part I
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
$ git log --oneline
845c2a5 Actually, that wasn't a bug. Reverting
10ae9b3 Oops, fixed a bug
5c68b04 Fixed Another typo
83775b2 Fixed Typo
099d98e Created New Function
37. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
$ git rebase -i HEAD~4
pick 83775b2 Fixed Typo
pick 5c68b04 Fixed Another typo
pick 10ae9b3 Oops, fixed a bug
pick 845c2a5 Actually, that wasn't a bug. Reverting
# Rebase 099d98e..845c2a5 onto 099d98e (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
# Note that empty commits are commented out
38. A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
pick 83775b2 Fixed Typo
pick 5c68b04 Fixed Another typo
pick 10ae9b3 Oops, fixed a bug
pick 845c2a5 Actually, that wasn't a bug. Reverting
squash
Git rebase --interactive
39. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
pick 83775b2 Fixed Typo
squash 5c68b04 Fixed Another typo
40. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
pick 83775b2 Fixed Typo
squash 5c68b04 Fixed Another typo
41. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
# This is a combination of 2 commits.
# The first commit's message is:
Fixed Typo
# This is the 2nd commit message:
Fixed Another typo
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
42. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
# This is a combination of 2 commits.
# The first commit's message is:
Documentation updates
# This is the 2nd commit message:
# Fixed Another typo
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
43. Git rebase --interactive
A B C D E
Created
New Function
Fixed typo Fixed
Another typo
Oops!
Fixed a bug.
Actually,
that wasn’t a bug.
Reverting.
$ git log --oneline
779dbd5 Documentation updates
099d98e Created New Function
45. Branches
A B C
MASTER
Remember: Your git
history is just a linked list
of commits.
A branch simply
represents a marker
somewhere in this
history. It points to the
latest commit within a
set.
51. Merge
A B C
MASTER
D E
DEVELOP
F M Merge commits have TWO
parents. They represent the
point where to branches
became one
git merge develop
X Y
Work can continue after a merge
52. Rebase
A B C
D E
DEVELOP
MASTER
F
We just re-wrote history!
git rebase develop
53. Rebase Vs Merge - RECAP
A B C
MASTER
D E
DEVELOP
F M
X Y
A B C
MASTER
D* E* F
Merge
Very explicit, accurate history
Visually complex
Easy to roll back
Rebase
Linear history
Easy to understand
Not historically accurate
Hard to undo.
58. Forking and Cloning - Common Error #1
UPSTREAM
ORIGIN
LOCAL
git push origin develop
DEVELOP BRANCH
git remote update
git rebase upstream/develop
git push origin develop
59. Forking and Cloning - Common Error #1
$ git push origin develop
To https://github.paypal.com/lmarkus/activation.git
! [rejected] develop -> develop (non-fast-forward)
error: failed to push some refs to
https://github.paypal.com/lmarkus/activation.git
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
60. Forking and Cloning - Common Error #1
UPSTREAM
ORIGIN
LOCAL
DEVELOP BRANCH
Option 1:
git pull origin develop
git push origin master
Solves the problem, but history
gets messy. Doubled up commits
61. Forking and Cloning - Common Error #1
UPSTREAM
ORIGIN
LOCAL
DEVELOP BRANCH
Option 2:
git push origin master --force
The Force has a dark side…
You are overwriting history which
may be shared
63. What happens during a merge
Animals.txt
Animals I like:
Dogs!
Animals.txt
Animals I like:
Cats!
Birds!
master feature1
git merge feature1
Animals.txt
Animals I like:
<<<<<<< HEAD
Dogs!
=======
Cats!
Birds!
>>>>>>> feature1
64. What happens during a merge
Animals.txt
Animals I like:
Dogs!
Animals.txt
Animals I like:
Cats!
Birds!
master feature1
Animals.txt
Animals I like:
<<<<<<< HEAD
Dogs!
=======
Cats!
Birds!
>>>>>>> feature1
git add Animals.txt
git commit -m “Dogs Rule”