Share
Embed
Understanding git
This is an internal training I made at Blablacar to better understand Git with visual graphics.
Matthieu Moquet
October 09, 2014
Transcript
164 slides
-
Today’s topics The basics of commit git add, commit, revert,
reset How to move in a git tree git checkout, HEAD References git branch, tag, merge History manipulation git cherry-pick, git rebase (—interactive) Working with a remote server git clone, remote, push, pull, fetch, merge, rebase -
Requirement Before using Git, make sure you correctly configured your
user: git config --global user.name "Matthieu Moquet" git config --global user.email [email protected] write in your global ~/.gitconfig Your real name -
What is Git? Git is a distributed version control system.
It works by creating local snapshots of all your files, like a big copy/paste, but better optimized. -
src/ Foobar.php Baz.php README.md New.php src/ Foobar.php Baz.php README.md New.php
abcdef234 Initial commit Workspace -
src/ Foobar.php Baz.php README.md New.php deadbeef42 A new file abcdef234
Initial commit Workspace -
src/ Foobar.php Baz.php README.md New.php src/ Foobar.php Baz.php README.md New.php
abcdef234 Initial commit deadbeef42 A new file Workspace -
src/ Foobar.php Baz.php README.md New.php fear8342abc Update foobar abcdef234 Initial
commit deadb33f42 A new file Workspace internally it only stores the deltas between commits -
fear8342abc Update foobar abcdef234 Initial commit deadb33f42 A new file
a commit: • is a snapshot of the sources • is identified by its hash (sha1) • has one (or many) parents • is immutable git commit once a commit is done, you can NOT modify it! -
➜ git init ➜ vim README.md ➜ git status ➜
git add README.md ➜ git commit ➜ git status Initialized empty Git repository in /path/to/myproject/.git/ Untracked files: (use "git add <file>..." to include in what will be committed) ! README.md Changes to be committed: (use "git reset HEAD <file>..." to unstage) ! new file: README.md [master (root-commit) 4b60dee] My commit ➜ git status On branch master nothing to commit, working directory clean -
➜ git commit -A -m "Your commit message" Shortcut git
add ALL FILES inline commit message -
Best practice ➜ git add -p diff --git a/README.md b/README.md
index 540550a..2f10d41 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -This is an old line +This is the new line Stage this hunk [y,n,q,a,d,/,e,?]? y diff --git a/src/Foobar.php b/src/Foobar.php index 540550a..2f10d41 100644 --- a/src/Foobar.php +++ b/src/Foobar.php @@ -1 +1 @@ -class Foobar -{ - const BAZ = 'world'; -} Stage this hunk [y,n,q,a,d,/,e,?]? git add patch -
➜ git commit Capitalized, short (50 chars or less) summary
! More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. ! Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." ! - Bullet points are okay, too - Use a hanging indent Best practice Open in your editor http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html -
Best practice commit 9342a5bf855042b29a2ebd3413ce8e6cfb65557f Author: Matthieu Moquet <[email protected]> Date: Thu
Sep 18 13:37:00 2014 +0200 ! Allow scope to be defined in the request entity body ! As defined in the RFC 6749: http://tools.ietf.org/html/rfc6749#section-4.4.2 ! Note that this change is backward compatible as it will first look up the request query. -
Best practice commit c1230da7409d22fe78263da777da237e9fc702d4 Author: Matthieu Moquet <[email protected]> Date: Tue
Jul 29 17:00:33 2014 +0200 ! Use UserChecker in OAuth2 listener ! In order to deny block users on every API call ! Fix https://jira.example.net/browse/FOO-123 -
git show abcdef Display the commit message + the diff
with the previous one git diff abcdef dfebca Display the diff between the two commits git diff abcd~ abcd How to display the diff between commits? -
git diff --staged Display the change put in the staging
area git diff Display the file I changed but not committed/staged How to display the diff between commits? -
What if I made a mistake and want to rollback
the committed code? -
Case #1 I made a typo on my previous commit,
let’s fix it quickly -
git commit --amend Replace previous commit with new one, using
the staging area git commit Oops, I made a mistake (typo, forget file, wrong commit msg) git add the_file.txt Add the concerned file into the staging area -
git commit Meh, I shouldn’t have committed git reset HEAD~
Cancel the commit, but keep the change locally, so you can create another one git reset --hard HEAD~ Cancel the commit, and remove all the change -
Case #3 I need to revert one or several commits
already pushed into production -
git commit Later: oops this commit (abc123) breaks something git
revert abc123 Create a new commit, which is the opposite of the given one -
git log master HEAD fear8342abc Update foobar abcdef234 Initial commit
deadb33f42 A new file Helps you to visualize the git tree start from a given commit… …and follow the parents to go through the history -
commit 9342a5bf855042b29a2ebd3413ce8e6cfb65557f Author: Matthieu Moquet <[email protected]> Date: Thu Sep 18
13:37:00 2014 +0200 ! Allow scope to be defined in the request entity body ! As defined in the RFC 6749: http://tools.ietf.org/html/rfc6749#section-4.4.2 ! Note that this change is backward compatible as it will first look up the request query. ! commit c851bca7bf85504fe82639342acb237e9fc87420 Author: Matthieu Moquet <[email protected]> Date: Tue Jul 30 12:34:56 2014 +0200 ! Update README.md ! commit c1230da7409d22fe78263da777da237e9fc702d4 Author: Matthieu Moquet <[email protected]> Date: Tue Jul 29 17:00:33 2014 +0200 ! Use UserChecker in OAuth2 listener ! In order to deny block users on every API call ! Fix https://jira.example.net/browse/FOO-123 git log -
[alias] lg = log --graph --abbrev-commit --date=relative \ --pretty=tformat:'%Cred%h%Creset -...
%s %Cgreen(%an %cr)%Creset' git log Shortcut -
* 1187cf9 - (HEAD, master) Merge branch ‘update-ui' (Matthieu Moquet
2 hours ago) |\ | * 3b1ba4e - Compile assets via Gulp (Matthieu Moquet 2 hours ago) | * 90575d5 - Fix layout login dependency (Matthieu Moquet 2 hours ago) | * a151f92 - Update editor shortcuts (Matthieu Moquet 2 hours ago) |/ * 56fa6f3 - Merge branch 'project-page' (Matthieu Moquet 3 hours ago) |\ | * 2ee4b97 - Add inLocale parameter to project page (Matthieu Moquet 4 hours ago) | * 73b044f - Update navbar user menu UI (Matthieu Moquet 4 hours ago) | * 6faa5c3 - Rework project page (Matthieu Moquet 6 hours ago) |/ * 0309542 - Merge branch 'rework-model' (Matthieu Moquet 30 hours ago) |\ | * 0082ea5 - Fix duplicate key error on import processor (Matthieu Moquet 2 days ago) | * 8dab5be - Add options parameters for export api (Matthieu Moquet 2 days ago) | * 0649be9 - Warmup symfony cache in Travis build (Matthieu Moquet 2 days ago) | * 6c72e8e - Refactor app architecture (Matthieu Moquet 2 days ago) |/ * 77cb4c9 - Remove old scripts (Matthieu Moquet 4 days ago) * ... * * git lg -
git checkout master fear8342abc Update foobar abcdef234 Initial commit deadb33f42
A new file Workspace ➜ git checkout abcdef234 HEAD -
git checkout master HEAD fear8342abc Update foobar abcdef234 Initial commit
deadb33f42 A new file Workspace copy files -
src/ Foobar.php Baz.php README.md New.php fear8342abc Update foobar abcdef234 Initial
commit deadb33f42 A new file Workspace HEAD -
src/ Foobar.php Baz.php README.md New.php fear8342abc Update foobar abcdef234 Initial
commit deadb33f42 A new file Workspace HEAD -
Workspace src/ Foobar.php Baz.php README.md fear8342abc Update foobar abcdef234 Initial
commit deadb33f42 A new file HEAD -
git checkout abcd1234 -- file.txt Copy the file.txt from abcd1234
to local workspace -
git checkout HEAD -- file.txt Cancel the changes of file.txt
git checkout file.txt -
Branches master HEAD fear8342abc Update foobar abcdef234 Initial commit deadb33f42
A new file A branch is just a pointer to a commit foobar ➜ git branch foobar deadb33f42 -
Branches Internally a branch is simply a reference in a
file ➜ cat .git/refs/heads/foobar deadb33f42d89b21f72f8115e34ad23c507db8b6 ➜ cat .git/HEAD ref: refs/heads/foobar So does HEAD ➜ cat .git/HEAD deadb33f42d89b21f72f8115e34ad23c507db8b6 git checkout foobar git checkout deadb33f42 -
Branches When committing, the reference pointed by the HEAD is
moved to the new commit master HEAD abcdef234 Initial commit deadb33f42 A new file -
Branches When committing, the reference pointed by the HEAD is
moved to the new commit master HEAD fear8342abc Update foobar abcdef234 Initial commit deadb33f42 A new file -
Branches If HEAD points to a commit (and not a
branch), then no branch will be updated master abcdef234 Initial commit deadb33f42 A new file HEAD -
Branches master HEAD fear8342abc Update foobar abcdef234 Initial commit deadb33f42
A new file If HEAD points to a commit (and not a branch), then no branch will be updated orphan commit, no branch is referencing it -
Shortcut Create a new branch and checkout it immediately. ➜
git branch foobar ; git checkout foobar ➜ git checkout -b foobar -
Merges Branches are useful when you’re working on a new
feature. Once the feature is done you want it to be merged into the master branch. -
HEAD master Let’s automatize with Rebase foobar ➜ git merge
--no-ff foobar master -
HEAD master Let’s automatize with Rebase foobar ➜ git merge
--no-ff foobar master -
HEAD master Interactive Rebase ➜ git rebase -i master foobar
pick 1df2b98 My green commit pick 3b7aff0 My yellow commit pick 2edf2b8 My purple commit -
HEAD master Interactive Rebase ➜ git rebase -i master foobar
pick 1df2b98 My green commit pick 3b7aff0 My yellow commit pick 2edf2b8 My purple commit -
HEAD master Interactive Rebase ➜ git rebase -i master foobar
pick 1df2b98 My green commit pick 2edf2b8 My purple commit pick 3b7aff0 My yellow commit -
HEAD master Interactive Rebase ➜ git rebase -i master foobar
pick 1df2b98 My green commit squash 2edf2b8 My purple commit pick 3b7aff0 My yellow commit -
Interactive Rebase # 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 -
Updating your local copy of remote branches origin/master master origin/foobar
master foobar git fetch When the branch is updated on the server, you have to synchronize you local copy -
Updating your local copy of remote branches origin/master master origin/foobar
master foobar git fetch fetch will only update the origin/* branches -
master master origin/master git merge origin/master master Updating your local
branch fast-forward -
Shortcut: git pull git pull origin master == git fetch
origin ; git merge origin/master -
Protip Never use git pull, except for master branch* *because
you’re not supposed to have divergence with master -
origin/master Updating your local branch with divergence master master someone
have added commits into master -
origin/master Updating your local branch with divergence master master git
merge origin/master master -
origin/master Updating your local branch with divergence master master Way
to Merge-Hell (even with a single branch) -
origin/master Updating your local branch with divergence master master git
rebase origin/master -
origin/master Updating your local branch with divergence master master git
push origin master -
Never use git pull. Instead do git fetch + git
rebase or git pull --rebase -
Master Branch We create a new branch to start a
new feature Basic Git Workflow -
Master Branch Once the feature done, we merge it into
master Basic Git Workflow -
master master origin/master Basic Git Workflow ➜ git chekcout -b
dev ➜ git commit; git commit -
master master dev origin/master Basic Git Workflow dev origin/dev Meanwhile,
master have been updated… -
master master origin/master Basic Git Workflow dev origin/dev dev ➜
git rebase origin/master -
master master origin/master Basic Git Workflow dev origin/dev dev git
push origin dev Server will reject the push because of the divergence -
master master origin/master Basic Git Workflow dev origin/dev dev git
push -f origin dev Say the server to forget its old commits Use with caution if several people work on that branch -
master master origin/master Basic Git Workflow origin/dev dev git push
-f origin dev dev -
master master origin/master Basic Git Workflow origin/dev dev dev ➜
git merge --no-ff dev master -
master master origin/master Basic Git Workflow origin/dev dev Merged with
Stash (non fast-forward) -
master master origin/master Basic Git Workflow origin/dev dev Prune option
delete removed origin/* branches ➜ git fetch -p -
master master origin/master Basic Git Workflow dev ➜ git checkout
master ! ➜ git merge origin/master or ➜ git pull -
➜ git:(master) git pull # ensure your master is up
to date Updating 7afe47a..fb562e8 Fast-forward ➜ git:(master) git checkout -b feat-something-1234 # start new branch ➜ git:(feat-something-1234) vim files ; git add -p ; git commit # commit code Switched to a new branch 'feat-something-1234' ➜ git:(feat-something-1234) git push -u origin feat-something-1234 To ssh://git@server/path/to/project.git + fb562e8...4b60dee feat-something-1234 -> feat-something-1234 [feat-something-1234 4b60dee] Some commit message ➜ git:(feat-something-1234) git rebase origin/master # update your branch First, rewinding head to replay your work on top of it... Applying: Some commit message ➜ git:(feat-something-1234) git fetch -p # later, before merging ➜ git:(feat-something-1234) git push -f # push force To ssh://git@server/path/to/project.git + fb562e8...4b60dee feat-something-1234 -> feat-something-1234 (force update) ➜ git:(feat-something-1234) stash pull-request master # open a PR ➜ git:(feat-something-1234) meps deploy # deploy on maquette -
Push git push origin foobar:foobar Long command git push origin
:foobar Deleting a remote branch branch name on the remote local branch name into the remote branch push nothing -
Push git push -u origin foobar Track upstream branch (the
first time you push it) git push Use the shortcut the next time [push] # git < v2.0 # 'nothing' : Do not push anything # 'matching' : Push all matching branches (default) # 'upstream' : Push the current branch to whatever it is tracking # 'current' : Push the current branch default = upstream -
Push git push origin foobar:foobar Push into the branch with
the same branch by default git push [push] # git >= v2.0 default = simple -
Track untracked file git add -N . Useful because git
add patch only works on tracked file git add -p -
Rebase conflict HEAD master foobar Problem: sometimes it happens you
have to resolves several times the same conflict while rebasing Solution: squash all your commits before rebasing on master -
Rebase conflict HEAD master foobar git rebase -i $(git merge-base
master foobar) origin/master First rebase on yourself There won’t be any conflict here -
Rebase conflict HEAD master foobar origin/master pick 1df2b98 My green
commit squash 3b7aff0 My yellow commit squash 2edf2b8 My purple commit -
Rebase conflict master origin/master HEAD foobar git rebase origin/master Then
you can rebase on master -
Rebase conflict $ (vim the file.txt with the conflict) $
git add file.txt $ git rebase --continue To resolve a rebase conflict $ git rebase --abort If you fucked up -
Branch diff $ git diff origin/master...foobar Diff for a whole
branch $ git diff --name-status origin/master... Only display changed files $ git lg origin/master..foobar Log of the branch -
Git Stash git stash save "WIP something" Want to save
your change, but don’t want to commit? git stash pop git stash list Try not to have too many thing in your stash, otherwise you will forgot it git stash show -p stash@{0}