Git Real Slides
Git Real Slides
LEVEL 1
! s e u s s I n o i t a r o b a l l Co
LEVEL 1 GIT BASICS
REPOSITORY LOCATIONS
Central Repository
Distributed Repository
Git is a
ORIGINS
Linux Kernel project.
Meant to be distributed, fast and more natural. Capable of handling large projects.
start here
LEVEL 1 GIT BASICS
GIT HELP
$ git help usage: git [--version] [--exec-path[=<path>]] [--html-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [-c name=value] [--help] <command> [<args>] The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug ...
GIT HELP
$ git help config GIT-CONFIG(1) Git Manual GIT-CONFIG(1)
SETTING UP GIT
$ git config --global user.name "Gregg Pollack"
Who gets credit for changes What email you use Pretty command line colors
$ git config --global user.email [email protected] $ git config --global color.ui true
STARTING A REPO
$ mkdir store $ cd store $ git init Initialized empty Git repository in /Users/gregg/store/.git/
Starts as untracked
Add "le to staging area
# On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Our newly created file # README.txt nothing added to commit but untracked files present (use "git add" to track)
Commit changes
timeline
$ git commit -m "Create a README." [master abe28da] Create a README. 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README.txt
master
$ git status # On branch master nothing to commit (working directory clean)
master
master
OR
$ git add --all $ git status # On branch master # Changes to be committed: # # new file: LICENSE # modified: README.txt #
LEVEL 1 GIT BASICS
Commit changes
$ git commit -m "Add LICENSE and finish README." [master 1b0019c] Add LICENSE and finish README. 2 files changed, 21 insertions(+), 0 deletions(-) create mode 100644 LICENSE
master
master
Add the list of files Add all files Add all txt files in current directory Add all txt files in docs directory Add all files in docs directory Add all txt files in the whole project
GIT DIFF
LICENSE
Copyright (c) 2012 Envy Labs LLC ... Permission is hereby granted, free of charge, to any person obtaining a copy
LICENSE
edit
Copyright (c) 2012 Code School LLC ... Permission is hereby granted, free of charge, to any person obtaining a copy
Show unstaged $ git diff diff --git a/LICENSE b/LICENSE index 7e4922d..442669e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 Envy Labs LLC +Copyright (c) 2012 Code School LLC
diff --git a/LICENSE b/LICENSE index 7e4922d..442669e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 Envy Labs LLC +Copyright (c) 2012 Code School LLC
UNSTAGING FILES
$ # # # # # # $
Unstage Tip git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
modified: LICENSE
master
HEAD
DISCARD CHANGES
$ git status
# On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: LICENSE #
Readme.txt
master
HEAD
edit
UNDOING A COMMIT
Whoops, we forgot something on that commit.
master
HEAD
ADDING TO A COMMIT
Maybe we forgot to add a !le
master
HEAD
$ git commit --amend -m "Modify readme & add todo.txt." [master fe98ef9] Modify readme and add todo.txt. 2 files changed, 2 insertions(+), 1 deletions(-) create mode 100644 todo.txt
USEFUL COMMANDS
$ git reset --soft HEAD^
Undo last commit, put changes into staging Change the last commit
Undo last commit and all changes Undo last 2 commits and all changes
HOW TO SHARE?
Remote Repository
push
master
pull
pull
GitHub BitBucket
push
master
LEVEL 2 STAGING & REMOTES
ADDING A REMOTE
$ git remote add origin https://github.com/Gregg/git-real.git
New remote
address
origin
PU SHING TO R EMOTE
remote repository name
$ git push -u origin master Username for 'https://github.com': Gregg Password for 'https://[email protected]': Counting objects: 11, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (11/11), 1.50 KiB, done. Total 11 (delta 0), reused 0 (delta 0) To https://github.com/Gregg/git-real.git * [new branch] master -> master
push
master
origin
Password caching
https://help.github.com/articles/set-up-git
pull
origin
good
n e t f o s i h t o to d
origin
production
test
LEVEL 2 STAGING & REMOTES
To remove remotes
$ git remote rm <name>
To push to remotes
$ git push -u <name> <branch>
usually master
LEVEL 2 STAGING & REMOTES
HEROKU REMOTE
$ heroku create Creating dev-server-1426... done, stack is cedar http://dev-server-1426.herokuapp.com/ | [email protected]: dev-server-1426.git Git remote heroku added $ git remote -v heroku [email protected]: dev-server-1426.git (fetch) heroku [email protected]: dev-server-1426.git (push) origin https://github.com/Gregg/git-real.git (fetch) origin https://github.com/Gregg/git-real.git (push)
To push to heroku
$ git push heroku master
LEVEL 2 STAGING & REMOTES
triggers deploy
USEFUL COMMANDS
$ git reset --soft HEAD^
you push
COLLABORATING
I want a copy Clone the repo!
g g e Gr
push
local repo
Jane
github
?
CLONING A REPOSITORY
$ git clone https://github.com/codeschool/git-real.git Cloning into 'git-real'...
GIT CLONE
1 - Downloads the entire repository into a new git-real directory. 2 - Adds the origin remote, pointing it to the clone URL.
master
HEAD
$ git remote -v
origin origin https://github.com/codeschool/git-real.git (fetch) https://github.com/codeschool/git-real.git (push)
BRANCHING OUT
Need to work on a feature that will take some time? Time to branch out. $ git branch cat
Jane
cat HEAD
master
SWITCHING TO A BRANCH
Time to jump on that new 'cat' branch. $ git checkout cat Switched to branch 'cat'
Jane
cat HEAD
WORKING ON A BRANCH
$ echo "Schrdinger" > cat.txt $ git add cat.txt $ git commit -m "Create quantum cat." [cat ab48a3f] Create quantum cat. 1 file changed, 1 insertion(+) create mode 100644 cat.txt
cat HEAD
WORKING ON A BRANCH
$ ls README.txt cat.txt
cat HEAD
master
LEVEL 3 CLONING & BRANCHING
BACK TO MASTER
$ git checkout master Switched to branch 'master' $ ls README.txt
cat
$ git log commit 1191ceb7252c9d4b1e05c9969a55766a8adfce3b Author: Gregg <[email protected]> Date: Wed Jun 27 23:11:20 2012 -0700 Add README.
master
BACK TO CAT
$ git checkout cat Switched to branch 'cat' $ ls README.txt cat.txt
cat
HEAD
master
LEVEL 3 CLONING & BRANCHING
TIME TO MERGE
Done with that feature branch? Time to merge it into 'master '. $ git checkout master Switched to branch 'master' $ ls README.txt
master cat
HEAD
no cat, as expected
$ git merge cat Updating 1191ceb..ab48a3f Fast-forward cat.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 cat.txt
whats that?
nothing new
something new
BRANCH CLEAN UP
When youre done with a branch, you can safely remove it. $ git branch -d cat Deleted branch cat (was 957dbff).
NON-FAST-FORWARD
Lets work on a new admin feature. $ git checkout -b admin Switched to a new branch 'admin' ... $ git add admin/dashboard.html $ git commit -m 'Add dashboard' ... $ git add admin/users.html $ git commit -m 'Add user admin'
h c n a r b t u o s k c e h c d n a s e creat
master admin HEAD
AND SUDDENLY...
Git uses Vi if no default editor is set to edit commit messages.
1 2 3 4 5 6 7 Merge branch 'admin' # # # # #
DON
T P
Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch. Lines starting with '#' will be ignored, and an empty message aborts the commit.
ANI
:wq
Vi commands
j h down left k l up right ESC i leave mode insert mode :wq :q! save & quit cancel & quit
RECURSIVE MERGING
Git cant fast-forward since changes were made in both branches.
Merge made by the 'recursive' strategy. 0 files changed create mode 100644 admin/dashboard.html create mode 100644 admin/users.html
master
merge commit
admin
2 commits
2 commits
COLLABORATION BASICS
LEVEL 4
github
I want a co py
?
push
Gregg
Clone the repo
Jane
COMMIT FLOW
jane $ git push Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 441 bytes, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/codeschool/git-real.git 4e67ded..30ce481 master -> master
github
gregg
different same
github
? w o n s n e p p a h t a Wh
To https://github.com/codeschool/git-real.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/codeschool/git-real.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull
...
$ git push
...
Success!
UNDERSTANDING PULL
$ git pull 1. Fetch (or Sync) our local repository with the remote one gregg github $ git fetch
origin
UNDERSTANDING PULL
$ git pull 1. Fetch (or Sync) our local repository with the remote one gregg github $ git fetch
master
origin/master
master
MERGE COMMIT
$ git pull 1. Fetch (or Sync) our local repository with the remote one 2. Merges the origin/master with master $ git merge origin/master
my editor
Merge branch 'master' of https://github.com/codeschool/git-real Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch. Lines starting with '#' will be ignored, and an empty message aborts the commit.
merge commit
MERGE COMMIT
$ git pull 1. Fetch (or Sync) our local repository with the remote one 2. Merges the origin/master with master $ git fetch $ git merge origin/master
master
merge commit
origin/master
PU SHING COMMITS
$ git push Update origin/master be at the same state as our local repo
origin/master master
merge commit
github
OUR LOG
gregg $ git log commit ee47baaedcd54e1957f86bda1aaa1b8a136185da Merge: 87c5243 57501d5 Author: Gregg Pollack <[email protected]>
Merge branch 'master' of https://github.com/Gregg/git-real commit 87c5243d2266f05cd9fda8b1c9137f11b3fe6f31 Author: Gregg Pollack <[email protected]> Update the readme. commit 57501d595b16e2d1198a9c04c547a5b1380a6618 Author: Gregg Pollack <[email protected]> Add store and product models.
MERGE CONFLICTS
README
here is my readme the cake is a lie
README
Gregg
committed
github
here is my readme
Jane
gregg
different same
MERGE CONFLICT
gregg $ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1) Unpacking objects: 100% (3/3), done. From https://github.com/Gregg/git-real ee47baa..4e76d35 master -> origin/master Auto-merging README.txt CONFLICT (content): Merge conflict in README.txt Automatic merge failed; fix conflicts and then commit the result.
MERGE CONFLICT
gregg $ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit each, respectively. # # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: README.txt # no changes added to commit (use "git add" and/or "git commit -a")
MERGE CONFLICT
README
here is my readme <<<<<<< HEAD the cake is a lie. ======= the cake is telling the truth! >>>>>>> 4e76d3542a7eee02ec516a47600002a90a4e4b48
gregg $ git commit -a Merge commit
LEVEL 4 COLLABORATION BASICS
cake is a lie.
COMMIT EDITOR
gregg $ git commit -a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Merge commit
Editor
Merge branch 'master' of https://github.com/Gregg/git-real Conflicts: ! README.txt # # It looks like you may be committing a merge. # If this is not correct, please remove the file #!.git/MERGE_HEAD # and try again.
# # # # #
Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. On branch master Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively.
MERGE COMMIT
master
Merge commit
origin/master master
Merge commit
origin/master
$ git push
COLLABORATION BASICS
LEVEL 4
Gregg
t u o t i k c e h c l l I , t Swee
remote branch
! h s u p d n a e t u b i r t n o c n a c e w Now
REMOTE SHOW
$ git remote show origin * remote origin Fetch URL: https://github.com/Gregg/git-real.git Push URL: https://github.com/Gregg/git-real.git HEAD branch: master Remote branches: master tracked shopping_cart tracked Local branches configured for 'git pull': master merges with remote master shopping_cart merges with remote shopping_cart Local refs configured for 'git push': master pushes to master (up to date) shopping_cart pushes to shopping_cart (local out of date)
REMOVING A BRANCH
$ git push origin :shopping_cart To https://github.com/codeschool/git-real.git - [deleted] shopping_cart $ git branch -d shopping_cart
error: The branch 'shopping_cart' is not fully merged. If you are sure you want to delete it, run 'git branch -D shopping_cart'.
Jane
Gregg
Gregg
t a local branch now)
nc h
heroku-staging
local:remote
$ git push heroku-staging staging:master
TAGGING
A tag is a reference to a commit (used mostly for release versioning)
$ git tag v0.0.1 v0.0.2
REBASE BELONG TO US
LEVEL 6
merge commit
s s e l e s u l e e f s t i m m o c e g r e
gregg
different same
github
t i m m o c s Jane
To https://github.com/codeschool/git-real.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/codeschool/git-real.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull
...
$ git push
...
! e p o N
FETCH
gregg $ git fetch remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 4 (delta 0) Unpacking objects: 100% (4/4), done. From https://github.com/codeschool/git-real f35f2f1..71a4650 master -> origin/master
gregg
github
master
origin/master
REBASE
gregg $ git rebase
1. Move all changes to master which are not in origin/master to a temporary area.
temp
master
origin/master
REBASE
gregg $ git rebase
1. Move all changes to master which are not in origin/master to a temporary area. 2. Run all origin/master commits.
temp
master
origin/master
REBASE
gregg $ git rebase
1. Move all changes to master which are not in origin/master to a temporary area. 2. Run all origin/master commits. 3. Run all commits in the temporary area, one at a time.
master
Update the Readme.
No M
erge
Comm it!
master admin
master admin
Rebase
master admin
github
d e t i d E
! e l i f e m sa
same
FETCH
gregg $ git fetch
master
Add shopping cart.
origin/master
Add truth to readme.
REBASE
gregg $ git rebase
1. Move all changes to master which are not in origin/master to a temporary area
temp
origin/master
Add truth to readme.
master
REBASE
gregg $ git rebase
temp
Add shopping cart.
master
Add truth to readme.
REBASE CONFLICT
gregg $ git rebase
First, rewinding head to replay your work on top of it... Applying: Add lie to readme. Using index info to reconstruct a base tree... M README.txt <stdin>:13: trailing whitespace. the cake is a lie, and I am your father! warning: 1 line adds whitespace errors. Falling back to patching base and 3-way merge... Auto-merging README.txt CONFLICT (content): Merge conflict in README.txt Failed to merge in the changes. Patch failed at 0001 Add lie to readme.
master CONFLICT
When you have resolved this problem run "git rebase --continue". If you would prefer to skip this patch, instead run "git rebase --skip". To check out the original branch and stop rebasing run "git rebase --abort".
REBASE CONFLICT
gregg $ git status
# Not currently on any branch. # Unmerged paths: # (use "git reset HEAD <file>..." to unstage) # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: README.txt # no changes added to commit (use "git add" and/or "git commit -a")
master
gregg $
REBASED LOG
master
Add shopping cart. Add lie to readme.
REBASE BELONG TO US
LEVEL 6
hash
LOG FORMAT
$ git log --pretty=format:"%h %ad- %s [%an]" placeholder %ad %an %h %s %d replaced with author date author name SHA hash subject ref names
PATCH
$ git log --oneline -p 3ea7f70 I'm telling you, it's 'Octopi'. diff --git a/index.html b/index.html index 021a54e..640d66d 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,7 @@ <nav> <ul> <li><a href="cat.html">Cats</a></li> <li><a href="octopus.html">Octopuses</a></li> + <li><a href="octopi.html">Octopi</a></li>
LEVEL 7 HISTORY & CONFIGURATION
STATS
$ git log --oneline --stat 3ea7f70 I'm telling you, it's 'Octopi'. index.html | 2 +1 file changed, 1 insertion(+), 1 deletion(-) 96776a4 Add index. index.html | 30 +++++++++++++++--------------1 file changed, 15 insertions(+), 15 deletions(-)
GRAPH
$ git log --oneline --graph * 30b1f8f Merge branch 'bird' into master |\ | * 8b8f950 Revise silly hipster name for bird aisle. * | 915f242 Add emphasis. |/ * 69728cd Update index descriptions.
DATE RANGES
$ git log --until=1.minute.ago $ git log --since=1.day.ago $ git log --since=1.hour.ago $ git log --since=1.month.ago --until=2.weeks.ago $ git log --since=2000-01-01 --until=2012-12-21
until since (days) since (hours) since & until (relative) since & until (absolute)
DIFFS
$ git diff diff --git a/index.html b/index.html @@ -8,7 +8,10 @@ <nav> <ul> <li><a href="cat.html">Cats</a></li> <li><a href="octopus.html">Octopuses</a></li> + <li><a href="birds.html">Birds</a></li> + <li><a href="hamsters.html">Hamsters</a></li> </ul> </nav> </body>
removed line
added lines
UNCOMMITTED CHANGES
$ git diff HEAD diff --git a/index.html b/index.html index 021a54e..1ceb9d6 100644 diff between last commit @@ -8,7 +8,10 @@ & current state <ul> <li><a href="cat.html">Cats</a></li> ... diff --git a/octopus.html b/octopus.html index 55806be..ce8a2c7 100644 @@ -2,6 +2,6 @@ <html lang="en"> ...
EARLIER COMMITS
$ git diff HEAD^ $ git diff HEAD^^ $ git diff HEAD~5 $ git diff HEAD^..HEAD
parent of latest commit grandparent of latest commit five commits ago second most recent commit vs. most recent
EARLIER COMMITS
$ git diff f5a6sdfsfsdfsdfff9..4sdsdfsdfsdfsdffb063f $ git log --oneline
257256c cat 4fb063f Add index f5a6ff9 Add catalog pages
range of SHAs
BLAME
$ git blame index.html --date short ... 96776a42 (Gregg 2012-06-29 9) <ul> 96776a42 (Gregg 2012-06-29 10) <li>Cats</li> 3ea7f709 (Jane 2012-06-30 11) <li>Octopi</li> 96776a42 (Gregg 2012-06-29 12) </ul> ...
date
line # content
EXCLUDING FILES
$ git status # Untracked files: # (use "git add <file>..." to include in what will be committed) # # experiments/ we dont want to commit this...
.git/info/exclude
experiments/
EXCLUDE PATTERNS
tutorial.mp4 *.mp4 experiments/ logs/*.log
exclude this file exclude all .mp4 files exclude directory exclude .log files in logs directory
.gitignore
logs/*.log
$ git status no more logs # On branch master nothing to commit (working directory clean)
REMOVING FILES
$ git rm README.txt $ git status # Changes to be committed: # # deleted: README.txt
UNTRACKING FILES
$ git rm --cached development.log $ git status # Changes to be committed: # # deleted: development.log
not deleted from the local file system, only from Git
UNTRACKING FILES
.gitignore
logs/*.log
$ git add .gitignore $ git commit -m "Ignore all log files." [master bccdc8c] Ignore all log files. 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .gitignore delete mode 100644 development.log
CONFIG
$ git config --global user.name "Gregg Pollack" $ git config --global user.email "[email protected]"
LOCAL CONFIG
$ git config user.email "[email protected]" sets email for current repo $ git config --list user.name=Gregg [email protected] color.ui=true core.editor=mate -w [email protected] $ git config user.email [email protected]
ALIASES
$ git config --global alias.mylog \ "log --pretty=format:'%h %s [%an]' --graph"
$ git config --global alias.lol \ "log --graph --decorate --pretty=oneline --abbrev-commit --all" $ git mylog * 19f735c |\ | * 7980856 * | 5c9ed90 |/ * ab48a3f
Merge branch 'admin' [Jane] Add user admin [Jane] Add dashboard. [Jane] Create quantum cat. [Jane]
ALIASES
$ git config --global alias.st status
$ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit