You already know how to use “git status”, “git push”, and “git add” for your personal projects. You know how to work on a team project with git version control. How do you achieve the next level of git mastery and become as productive as possible? Mistakes happen very frequently when coding. Sometimes they can be terrifying mistakes. You’ll learn git commands to un-do your coding mistakes effortlessly. How do you re-order, edit, remove, and squash commits? You’ll understand the differences between all the "git r*” commands: "git rebase", "git revert", "git reset", and "git reflog" and know when and why to use them. We’ll cover how to set up your git environment for a productive workflow, different ways to undo your mistakes in git, and finally, different ways to automate an entire git workflow.
These are the slides for a talk that I gave at PyTN 2015 on a Sunday morning.
5. Git Configs: AliasesGit Configs: Aliases
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# _posts/2009-02-06-helpful-command-aliases.textile
nothing added to commit but untracked files present (use "git add" to track)
In ~/.gitconfig, put this here:
7. Git Configs:Git Configs:
Tab AutocompletionTab Autocompletion
1. Follow instructions for your OS.
2. Then you can use auto tab completion on long commands
here
$ git chec<tab>
8. Git Configs:Git Configs:
A Useful Bash PromptA Useful Bash Prompt
https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
A useful prompt shows
current branch
current directory
current virtualenv
status of working directory
9. Who is this talk for?
You already know the basics of git, but you still make
mistakes (well, so does everyone)
Common git commands you already know:
git add
git status
git push origin
git fetch origin
You want to know how to deal with mistakes
Common git commands you'll learn about:
git rebase
git reset
git reflog
git revert
10. This Should Look FamiliarThis Should Look Familiar
Source: http://git-scm.com/book/ch1-3.html
11. This should be familiar, tooThis should be familiar, too
12. This may be familiar, tooThis may be familiar, too
13. This should be familiar, too -- Collaboration with git.This should be familiar, too -- Collaboration with git.
Source: http://nvie.com/posts/a-successful-git-branching-model/
14. II. Mistakes and howII. Mistakes and how
to fix themto fix them
"People think computers will keep them from making mistakes.
They're wrong. With computers you make mistakes faster."
--Adam Osborn
16. git rebasegit rebase
Why use rebase?
1. git rebase to keep a branch updated
2. git rebase --interactive flag to change history
17. git rebase: The Simplest Usagegit rebase: The Simplest Usage
Note: "master" here means remote upstream's master branch.
18. git rebase: The Simplest Usagegit rebase: The Simplest Usage
19. What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a feature branch shared by your team members?against a feature branch shared by your team members?
Note: "master" here means remote upstream repo's master branch.
20. What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a feature branch shared by your team members?against a feature branch shared by your team members?
Note: "master" here means remote upstream repo's master branch.
21. git rebase master/feature develop
git rebase --onto master feature develop
Note: "master" here means remote upstream repo's master branch.
OR
git rebasegit rebase
git rebase <remote_repo>/<remote_branch> <your_branch_name>
git rebase --onto <first_this> <then_this> <last>
Syntax for git rebase
22. What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?
Note: "master" here means remote upstream repo's master branch.
23. What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?
Note: "master" here means remote upstream repo's master branch.
25. Rebase Conflicts require manual fixesRebase Conflicts require manual fixes
(because git isn't clever enough to fix conflicts)
1. Locate conflict markers (<<<<<<) and
make edits to resolve
2. Resolve a conflict in each file with git
add <filename>
3. git rebase --continue
4. Alternatively, you can undo the git
rebase with git rebase --abort
26. Fixing git rebase conflicts is notFixing git rebase conflicts is not
always the right solutionalways the right solution
git checkout master
git branch -D feature
git fetch master
git checkout -b feature master/feature
git checkout -b develop_v2
git merge develop_v1
git branch -D develop_v1
git fetch master
git checkout [YOUR BRANCH]
git rebase --onto master/feature [commit]
OR
27. Different computers, sameDifferent computers, same
branchbranch
git fetch origin && git rebase origin/<MY_BRANCH>
Note: "origin" is your personal cloned copy of an upstream master
repo
git pull --rebase
Or, equivalently
28.
29. git rebase --interactive <HASH>
or
git rebase --interactive HEAD^
Defn: To re-order, edit, squash, or remove many
commits at once
33. git reset --soft HEAD^
Defn: Set files from previous commit onto staging area. HEAD
points to the previous commit. This leaves all your changed files
as "Changes to be committed".
35. git reset --hard HEAD^
Defn: Abandon everything since your last commit. HEAD points
to the previous commit. Nothing is in staging. Use with great
caution
36. git refloggit reflog
Defn: shows all the commits and actions on your machine that has
ever happened such as branch switches, rebases, resets, branch
deletions, cherry-picks, reverts.
37. git reflog git reflog --grep="Revert"
How to make git reflog more usefulHow to make git reflog more useful
git reflog --all --date=iso
38. You can undo a hard reset with the help of git reflogYou can undo a hard reset with the help of git reflog
git reset --hard HEAD^^ to remove the last 3 commits
39. You can undo a hard reset with git reflog + git reset --hardYou can undo a hard reset with git reflog + git reset --hard
git reset --hard a64b0f2 to restore branch to previous stategit reset --hard a64b0f2 to restore branch to previous state
40. Why use git reflog?Why use git reflog?
You lost commits when you did a git reset --hard
You squashed too many commits into 1 commit, so you
want to undo git rebase -i
You accidentally deleted branches and want to restore
them
Summary: Use reflog to look up old lost commits
41. Use cases for git reflogUse cases for git reflog
Look up a commit hash. Then either
create a new (un-named) branch out of this commit
git checkout -b <COMMIT_HASH>
reset your branch to a previous state
git reset --hard <COMMIT_HASH>
git reset --soft <COMMIT_HASH>
cherrypick that lost commit on top of your branch
git cherry-pick <COMMIT_HASH>
42. How long does theHow long does the
output of reflog last?output of reflog last?
Hanging commits are removed from the local repository by
garbage collection (gc) or by manual removal. Default is 30
days.
[gc]
reflogExpire = never
reflogExpireUnreachable = never
44. Defn: Revert changes specified by 4th last commit away
from HEAD and create a new commit with reverted changes.
git revert HEAD~3
45. git revert --continue
Continue after resolving conflicts
git revert --abort
Cancel operation and return to pre-sequence state
git revert --quit
Can be used to start over after a failed cherry-pick or revert
Git revert conflictsGit revert conflicts
46. When to use git reset vs. git revertWhen to use git reset vs. git revert
54. Problem: Fetch & rebase repetitionsProblem: Fetch & rebase repetitions
cd <PROJECT>
git fetch upstream
git rebase upstream/master
cd ../<ANOTHER_PROJECT>
git fetch upstream
git rebase upstream/master
# And again
....