Avilay Parekh (@avilay)
CTO, Co-Founder of Informion, Inc.
January 2014
Learn how to use git-checkout, git-reset,
git-revert, and git-clean
Walk through typical undo scenarios using
these comman...
Kevin Skoglund’s excellent git tutorial on
Lynda.com
Pro Git Book
(http://www.amazon.com/Pro-Experts-Voice-Software-
Devel...
You know the 3-tree architecture used in git.
You are familiar with the terms repo, staged
index, working directory, and H...
git checkout [changeset] -- <file>
git reset [option] [changeset]
git revert <changeset>
git clean [-n] [-f]
git checkout [changeset] -- <file>
git reset [option] [changeset]
git revert <changeset>
git clean [-n] [-f]
Can be used i...
git checkout [changeset] -- <file>
git reset [option] [changeset]
git revert <changeset>
git clean [-n] [-f]
Does a single...
A way to undo any uncommitted changes in the
staging index or the working directory
repo is left untouched
Undo un-staged changes to a particular file in my
working directory.
1
repo
Staging
index
Working
directory
git checkout -...
Initial State
The repo has 3 commits.
Staging index has some
uncommitted changes.
hello.py in the working
directory has so...
Git Checkout --
The two dashes -- are telling git not to
switch branches; something git
checkout command will do otherwise...
Final State
Staging index remains
unchanged.
hello.py in the working
directory is overwritten by
the version in the stagin...
With a bunch of files in staged or unstaged
states, undo changes to a particular file and
revert it to a version 2 commits...
Initial State
The repo has 3 commits.
There are a bunch of
staged, but uncommitted
files shown in green.
There are a bunch...
Git Checkout --
Note, the changeset fff999 is
parent of HEAD.
We are telling git to undo all
current changes to a single f...
Final State
First hello.py in the staging
index is overwritten by the
version in commit fff999.
Then hello.py in the worki...
A way to move the HEAD pointer in the repo.
Staging index and working directory can also be
changed by using the right opt...
Undo the last commit in the repo, but keep all the
uncommitted and unstaged changes intact
3
repo
Staging
index
Working
di...
Initial State
The repo has 3 commits.
Staging index has some
uncommitted changes.
hello.py in the working
directory has so...
git reset --soft
We are telling git to move the HEAD
pointer to the specified commit ID
which is the parent of the current...
Final State
HEAD pointer moves one
commit up. Any new
commits will be appended
to fff999 effectively losing
5d6ef1.
Stagin...
Undo the last commit in the repo, discard all
staged changes, but keep the working directory
intact
4
repo
Staging
index
W...
Initial State
The repo has 3 commits.
Staging index has some
uncommitted changes.
hello.py in the working
directory has so...
git reset --mixed
We are telling git to move the HEAD
pointer to the specified commit ID
which is the parent of the curren...
Final State
HEAD pointer moves one
commit up. Any new
commits will be appended
to fff999 effectively losing
5d6ef1.
Stagin...
Undo the last commit in the repo, discard all
staged and unstaged changes
5
repo
Staging
index
Working
directory
git reset...
Initial State
The repo has 3 commits.
Staging index has some
uncommitted changes.
Working directory has
some unstaged chan...
git reset --hard
We are telling git to move the HEAD
pointer to the specified commit ID
which is the parent of the current...
Final State
HEAD pointer moves one
commit up. Any new
commits will be appended
to fff999 effectively losing
5d6ef1.
All ch...
How can you undo all uncommitted changes (i.e.,
staged and unstaged changes) and go back to what
was last committed?
 git...
How can you undo all uncommitted changes (i.e.,
staged and unstaged changes) and go back to what
was last committed?
 git...
A way to discard changes made in a specific
commit
Always affects all 3 trees
Working directory has to be clean before a r...
Undo the last commit
6
repo
Staging
index
Working
directory
git revert
repo
Staging
index
Working
directory
Initial State ...
Initial State
The repo has 3 commits.
Working directory is clean.
6
a1b2c3 fff999 5d6ef1
HEAD
repo
staging index working
d...
git revert
We are telling git to discard changes in
the specified commit, in this case
HEAD, which is the last commit
6
gi...
Final State
What really happens is that
a new commit is made which
has the inverse of all
changes that were in 5d6ef1,
eff...
The “only” way to discard untracked changes, i.e,
new files that have not been added yet
These files can always just be rm...
Upcoming SlideShare
Loading in …5
×

Git undo

666 views

Published on

A detailed look at how to undo changes in git using git-checkout, git-reset, git-revert, and git-clean

Published in: Technology, Business
  • Be the first to comment

Git undo

  1. 1. Avilay Parekh (@avilay) CTO, Co-Founder of Informion, Inc. January 2014
  2. 2. Learn how to use git-checkout, git-reset, git-revert, and git-clean Walk through typical undo scenarios using these commands
  3. 3. Kevin Skoglund’s excellent git tutorial on Lynda.com Pro Git Book (http://www.amazon.com/Pro-Experts-Voice-Software- Development/dp/1430218339/)
  4. 4. You know the 3-tree architecture used in git. You are familiar with the terms repo, staged index, working directory, and HEAD. You are familiar with basic git commands like git add, git status, git log, git commit.
  5. 5. git checkout [changeset] -- <file> git reset [option] [changeset] git revert <changeset> git clean [-n] [-f]
  6. 6. git checkout [changeset] -- <file> git reset [option] [changeset] git revert <changeset> git clean [-n] [-f] Can be used in a number of different scenarios A bit difficult to understand at a conceptual level
  7. 7. git checkout [changeset] -- <file> git reset [option] [changeset] git revert <changeset> git clean [-n] [-f] Does a single task really well Easy to conceptually understand what is going on
  8. 8. A way to undo any uncommitted changes in the staging index or the working directory repo is left untouched
  9. 9. Undo un-staged changes to a particular file in my working directory. 1 repo Staging index Working directory git checkout -- repo Staging index Working directory Initial State Final State
  10. 10. Initial State The repo has 3 commits. Staging index has some uncommitted changes. hello.py in the working directory has some changes that have not been staged yet. 1 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git checkout -- hello.py has un-added changes hello.py has some uncommitted changes
  11. 11. Git Checkout -- The two dashes -- are telling git not to switch branches; something git checkout command will do otherwise. We are telling git to undo all current changes to hello.py and go back to the version in the staging index. To undo changes to all files in the working directory say – git checkout -- . 1 git checkout -- hello.py git checkout --
  12. 12. Final State Staging index remains unchanged. hello.py in the working directory is overwritten by the version in the staging index. 1 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git checkout -- hello.py is same as staging index No changes in the staging index
  13. 13. With a bunch of files in staged or unstaged states, undo changes to a particular file and revert it to a version 2 commits ago. 2 repo Staging index Working directory git checkout -- repo Staging index Working directory Initial State Final State
  14. 14. Initial State The repo has 3 commits. There are a bunch of staged, but uncommitted files shown in green. There are a bunch of unstaged changes to files in the working directory shown in red. 2 git checkout -- a1b2c3 fff999 5d6ef1 HEAD repo Added (but uncommitted changes) New changes not added to stage staging index working directory
  15. 15. Git Checkout -- Note, the changeset fff999 is parent of HEAD. We are telling git to undo all current changes to a single file “hello.py” and go back to the version in the changeset fff999. Unlike Scenario 1, the staging index will also change 2 git checkout -- git checkout fff999 -- hello.py
  16. 16. Final State First hello.py in the staging index is overwritten by the version in commit fff999. Then hello.py in the working directory is overwritten by the version in the staging index. All other staged and unstaged files are left alone. 2 git checkout -- a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory hello.py is changed to the version 2 commits ago hello.py is changed to the staging version
  17. 17. A way to move the HEAD pointer in the repo. Staging index and working directory can also be changed by using the right options
  18. 18. Undo the last commit in the repo, but keep all the uncommitted and unstaged changes intact 3 repo Staging index Working directory git reset --soft repo Staging index Working directory Initial State Final State
  19. 19. Initial State The repo has 3 commits. Staging index has some uncommitted changes. hello.py in the working directory has some unstaged changes. 3 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git reset --soft hello.py has un-added changes hello.py has some uncommitted changes
  20. 20. git reset --soft We are telling git to move the HEAD pointer to the specified commit ID which is the parent of the current HEAD The soft switch is telling git not to touch the working directory or the staging index 3 git reset --soft ff999 git reset --soft
  21. 21. Final State HEAD pointer moves one commit up. Any new commits will be appended to fff999 effectively losing 5d6ef1. Staging index and working directory remain untouched. 3 a1b2c3 fff999 repo staging index working directory git reset --soft No changes to the working directory No changes in the staging index HEAD
  22. 22. Undo the last commit in the repo, discard all staged changes, but keep the working directory intact 4 repo Staging index Working directory git reset --mixed repo Staging index Working directory Initial State Final State
  23. 23. Initial State The repo has 3 commits. Staging index has some uncommitted changes. hello.py in the working directory has some unstaged changes. 4 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git reset --mixed hello.py has un-added changes hello.py has some uncommitted changes
  24. 24. git reset --mixed We are telling git to move the HEAD pointer to the specified commit ID which is the parent of the current HEAD The mixed switch is telling git to make the staging index look like HEAD after it has been moved Mixed is the default switch if none is provided 4 git reset --mixed ff999 git reset --mixed
  25. 25. Final State HEAD pointer moves one commit up. Any new commits will be appended to fff999 effectively losing 5d6ef1. Staging index looks like fff999 Working directory is left unchanged 4 a1b2c3 fff999 repo staging index working directory git reset --mixed No changes to the working directory HEAD
  26. 26. Undo the last commit in the repo, discard all staged and unstaged changes 5 repo Staging index Working directory git reset --hard repo Staging index Working directory Initial State Final State
  27. 27. Initial State The repo has 3 commits. Staging index has some uncommitted changes. Working directory has some unstaged changes. 5 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git reset --hard hello.py has un-added changes hello.py has some uncommitted changes
  28. 28. git reset --hard We are telling git to move the HEAD pointer to the specified commit ID which is the parent of the current HEAD The hard switch is telling git to make the staging index look like HEAD after it has been moved, and make the working directory look like staging after it has been changed Effectively losing all work after fff999! 5 git reset --hard ff999 git reset --hard
  29. 29. Final State HEAD pointer moves one commit up. Any new commits will be appended to fff999 effectively losing 5d6ef1. All changes to the staging index and the working directory after fff999 are lost. 5 a1b2c3 fff999 repo staging index working directory git reset --hard HEAD
  30. 30. How can you undo all uncommitted changes (i.e., staged and unstaged changes) and go back to what was last committed?  git checkout HEAD -- .  git reset --hard HEAD
  31. 31. How can you undo all uncommitted changes (i.e., staged and unstaged changes) and go back to what was last committed?  git checkout HEAD -- .  git reset --hard HEAD
  32. 32. A way to discard changes made in a specific commit Always affects all 3 trees Working directory has to be clean before a revert
  33. 33. Undo the last commit 6 repo Staging index Working directory git revert repo Staging index Working directory Initial State Final State
  34. 34. Initial State The repo has 3 commits. Working directory is clean. 6 a1b2c3 fff999 5d6ef1 HEAD repo staging index working directory git revert
  35. 35. git revert We are telling git to discard changes in the specified commit, in this case HEAD, which is the last commit 6 git revert HEAD git reset --hard
  36. 36. Final State What really happens is that a new commit is made which has the inverse of all changes that were in 5d6ef1, effectively making the project look like fff999 In theory, reverting 333aaa will “redo” the previous “undo” 6 staging index working directory git reset --hard a1b2c3 fff999 5d6ef1 HEADrepo 333aaa
  37. 37. The “only” way to discard untracked changes, i.e, new files that have not been added yet These files can always just be rm’ed

×