Share
Using git to make life easier and code better
Good version control is essential for keeping code manageable. If you use git well, you (and your co-contributors) can very easily go back in time and know exactly what was changed 6 months ago, by whom, and why. Integrating changes into a fast-moving codebase where files are being updated by very large numbers of people in real time becomes a breeze and production bugs can be fixed in minutes with a simple `git revert`.
We will cover the most commonly used git commands, explain some of the core functionality of git to help you most effectively understand and use it as a tool, and finally move on to some advanced tricks and tips to help you get the most out of your git workflow. Suitable for all levels but some Bash knowledge is assumed and some experience with git may be helpful.
Nathan Dunn
Transcript
266 slides
-
git(1) NAME git - the stupid content tracker DESCRIPTION Git
is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. -
How does git make life easier? ✪ Store and Record
changes to files ✪ Compare versions ✪ Play nice with others ✪ Restore previous work ✪ Awesome stuff -
How does git make code better? ✪ Store and Record
changes to files ✭ History of everything changed and why ✪ Manage and compare versions ✭ Context switch. What changed? ✪ Play nice with others ✭ Learn. Collaborate. Share. Review. ✪ Restore previous work ✭ Go back in time. Undo mistakes. ✪ Awesome stuff -
How does git make code better? ✪ Store and Record
changes to files ✭ History of everything changed and why ✪ Manage and compare versions ✭ Context switch. What changed? ✪ Play nice with others ✭ Learn. Collaborate. Share. Review. ✪ Restore previous work ✭ Go back in time. Undo mistakes. ✪ Awesome stuff -
How does git make code better? ✪ Store and Record
changes to files ✭ History of everything changed and why ✪ Manage and compare versions ✭ Context switch. What changed? ✪ Play nice with others ✭ Learn. Collaborate. Share. Review. ✪ Restore previous work ✭ Go back in time. Undo mistakes. ✪ Awesome stuff -
So git is super cool. yep. got that. How do
we get started? -
Store and Record changes to files ✪ History of everything
changed, ever ✭ init ✭ clone ✭ submodule ✭ status ✭ commit ✭ add -
git init NAME git-init - Create an empty Git repository
or reinitialize an existing one SYNOPSIS git init [--bare] SUMMARY repo/.git -> work locally repo.git (bare) -> put on a server -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al total 4 drwxrwxr-x 7 xxx xxx 4096 Jun 22 16:32 .git -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al total 4 drwxrwxr-x 7 xxx xxx 4096 Jun 22 16:32 .git $ ls .git -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al total 4 drwxrwxr-x 7 xxx xxx 4096 Jun 22 16:32 .git $ ls .git branches config description HEAD hooks info objects refs -
git init $ git init talk-git Initialized empty Git repository
in /xxx/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al total 4 drwxrwxr-x 7 xxx xxx 4096 Jun 22 16:32 .git $ ls .git branches config description HEAD hooks info objects refs $ vim .git/config -
git init $ git init talk-git Initialized empty Git repository
in /xxx/src/talk-git/.git/ $ cd talk-git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $ cd talk-git $ ls -Al total 4 drwxrwxr-x 7 xxx xxx 4096 Jun 22 16:32 .git $ ls .git branches config description HEAD hooks info objects refs $ vim .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true -
git init .git/config <- where your git preferences are stored
for the repo .git/HEAD <- where your work tree points to .git/description .git/refs/ <- where your branches point to .git/hooks/ <- automation scripts .git/branches/ .git/objects/ <- data lives here .git/log/ <- history of your git activities .git/info/ -
git clone NAME git-clone - Clone a repository into a
new directory SYNOPSIS git clone [--bare] [--depth <depth>] SUMMARY Copy of another repo --bare is the same as with git init --depth lets you pick less commits (useful for checking out with no history) Try it out! - https://github.com/ -
git clone $ git clone https://github.com/xxx/talk-git Cloning into 'talk-git'... remote:
Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done. Initial commit -
git clone $ git clone https://github.com/xxx/talk-git Cloning into 'talk-git'... remote:
Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done. $ ls Initial commit -
git clone $ git clone https://github.com/xxx/talk-git Cloning into 'talk-git'... remote:
Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done. $ ls README.md Initial commit -
git clone $ git clone https://github.com/xxx/talk-git Cloning into 'talk-git'... remote:
Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done. $ ls README.md $ vim .git/config Initial commit -
git clone $ git clone https://github.com/xxx/talk-git Cloning into 'talk-git'... remote:
Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done. $ ls README.md $ vim .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = https://github.com/xxx/talk-git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master Initial commit -
git clone .git/index <- binary file (references to BLOBs) .git/refs/heads/master
<- points to a commit for a local branch .git/refs/remotes/origin .git/refs/remotes/origin/HEAD <- points to a commit for a remote branch .git/packed-refs/ .git/logs/HEAD <- logging your activities .git/logs/refs/remotes .git/logs/refs/heads/master .git/logs/refs/remotes/origin .git/logs/refs/remotes/origin/HEAD .git/objects/47 .git/objects/cd .git/objects/dd .git/objects/47/dbdf0659a968efffdd743224af011882db2e2b <- Actual data for files .git/objects/cd/e833ef40e01cc2393cf65b2f12bcc0409d7332 .git/objects/dd/0a9b447f57d7965f088865b987e6e91a5f824b http://stackoverflow.com/questions/4084921/what-does-the-git-index-contain-exactly -
git submodule NAME git-submodule - Initialize, update or inspect submodules
SYNOPSIS git submodule add <repository> [<path>] SUMMARY Copy of a repo INSIDE another repo Submodules allow foreign repositories to be embedded within a subdirectory of the source tree -
git submodule $ git submodule add [email protected]:agross/git-bisect-demo.git Cloning into 'git-bisect-demo'…
remote: Counting objects: 340, done. remote: Compressing objects: 100% (237/237), done. remote: Total 340 (delta 91), reused 340 (delta 91), pack-reused 0 Receiving objects: 100% (340/340), 1.29 MiB | 507.00 KiB/s, done. Resolving deltas: 100% (91/91), done. Checking connectivity... done. DEPENDENCIES Initial commit -
git submodule $ git submodule add [email protected]:agross/git-bisect-demo.git Cloning into 'git-bisect-demo'…
remote: Counting objects: 340, done. remote: Compressing objects: 100% (237/237), done. remote: Total 340 (delta 91), reused 340 (delta 91), pack-reused 0 Receiving objects: 100% (340/340), 1.29 MiB | 507.00 KiB/s, done. Resolving deltas: 100% (91/91), done. Checking connectivity... done. $ vim .git/config DEPENDENCIES Initial commit -
git submodule $ git submodule add [email protected]:agross/git-bisect-demo.git Cloning into 'git-bisect-demo'…
remote: Counting objects: 340, done. remote: Compressing objects: 100% (237/237), done. remote: Total 340 (delta 91), reused 340 (delta 91), pack-reused 0 Receiving objects: 100% (340/340), 1.29 MiB | 507.00 KiB/s, done. Resolving deltas: 100% (91/91), done. Checking connectivity... done. $ vim .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = https://github.com/xxx/talk-git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [submodule "git-bisect-demo"] url = [email protected]:agross/git-bisect-demo.git DEPENDENCIES Initial commit -
git submodule $ ls -A .git git-bisect-demo .gitmodules README.md $
vim .gitmodules DEPENDENCIES Initial commit -
git submodule $ ls -A .git git-bisect-demo .gitmodules README.md $
vim .gitmodules [submodule "git-bisect-demo"] path = git-bisect-demo url = [email protected]:agross/git-bisect-demo.git DEPENDENCIES Initial commit -
git submodule .git/index <- binary file representing current state .git/modules
.git/modules/git-bisect-demo <- clone of the github repo .git/modules/git-bisect-demo/branches .git/modules/git-bisect-demo/config .git/modules/git-bisect-demo/description ... .git/objects/9f .git/objects/9f/cb792aad6767c2e7346e1e41391761bc71ceb5 .gitmodules <- stores module metadata -
git submodule allows you to build on existing work, with
a separate source tree -
git submodule allows you to build on existing work, with
a separate source tree ....but use composer. Seriously. -
git status NAME git-status - Show the working tree status
SYNOPSIS git status [<options>...] [<pathspec>...] SUMMARY Displays: staged changes (index) unstaged changes untracked files -
git status $ ls git-bisect-demo README.md $ git status On
branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .gitmodules <-staged changes new file: git-bisect-demo <-staged changes Untracked files: (use "git add <file>..." to include in what will be committed) Initial commit -
git status $ ls git-bisect-demo README.md $ git status On
branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .gitmodules <-staged changes new file: git-bisect-demo <-staged changes Untracked files: (use "git add <file>..." to include in what will be committed) Initial commit -
git status $ echo Hi\!, I’m Alice. > alice.txt $
git status Initial commit -
git status $ echo Hi\!, I’m Alice. > alice.txt $
git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .gitmodules <-staged changes new file: git-bisect-demo <-staged changes Untracked files: (use "git add <file>..." to include in what will be committed) alice.txt <-unstaged changes Initial commit -
git status tells you what has changed since the last
commit staged or unstaged -
git commit NAME git-commit - Record changes to the repository
SYNOPSIS git commit git commit -m git commit --patch SUMMARY -m will shortcut the commit message --patch for cleaner history (small chunks) -
What is a commit? ✪ A version of all your
files at one point in time ✪ Record of who did what, when ✭ Author ✭ Committer ✭ Date ✭ Subject / Description ✪ Has a parent commit ✪ Stored as collections of trees and BLOBs (objects) ✪ Referenced by a SHA-1 hash of it’s contents -
How should I write a commit? ✪ One commit, one
idea ✪ Subject line (the what) ✭ Limit to 50 chars ✭ This commit will... your subject line here ✮ Refactor subsystem X for readability (imperative voice) ✮ more fixes for broken stuff (indicative voice, unclear) ✪ Body provides context (the why) ✭ Wrap at 72 chars http://chris.beams.io/posts/git-commit/ -
git commit $ git commit Add git-bisect-demo as a submodule
# 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 is up-to-date with 'origin/master'. # # Changes to be committed: # new file: .gitmodules # new file: git-bisect-demo # # Untracked files: # alice.txt Initial commit -
git commit $ git commit Add git-bisect-demo as a submodule
# 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 is up-to-date with 'origin/master'. # # Changes to be committed: # new file: .gitmodules # new file: git-bisect-demo # # Untracked files: # alice.txt [master 24c8660] Add git-bisect-demo as a submodule 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 git-bisect-demo Initial commit git-bisect-demo -
git commit .git/COMMIT_EDITMSG <- Your commit message .git/HEAD <- points
to current commit .git/index <- Staged changes .git/refs/heads/master <- master branch has moved .git/objects/9f <- MOAR data .git/objects/9f/cb792aad6767c2e7346e1e41391761bc71ceb5 http://stackoverflow.com/questions/17595524/orig-head-fetch-head-merge-head-etc -
git add NAME git-add - Add file contents to the
index SYNOPSIS git add [--patch | -p] [--update | -u]] SUMMARY --patch for cleaner history (small chunks) --update only work with files in the index useful for deleting files -
git add $ git status On branch master Your branch
is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) alice.txt nothing added to commit but untracked files present (use "git add" to track) Initial commit git-bisect-demo -
git add $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt Initial commit git-bisect-demo -
git add+commit $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt $ git commit -m 'Add alice.txt' Initial commit git-bisect-demo -
git add+commit $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt $ git commit -m 'Add alice.txt' [master 712cc39] Add alice.txt 1 file changed, 1 insertion(+) create mode 100644 alice.txt Initial commit git-bisect-demo alice -
git add+commit $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt $ git commit -m 'Add alice.txt' [master 712cc39] Add alice.txt 1 file changed, 1 insertion(+) create mode 100644 alice.txt Initial commit git-bisect-demo alice -
git add+commit $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt $ git commit -m 'Add alice.txt' [master 712cc39] Add alice.txt 1 file changed, 1 insertion(+) create mode 100644 alice.txt $ git status Initial commit git-bisect-demo alice -
git add+commit $ git add alice.txt $ git status On
branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: alice.txt $ git commit -m 'Add alice.txt' [master 712cc39] Add alice.txt 1 file changed, 1 insertion(+) create mode 100644 alice.txt $ git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working directory clean Initial commit git-bisect-demo alice -
Store and record changes to files ...What do I do
with this stuff? -
Manage and compare versions ✪ Context switch. What changed? ✭
branch ✭ tag ✭ checkout ✭ diff -
git branch NAME git-branch - List, create, or delete branches
SYNOPSIS git branch (-a | -r) git branch (-d | -D) <branchname> SUMMARY git branch lists branches git branch <branchname> creates a branch git branch -d <branchname> deletes a branch -
What is a branch? ✪ Divergent line of work ✭
master ✭ release / develop ✭ feature / hotfix ✪ A text file ✪ A bookmark ✭ points to a commit ✭ moves when you make another commit on top -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master Initial commit git-bisect-demo alice remotes/origin/master master -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch Initial commit git-bisect-demo alice remotes/origin/master master -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch Initial commit git-bisect-demo alice remotes/origin/master master randombranch -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch $ git branch alice-interests Initial commit git-bisect-demo alice remotes/origin/master master randombranch -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch $ git branch alice-interests Initial commit git-bisect-demo alice remotes/origin/master master randombranch alice-interests -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch $ git branch alice-interests $ git branch -d randombranch Initial commit git-bisect-demo alice remotes/origin/master master randombranch alice-interests -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch $ git branch alice-interests $ git branch -d randombranch Initial commit git-bisect-demo alice remotes/origin/master master randombranch alice-interests -
git branch $ git branch -a * master remotes/origin/HEAD ->
origin/master remotes/origin/master $ git branch randombranch $ git branch alice-interests $ git branch -d randombranch Deleted branch randombranch (was 712cc39). Initial commit git-bisect-demo alice remotes/origin/master master alice-interests -
git branch .git/refs/heads/alice-interests <- points to position of branch HEAD
.git/logs/refs/heads/alice-interests <- tracks movement of HEAD over time -
What is a tag? ✪ Marker ✭ release version ✭
bug / working version ✭ diff base ✪ A text file ✪ A fixed-in-stone-bookmark ✭ points to a commit ✭ does not move as new commits are added -
git checkout NAME git-checkout - Checkout a branch or paths
to the working tree SYNOPSIS git checkout [<commit>] [<paths>...] SUMMARY git checkout <commit> jump to branch, checkout all files git checkout <commit> <paths>... don’t jump to branch, checkout specific files (NOTE:You can swap <commit> for a branch/tag) -
git checkout $ git checkout alice-interests Initial commit git-bisect-demo alice
remotes/origin/master master alice-interests -
git checkout $ git checkout alice-interests Switched to branch 'alice-interests'
Initial commit git-bisect-demo alice remotes/origin/master master alice-interests -
git checkout $ git checkout alice-interests Switched to branch 'alice-interests'
$ echo -e "\nI like rainbows." >> alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests -
git checkout $ git checkout alice-interests Switched to branch 'alice-interests'
$ echo -e "\nI like rainbows." >> alice.txt $ git add alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests -
git checkout $ git checkout alice-interests Switched to branch 'alice-interests'
$ echo -e "\nI like rainbows." >> alice.txt $ git add alice.txt $ git commit -m "Note that Alice likes rainbows." Initial commit git-bisect-demo alice remotes/origin/master master alice-interests -
git checkout $ git checkout alice-interests Switched to branch 'alice-interests'
$ echo -e "\nI like rainbows." >> alice.txt $ git add alice.txt $ git commit -m "Note that Alice likes rainbows." [alice-interests 1593781] Note that Alice likes rainbows. 1 file changed, 3 insertions(+) Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Initial commit git-bisect-demo alice remotes/origin/master
master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt $ cat alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt $ cat alice.txt Hi!, I’m Alice. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt $ cat alice.txt Hi!, I’m Alice. $ git checkout HEAD alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt $ cat alice.txt Hi!, I’m Alice. $ git checkout HEAD alice.txt $ cat alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows http://stackoverflow.com/questions/17595524/orig-head-fetch-head-merge-head-etc -
git checkout $ cat alice.txt Hi!, I’m Alice. I like
rainbows. $ git checkout master alice.txt $ cat alice.txt Hi!, I’m Alice. $ git checkout HEAD alice.txt $ cat alice.txt Hi!, I’m Alice. I like rainbows. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows http://stackoverflow.com/questions/17595524/orig-head-fetch-head-merge-head-etc -
git checkout .git/refs/heads/alice-interests <- points to position of branch HEAD
.git/logs/refs/heads/alice-interests <- tracks movement of HEAD over time (Nothing changes if it’s just a file) -
git diff NAME git-diff - Show changes between commits, commit
and working tree, etc SYNOPSIS git diff [<commit>] git diff --cached [<commit>] SUMMARY git diff [<commit>] compares work tree git diff --cached [<commit>] compares index (NOTE:You can swap <commit> for a branch/tag) -
git diff $ echo -e "\nI also like unicorns." >>
alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ echo -e "\nI also like unicorns." >>
alice.txt $ git status Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ echo -e "\nI also like unicorns." >>
alice.txt $ git status On branch alice-interests 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: alice.txt no changes added to commit (use "git add" and/or "git commit -a") Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ echo -e "\nI also like unicorns." >>
alice.txt $ git status On branch alice-interests 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: alice.txt no changes added to commit (use "git add" and/or "git commit -a") $ git diff Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ echo -e "\nI also like unicorns." >>
alice.txt $ git status On branch alice-interests 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: alice.txt no changes added to commit (use "git add" and/or "git commit -a") $ git diff diff --git a/alice.txt b/alice.txt index ac97c89..3da64f6 100644 --- a/alice.txt +++ b/alice.txt @@ -1,3 +1,5 @@ Hi!, I’m Alice. I like rainbows. + +I also like unicorns. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . Initial commit git-bisect-demo alice
remotes/origin/master master alice-interests rainbows -
git diff $ git add . Initial commit git-bisect-demo alice
remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status Initial
commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status On
branch alice-interests Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: alice.txt Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status On
branch alice-interests Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: alice.txt $ git diff Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status On
branch alice-interests Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: alice.txt $ git diff $ git diff --cached Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status On
branch alice-interests Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: alice.txt $ git diff $ git diff --cached diff --git a/alice.txt b/alice.txt index ac97c89..3da64f6 100644 --- a/alice.txt +++ b/alice.txt @@ -1,3 +1,5 @@ Hi!, I’m Alice. I like rainbows. + +I also like unicorns. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git add . $ git status On
branch alice-interests Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: alice.txt $ git diff $ git diff --cached diff --git a/alice.txt b/alice.txt index ac97c89..3da64f6 100644 --- a/alice.txt +++ b/alice.txt @@ -1,3 +1,5 @@ Hi!, I’m Alice. I like rainbows. + +I also like unicorns. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git commit -m "Note that alice also
likes unicorns" Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows -
git diff $ git commit -m "Note that alice also
likes unicorns" [alice-interests 047fc66] Note that alice also likes unicorns 1 file changed, 2 insertions(+) Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git diff $ git commit -m "Note that alice also
likes unicorns" [alice-interests 047fc66] Note that alice also likes unicorns 1 file changed, 2 insertions(+) $ git diff master Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git diff $ git commit -m "Note that alice also
likes unicorns" [alice-interests 047fc66] Note that alice also likes unicorns 1 file changed, 2 insertions(+) $ git diff master diff --git a/alice.txt b/alice.txt index f463940..3da64f6 100644 --- a/alice.txt +++ b/alice.txt @@ -1 +1,5 @@ Hi!, I’m Alice. + +I like rainbows. + +I also like unicorns. Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
Playing nicely with others ✪ Learn. Collaborate. Share. Review ✭
push ✭ fetch ✭ merge ✮ fast forward ✮ 3-way ✭ pull (fetch + merge) ✭ cherry-pick ✭ rebase ✮ Lots of cherry picks -
git push NAME git-push - Update remote refs along with
associated objects SYNOPSIS git push [<remote> <source>:<destination>] SUMMARY Allows you to share your code Can specify remote - where to push to source - local branch destination - remote branch -
git push $ git checkout master Initial commit git-bisect-demo alice
remotes/origin/master master alice-interests rainbows unicorns -
git push $ git checkout master Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git push $ git checkout master Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) $ git push Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git push $ git checkout master Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) $ git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 651 bytes | 0 bytes/s, done. Total 6 (delta 1), reused 0 (delta 0) To https://github.com/xxx/talk-git cde833e..712cc39 master -> master Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git push $ git checkout master Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) $ git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 651 bytes | 0 bytes/s, done. Total 6 (delta 1), reused 0 (delta 0) To https://github.com/xxx/talk-git cde833e..712cc39 master -> master Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git push $ git checkout master Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) $ git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 651 bytes | 0 bytes/s, done. Total 6 (delta 1), reused 0 (delta 0) To https://github.com/xxx/talk-git cde833e..712cc39 master -> master // (remote local-source:remote-destination) //Same as $ git push origin master:master Initial commit git-bisect-demo alice remotes/origin/master master alice-interests rainbows unicorns -
git push .git/refs/remotes/origin/master <- points to position of branch HEAD
.git/logs/refs/remotes/origin/master <- tracks movement of HEAD over time -
git fetch NAME git-fetch - Download objects and refs from
another repository SYNOPSIS git fetch [<remote> <source>:<destination>] SUMMARY Allows you to get someone else’s code Can specify remote - where to download from source - remote branch destination - local branch -
$ git fetch remote: Counting objects: 3, done. remote: Compressing
objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/xxx/talk-git 712cc39..ca980cb master -> origin/master git fetch alice remotes/origin/master master alice-interests rainbows unicorns hates -
$ git fetch remote: Counting objects: 3, done. remote: Compressing
objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/xxx/talk-git 712cc39..ca980cb master -> origin/master //Same as $ git fetch origin master:master // (remote remote-source:local-destination) git fetch alice remotes/origin/master master alice-interests rainbows unicorns hates -
$ git fetch remote: Counting objects: 3, done. remote: Compressing
objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/xxx/talk-git 712cc39..ca980cb master -> origin/master //Same as $ git fetch origin master:master // (remote remote-source:local-destination) $ git checkout git fetch alice remotes/origin/master master alice-interests rainbows unicorns hates -
$ git fetch remote: Counting objects: 3, done. remote: Compressing
objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/xxx/talk-git 712cc39..ca980cb master -> origin/master //Same as $ git fetch origin master:master // (remote remote-source:local-destination) $ git checkout Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) git fetch alice remotes/origin/master master alice-interests rainbows unicorns hates -
git fetch .git/refs/remotes/origin/alice-interests <- points to position of branch HEAD
.git/logs/refs/remotes/origin/alice-interests <- tracks movement of HEAD over time .git/objects/??/??? <- new commits! -
git merge NAME git-merge - Join two or more development
histories together SYNOPSIS git merge [--no-ff] <branch> SUMMARY Combines code from two separate branches fast-forward moves the branch pointer 3 way merge combines divergent files --no-ff prevents fast forward merges -
$ git checkout Your branch is behind 'origin/master' by 1
commit, and can be fast-forwarded. (use "git pull" to update your local branch) $ git merge origin/master git merge alice remotes/origin/master master alice-interests rainbows unicorns hates -
$ git checkout Your branch is behind 'origin/master' by 1
commit, and can be fast-forwarded. (use "git pull" to update your local branch) $ git merge origin/master Updating 712cc39..ca980cb Fast-forward alice.txt | 2 ++ 1 file changed, 2 insertions(+) git merge alice remotes/origin/master alice-interests rainbows unicorns hates master -
a fast-forward merge moves the branch pointer ‘forward’ to a
subsequent commit ...No need to create new commits -
$ git diff alice-interests diff --git a/alice.txt b/alice.txt index 3da64f6..d30e97a
100644 --- a/alice.txt +++ b/alice.txt @@ -1,5 +1,3 @@ Hi!, I’m Alice. -I like rainbows. - -I also like unicorns. +I hate merge conflicts git merge alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git merge alice-interests Auto-merging alice.txt CONFLICT (content): Merge conflict
in alice.txt Automatic merge failed; fix conflicts and then commit the result. alice remotes/origin/master alice-interests rainbows unicorns hates master git merge -
$ git merge alice-interests Auto-merging alice.txt CONFLICT (content): Merge conflict
in alice.txt Automatic merge failed; fix conflicts and then commit the result. $ vim alice.txt alice remotes/origin/master alice-interests rainbows unicorns hates master git merge -
$ git merge alice-interests Auto-merging alice.txt CONFLICT (content): Merge conflict
in alice.txt Automatic merge failed; fix conflicts and then commit the result. $ vim alice.txt Hi!, I’m Alice. <<<<<<< HEAD I hate merge conflicts ======= I like rainbows. I also like unicorns. >>>>>>> alice-interests alice remotes/origin/master alice-interests rainbows unicorns hates master git merge (3-way) -
$ git merge alice-interests Auto-merging alice.txt CONFLICT (content): Merge conflict
in alice.txt Automatic merge failed; fix conflicts and then commit the result. $ vim alice.txt Hi!, I’m Alice. I hate merge conflicts. I like rainbows. I also like unicorns. alice remotes/origin/master alice-interests rainbows unicorns hates master git merge (3-way) -
$ git add alice.txt alice remotes/origin/master alice-interests rainbows unicorns hates
master git merge (3-way) -
$ git add alice.txt $ git commit alice remotes/origin/master alice-interests
rainbows unicorns hates master git merge (3-way) -
$ git add alice.txt $ git commit Merge branch 'alice-interests'
# Conflicts: # alice.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 is up-to-date with 'origin/master'. # # All conflicts fixed but you are still merging. # # Changes to be committed: # modified: alice.txt # git merge (3-way) alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git add alice.txt $ git commit [master 257b18b] Merge
branch 'alice-interests' git merge (3-way) alice remotes/origin/master alice-interests rainbows unicorns hates master merge -
If we cannot fast forward, git will 3-way merge two
branch tips with a common base ...Creating a new commit -
git merge .git/refs/heads/alice-interests <- points to position of branch HEAD
.git/logs/refs/heads/alice-interests <- tracks movement of HEAD over time .git/objects/??/?? <- new commits! .git/MERGE_HEAD also changes during this process -
git cherry-pick NAME git-cherry-pick - Apply the changes introduced by
some existing commits SYNOPSIS git cherry-pick <commit> SUMMARY Applies a single commit to the HEAD of your branch -
$ git reset --hard origin/master HEAD is now at 123b993
Note that Alice hates merge conflicts git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' git cherry-pick
alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 Note: checking out 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 1593781... Note that Alice likes rainbows. //note this is the same as HEAD^ git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 Note: checking out 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 1593781... Note that Alice likes rainbows. //note this is the same as HEAD^ $ git tag rainbows git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 Note: checking out 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 1593781... Note that Alice likes rainbows. //note this is the same as HEAD^ $ git tag rainbows git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 Note: checking out 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 1593781... Note that Alice likes rainbows. //note this is the same as HEAD^ $ git tag rainbows $ git checkout master git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout HEAD~1 Note: checking out 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 1593781... Note that Alice likes rainbows. //note this is the same as HEAD^ $ git tag rainbows $ git checkout master Your branch is up-to-date with 'origin/master'. git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git cherry-pick rainbows git cherry-pick alice remotes/origin/master alice-interests rainbows
unicorns hates master rainbows -
$ git cherry-pick rainbows error: could not apply 1593781... Note
that Alice likes rainbows. hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit' git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ vim alice.txt Hi!, I’m Alice. <<<<<<< HEAD I hate
merge conflicts ======= I like rainbows. >>>>>>> 1593781... Note that Alice likes rainbows. git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ vim alice.txt Hi!, I’m Alice. I hate merge conflicts
I like rainbows. git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git add alice.txt git cherry-pick alice remotes/origin/master alice-interests rainbows
unicorns hates master rainbows -
$ git add alice.txt $ git status git cherry-pick alice
remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git add alice.txt $ git status On branch master
Your branch is up-to-date with 'origin/master'. You are currently cherry-picking commit 1593781. (all conflicts fixed: run "git cherry-pick --continue") (use "git cherry-pick --abort" to cancel the cherry-pick operation) Changes to be committed: modified: alice.txt git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git cherry-pick --continue git cherry-pick alice remotes/origin/master alice-interests rainbows
unicorns hates master rainbows -
$ git cherry-pick --continue Note that Alice likes rainbows. #
Conflicts: # alice.txt # # It looks like you may be committing a cherry-pick. # If this is not correct, please remove the file # .git/CHERRY_PICK_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. # # Date: Wed Jun 24 15:46:23 2015 +1000 # # On branch master # Your branch is up-to-date with 'origin/master'. # # You are currently cherry-picking commit 1593781. # # Changes to be committed: # modified: alice.txt # git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git cherry-pick --continue Note that Alice likes rainbows. #
Conflicts: # alice.txt # # It looks like you may be committing a cherry-pick. # If this is not correct, please remove the file # .git/CHERRY_PICK_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. # # Date: Wed Jun 24 15:46:23 2015 +1000 # # On branch master # Your branch is up-to-date with 'origin/master'. # # You are currently cherry-picking commit 1593781. # # Changes to be committed: # modified: alice.txt # git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows -
$ git cherry-pick --continue [master 2bd4e74] Note that Alice likes
rainbows. Date: Wed Jun 24 15:46:23 2015 +1000 1 file changed, 1 insertion(+) git cherry-pick alice remotes/origin/master alice-interests rainbows unicorns hates rainbows cherry master -
git cherry-pick .git/refs/heads/master <- points to position of branch HEAD
.git/logs/refs/heads/master <- tracks movement of HEAD over time .git/objects/??/?? <- new commits! .git/CHERRY_PICK_HEAD also changes during this process -
git rebase NAME git-rebase - Forward-port local commits to the
updated upstream head SYNOPSIS git rebase [--interactive] <branch> SUMMARY Fetches, then merges (or rebases with --rebase) -
$ git reset --hard origin/master HEAD is now at 123b993
Note that Alice hates merge conflicts git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' git rebase
alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout -b alice-interests-rebase git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master -
$ git checkout alice-interests Switched to branch 'alice-interests' $ git
checkout -b alice-interests-rebase Switched to a new branch 'alice-interests-rebase' git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ git rebase master git rebase alice remotes/origin/master alice-interests rainbows
unicorns hates master alice-interests-rebase -
$ git rebase master git rebase alice remotes/origin/master alice-interests rainbows
unicorns hates master alice-interests-rebase -
$ git rebase master First, rewinding head to replay your
work on top of it... git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ git rebase master First, rewinding head to replay your
work on top of it… Applying: Note that Alice likes rainbows. git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ git rebase master First, rewinding head to replay your
work on top of it… Applying: Note that Alice likes rainbows. Using index info to reconstruct a base tree... M alice.txt Falling back to patching base and 3-way merge... Auto-merging alice.txt CONFLICT (content): Merge conflict in alice.txt Failed to merge in the changes. Patch failed at 0001 Note that Alice likes rainbows. The copy of the patch that failed is found in: /xxx/talk-git/.git/rebase-apply/patch When you have resolved this problem, run "git rebase --continue". If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort". git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ vim alice.txt Hi!, I’m Alice. <<<<<<< HEAD I hate
merge conflicts ======= I like rainbows. >>>>>>> Note that Alice likes rainbows. git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ vim alice.txt Hi!, I’m Alice. I hate merge conflicts
I like rainbows. git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ vim alice.txt Hi!, I’m Alice. I hate merge conflicts
I like rainbows. $ git add . git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ git rebase --continue git rebase alice remotes/origin/master alice-interests rainbows
unicorns hates master alice-interests-rebase -
$ git rebase --continue Applying: Note that Alice likes rainbows.
git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase -
$ git rebase --continue Applying: Note that Alice likes rainbows.
Applying: Note that alice also likes unicorns git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase rainbows-rb -
$ git rebase --continue Applying: Note that Alice likes rainbows.
Applying: Note that alice also likes unicorns Using index info to reconstruct a base tree... M alice.txt git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase rainbows-rb -
$ git rebase --continue Applying: Note that Alice likes rainbows.
Applying: Note that alice also likes unicorns Using index info to reconstruct a base tree... M alice.txt Falling back to patching base and 3-way merge... git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase rainbows-rb -
$ git rebase --continue Applying: Note that Alice likes rainbows.
Applying: Note that alice also likes unicorns Using index info to reconstruct a base tree... M alice.txt Falling back to patching base and 3-way merge... Auto-merging alice.txt git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master alice-interests-rebase rainbows-rb -
$ git rebase --continue Applying: Note that Alice likes rainbows.
Applying: Note that alice also likes unicorns Using index info to reconstruct a base tree... M alice.txt Falling back to patching base and 3-way merge... Auto-merging alice.txt $ git rebase alice remotes/origin/master alice-interests rainbows unicorns hates master rainbows-rb unicorns alice-interests-rebase -
git rebase .git/refs/heads/master <- points to position of branch HEAD
.git/logs/refs/heads/master <- tracks movement of HEAD over time .git/objects/??/?? <- new commits! .git/REBASE_HEAD also changes during this process -
Rebase disclaimer ✪ Re-writing history ✭ Duplicating commits - may
break some git operations ✮ git branch --merged, --contains etc. ✮ git bisect ✭ Not for public branches (you will break peoples trees!) ✮ force pushing makes bunny cry! ✭ Tags will be incorrect (they never move) ✪ New base ✭ Integration issues may not be apparent ✭ Reverted code gets thrown away (fast-forwarded) http://paul.stadig.name/2010/12/thou-shalt-not-lie-git-rebase-ammend.html -
git pull NAME Fetch from and integrate with another repository
or a local branch SYNOPSIS git pull [options] [<repository> [<refspec>...]] SUMMARY Fetches, then merges (or rebases with --rebase) -
git pull is a shorthand for git fetch && git
merge (or git rebase with --rebase) -
✪ Easier to revert in an emergency ✪ Easier to
track down bugs ✪ Easier to read! Linear vs Non-Linear source tree -
Restore previous work ✪ Go back in time. Undo mistakes.
✭ stash ✭ reset ✭ revert -
git stash NAME git-stash - Stash the changes in a
dirty working directory away SYNOPSIS git stash git stash pop SUMMARY stash hides your work tree stash pop brings it back -
git reset NAME git-reset - Reset current HEAD to the
specified state SYNOPSIS git reset (--hard | --soft | --mixed) [<commit>] SUMMARY --hard moves HEAD, throws away work tree --soft moves HEAD, stages work tree (index) --mixed moves HEAD, unstages work tree HEAD == BRANCH -
git revert NAME git-revert - Revert some existing commits SYNOPSIS
git revert [-m parent#] <commit> SUMMARY Writes a new commit, negating the old one. Can undo an entire feature branch by reverting a merge commit ONE COMMIT = ONE IDEA. VERY IMPORTANT. merge --no-ff also important (so you can revert all changes in branch) -
Revert pitfalls ✪ Blackballing commits ✭ Revert “means” I do
not want to release this ✭ Break subsequent rebases/merges ✮ git will avoid duplication of existing commits ✮ Must revert the revert commit ✭ May break some analysis tools ✭ Harder to read history ✭ Integration issues may not be apparent http://paul.stadig.name/2010/12/thou-shalt-not-lie-git-rebase-ammend.html -
git show $ git show 741600aaf5ba3c8f9d7b78b5082370141a2a4632 commit 741600aaf5ba3c8f9d7b78b5082370141a2a4632 Merge: 123b993
047fc66 Author: xxx Date: Wed Jun 24 21:10:11 2015 +1000 Merge branch 'alice-interests' diff --cc alice.txt index d30e97a,3da64f6..64edcef --- a/alice.txt +++ b/alice.txt @@@ -1,3 -1,5 +1,5 @@@ Hi!, I’m Alice. - I hate merge conflicts ++I hate merge conflicts. + I like rainbows. - -I also like unicorns. ++I also like unicorns. http://www.justinweiss.com/blog/2015/03/24/go-beyond-the-easy-fix-with-code-archaeology/ -
git cat-file $ cat .git/refs/heads/merged 741600aaf5ba3c8f9d7b78b5082370141a2a4632 $ git cat-file -p
741600aaf5ba3c8f9d7b78b5082370141a2a4632 tree 9a286e419d26c301178738c752e85a5cbf17cbf2 parent 123b9930155cc4178daf5e04c770e33c30fd2f32 parent 047fc663da7735d5505873756c02ad0414e88804 author xxx <xxx> 1435144211 +1000 committer xxx <xxx> 1435144211 +1000 Merge branch 'alice-interests' $ git cat-file -p 9a286e419d26c301178738c752e85a5cbf17cbf2 100644 blob 9fcb792aad6767c2e7346e1e41391761bc71ceb5 .gitmodules 100644 blob 47dbdf0659a968efffdd743224af011882db2e2b README.md 100644 blob 64edcef09c4acbf7763589a68ce6c77f54d11d42 alice.txt 160000 commit 7c5ad9134934288d364ec5befd1f293f12d760f3 git-bisect-demo $ git cat-file -p 64edcef09c4acbf7763589a68ce6c77f54d11d42 Hi!, I’m Alice. I hate merge conflicts. I like rainbows. -
git reflog NAME git-reflog - Manage reflog information SYNOPSIS git
reflog SUMMARY Shows a log of commits you have switched to while working -
git reflog $ git reflog 123b993 HEAD@{0}: checkout: moving from
alice-interests-rebase to master 32d29b9 HEAD@{1}: rebase finished: returning to refs/heads/alice-interests-rebase 32d29b9 HEAD@{2}: rebase: Note that alice also likes unicorns a06280e HEAD@{3}: rebase: Note that Alice likes rainbows. 741600a HEAD@{13}: checkout: moving from master to merge 70fc2b6 HEAD@{16}: commit (cherry-pick): Note that Alice likes rainbows. 1593781 HEAD@{34}: checkout: moving from alice-interests to HEAD~1 047fc66 HEAD@{36}: checkout: moving from master to alice-interests 123b993 HEAD@{38}: reset: moving to origin/master 257b18b HEAD@{42}: commit (merge): Merge branch 'alice-interests' ca980cb HEAD@{43}: merge origin/master: Fast-forward 712cc39 HEAD@{48}: checkout: moving from alice-interests to master 047fc66 HEAD@{49}: commit: Note that alice also likes unicorns fcd5644 HEAD@{52}: commit: Note that Alice likes rainbows. b815e8e HEAD@{59}: commit: Add Alice's interests 712cc39 HEAD@{60}: commit: Add alice.txt 36d64b1 HEAD@{61}: commit: Add git-bisect-demo as a submodule cde833e HEAD@{62}: reset: moving to cde833ef40e01cc2393cf65b2f12bcc0409d7332 24c8660 HEAD@{63}: commit: Add git-bisect-demo as a submodule cde833e HEAD@{64}: clone: from https://github.com/xxx/talk-git (excerpt) -
.git/hooks/pre-commit #!/usr/bin/php <?php $output = array(); $return = 0; exec('git
rev-parse --verify HEAD 2> /dev/null', $output, $return); $against = $return == 0 ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; exec("git diff-index --cached --name-only {$against}", $output); $filename_pattern = '/\.php$/'; $exit_status = 0; foreach ($output as $file) { if (!preg_match($filename_pattern, $file) || !file_exists($file)) { // don't check files that aren't PHP continue; } $lint_output = array(); exec("php -l " . escapeshellarg($file), $lint_output, $return); if ($return == 0) { continue; } echo implode("\n", $lint_output), "\n"; $exit_status = 1; } exit($exit_status); NO MORE SYNTAX ERRORS -
.git/hooks/post-receive #!/bin/sh #Update GIT_WORK_TREE=/YOUR/WEB/ROOT echo "********************************************" echo "Post receive hook
- Updating:" echo $GIT_WORK_TREE echo "********************************************" sudo rm -Rf $GIT_WORK_TREE sudo mkdir $GIT_WORK_TREE sudo chown -R git $GIT_WORK_TREE GIT_WORK_TREE=$GIT_WORK_TREE git checkout master -f #set perms sudo chown -R www-data $GIT_WORK_TREE sudo chgrp -R www-data $GIT_WORK_TREE echo "********************************************" echo "Updated:" echo $GIT_WORK_TREE echo "********************************************" CI/CD IN THE GHETTO https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate- development-and-deployment-tasks -
git is commandline - scripts! ✪ All the tools pretty
much trigger off git operations ✪ git alias or roll your own shell scripts to save typing ✭ checkout master && git pull --rebase && arc patch $1 ✭ git add && clear && git status ✭ git diff --cached && git status && echo ---- && git commit ✪ Piping output into other scripts / commands ✭ Prune old branches ✮ checkout master && git branch --merged | xargs git branch -d ✭ Parse logs ✭ Automated bug hunting with git bisect -
git bisect NAME git-bisect - Find by binary search the
change that introduced a bug SYNOPSIS git bisect start git bisect bad git bisect good git bisect run (scripted) SUMMARY Searches through your commits, allowing you to eliminate ‘good’ commits until you find the ‘bad’ commit that introduced a bug. https://github.com/agross/git-bisect-demo (also a module in talk source) -
MOAR Links! ✪ General ✭ http://git-scm.com/doc ✭ https://www.atlassian.com/git/tutorials/advanced-overview ✭ https://speakerdeck.com/holman/git-and-github-secrets
✭ http://zrusin.blogspot.com.au/2007/09/git-cheat-sheet.html ✪ Rebasing / Workflow ✭ https://www.atlassian.com/git/articles/git-team-workflows-merge-or-rebase/ ✪ Tutorials ✭ http://pcottle.github.io/learnGitBranching/ ✭ http://gitready.com/