Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
How to Really GetHow to Really Get
GitGit
Sunday, February 8, 2015
Susan Tan
@ArcTanSusan
Who am I?
Software Engineer @Piston who uses Python and git
A New-Yorker-who-moved-to-San-Francisco
Also Piston is .hiring
I. Git SetupI. Git Setup
Git Configs: AliasesGit Configs: Aliases
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
$...
Git Configs: ColorsGit Configs: Colors
[color "status"]
added = magenta
changed = yellow
untracked = cyan
In ~/.gitconfig, ...
Git Configs:Git Configs:
Tab AutocompletionTab Autocompletion
1. Follow instructions for your OS.
2. Then you can use auto...
Git Configs:Git Configs:
A Useful Bash PromptA Useful Bash Prompt
https://github.com/bobthecow/git-flow-completion/wiki/Ins...
Who is this talk for?
You already know the basics of git, but you still make
mistakes (well, so does everyone)
Common git ...
This Should Look FamiliarThis Should Look Familiar
Source: http://git-scm.com/book/ch1-3.html
This should be familiar, tooThis should be familiar, too
This may be familiar, tooThis may be familiar, too
This should be familiar, too -- Collaboration with git.This should be familiar, too -- Collaboration with git.
Source: htt...
II. Mistakes and howII. Mistakes and how
to fix themto fix them
"People think computers will keep them from making mistake...
Source: http://justinhileman.info/article/git-pretty/git-pretty.pdf
git rebasegit rebase
Why use rebase?
1. git rebase to keep a branch updated
2. git rebase --interactive flag to change hist...
git rebase: The Simplest Usagegit rebase: The Simplest Usage
Note: "master" here means remote upstream's master branch.
git rebase: The Simplest Usagegit rebase: The Simplest Usage
What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a fea...
What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a fea...
git rebase master/feature develop
git rebase --onto master feature develop
Note: "master" here means remote upstream repo'...
What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's m...
What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's m...
Rebase ConflictsRebase Conflicts
Rebase Conflicts require manual fixesRebase Conflicts require manual fixes
(because git isn't clever enough to fix conflicts...
Fixing git rebase conflicts is notFixing git rebase conflicts is not
always the right solutionalways the right solution
gi...
Different computers, sameDifferent computers, same
branchbranch
git fetch origin && git rebase origin/<MY_BRANCH>
Note: "o...
git rebase --interactive <HASH>
or
git rebase --interactive HEAD^
Defn: To re-order, edit, squash, or remove many
commits ...
git rebase -i HEAD~5
Squashing commitsSquashing commits
git resetgit reset
Defn: Reset current HEAD to the specified state
git reset --soft HEAD^
Defn: Set files from previous commit onto staging area. HEAD
points to the previous commit. This lea...
git reset --soft HEAD^
git reset --hard HEAD^
Defn: Abandon everything since your last commit. HEAD points
to the previous commit. Nothing is in ...
git refloggit reflog
Defn: shows all the commits and actions on your machine that has
ever happened such as branch switche...
git reflog git reflog --grep="Revert"
How to make git reflog more usefulHow to make git reflog more useful
git reflog --al...
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 --har...
You can undo a hard reset with git reflog + git reset --hardYou can undo a hard reset with git reflog + git reset --hard
g...
Why use git reflog?Why use git reflog?
You lost commits when you did a git reset --hard
You squashed too many commits into...
Use cases for git reflogUse cases for git reflog
Look up a commit hash. Then either
create a new (un-named) branch out of ...
How long does theHow long does the
output of reflog last?output of reflog last?
Hanging commits are removed from the local...
git revert 638351801cd802d6e0c4e601db1eca801dd58936
Defn: Makes a revert commit with a message that states the
specified co...
Defn: Revert changes specified by 4th last commit away
from HEAD and create a new commit with reverted changes.
git revert ...
git revert --continue
Continue after resolving conflicts
git revert --abort
Cancel operation and return to pre-sequence sta...
When to use git reset vs. git revertWhen to use git reset vs. git revert
Permanent MistakesPermanent Mistakes
Permanent MistakesPermanent Mistakes
git push -f origin <REMOTE>
SummarySummary
If you run into git problems, the solution is most likely to
use one of these --
git rebase
git reset
git r...
III. Automate gitIII. Automate git
Problem: Repetitive branch deletingProblem: Repetitive branch deleting
vagrant@precise64:~/work/savage$ git push origin :7...
IPython notebook supports gitIPython notebook supports git
Solution: IPython notebook scriptSolution: IPython notebook script
Problem: Fetch & rebase repetitionsProblem: Fetch & rebase repetitions
cd <PROJECT>
git fetch upstream
git rebase upstream...
Solution: IPython notebook scriptSolution: IPython notebook script
list_of_projects = ['ipython', 'oh-mainline']
def updat...
Solution: IPython notebook scriptSolution: IPython notebook script
Thanks! Q&A Time.Thanks! Q&A Time.
How to Really Get Git
How to Really Get Git
Upcoming SlideShare
Loading in …5
×

How to Really Get Git

1,399 views

Published on

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.

Published in: Technology
  • Be the first to comment

How to Really Get Git

  1. 1. How to Really GetHow to Really Get GitGit Sunday, February 8, 2015 Susan Tan @ArcTanSusan
  2. 2. Who am I? Software Engineer @Piston who uses Python and git A New-Yorker-who-moved-to-San-Francisco Also Piston is .hiring
  3. 3. I. Git SetupI. Git Setup
  4. 4. 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:
  5. 5. Git Configs: ColorsGit Configs: Colors [color "status"] added = magenta changed = yellow untracked = cyan In ~/.gitconfig, put this:
  6. 6. 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>
  7. 7. 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
  8. 8. 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
  9. 9. This Should Look FamiliarThis Should Look Familiar Source: http://git-scm.com/book/ch1-3.html
  10. 10. This should be familiar, tooThis should be familiar, too
  11. 11. This may be familiar, tooThis may be familiar, too
  12. 12. 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/
  13. 13. 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
  14. 14. Source: http://justinhileman.info/article/git-pretty/git-pretty.pdf
  15. 15. git rebasegit rebase Why use rebase? 1. git rebase to keep a branch updated 2. git rebase --interactive flag to change history
  16. 16. git rebase: The Simplest Usagegit rebase: The Simplest Usage Note: "master" here means remote upstream's master branch.
  17. 17. git rebase: The Simplest Usagegit rebase: The Simplest Usage
  18. 18. 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.
  19. 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. 20. 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
  21. 21. 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.
  22. 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. 23. Rebase ConflictsRebase Conflicts
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. git rebase --interactive <HASH> or git rebase --interactive HEAD^ Defn: To re-order, edit, squash, or remove many commits at once
  28. 28. git rebase -i HEAD~5
  29. 29. Squashing commitsSquashing commits
  30. 30. git resetgit reset Defn: Reset current HEAD to the specified state
  31. 31. 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".
  32. 32. git reset --soft HEAD^
  33. 33. 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
  34. 34. 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.
  35. 35. git reflog git reflog --grep="Revert" How to make git reflog more usefulHow to make git reflog more useful git reflog --all --date=iso
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. 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>
  40. 40. 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
  41. 41. git revert 638351801cd802d6e0c4e601db1eca801dd58936 Defn: Makes a revert commit with a message that states the specified commit is reverted git revert <HASH>
  42. 42. Defn: Revert changes specified by 4th last commit away from HEAD and create a new commit with reverted changes. git revert HEAD~3
  43. 43. 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
  44. 44. When to use git reset vs. git revertWhen to use git reset vs. git revert
  45. 45. Permanent MistakesPermanent Mistakes
  46. 46. Permanent MistakesPermanent Mistakes git push -f origin <REMOTE>
  47. 47. SummarySummary If you run into git problems, the solution is most likely to use one of these -- git rebase git reset git reflog git revert
  48. 48. III. Automate gitIII. Automate git
  49. 49. Problem: Repetitive branch deletingProblem: Repetitive branch deleting vagrant@precise64:~/work/savage$ git push origin :73997184 To git@github.com:onceuponatimeforever/savage.git - [deleted] 73997184 vagrant@precise64:~/work/savage$ git push origin :76321732-on-mast To git@github.com:onceuponatimeforever/savage.git - [deleted] 76321732-on-master vagrant@precise64:~/work/savage$ git push origin :77910316 To git@github.com:onceuponatimeforever/savage.git - [deleted] 77910316 vagrant@precise64:~/work/savage$ git push origin :77910316-bottle To git@github.com:onceuponatimeforever/savage.git - [deleted] 77910316-bottle vagrant@precise64:~/work/savage$ git push origin :77910316-bottle- To git@github.com:onceuponatimeforever/savage.git - [deleted] 77910316-bottle-from-scratch vagrant@precise64:~/work/savage$ git push origin :2.0/master
  50. 50. IPython notebook supports gitIPython notebook supports git
  51. 51. Solution: IPython notebook scriptSolution: IPython notebook script
  52. 52. 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 ....
  53. 53. Solution: IPython notebook scriptSolution: IPython notebook script list_of_projects = ['ipython', 'oh-mainline'] def update(project): %cd {project} !git checkout master !git fetch upstream !git rebase upstream/master !git push origin master %cd .. print "Done git rebasing this repoo: ", {project} for project in list_of_projects: update(list_of_projects[project]) print "---------------------------------"
  54. 54. Solution: IPython notebook scriptSolution: IPython notebook script
  55. 55. Thanks! Q&A Time.Thanks! Q&A Time.

×