SlideShare a Scribd company logo
Git Tutorial –
Undoing Changes
Bryan Lin
2013/11/25
Agenda







Overview
git checkout
git revert
git reset
git clean
Overview


This tutorial provides all of the necessary
skills to work with previous revisions of a
software project. First, it shows you how
to explore old commits, then it explains
the difference between reverting public
commits in the project history vs.
resetting unpublished changes on your
local machine.
git checkout


Serves three distinct functions:
◦ checking out files
◦ checking out commits
◦ checking out branches
git checkout


Usage:
◦ git checkout master
 Return to the master branch

◦ git checkout <commit> <file>
 Check out a previous version of a file

◦ git checkout <commit>
 Update all files in the working directory to match
the specified commit
git checkout


Disscusion:
◦ git checkout is an easy way to ―load‖ any of
these saved snapshots onto your development
machine
◦ Checking out an old commit is a read-only
operation
git checkout


Disscusion:
◦ detached HEAD state:

 when you check out a previous commit, HEAD no
longer points to a branch—it points directly to a
commit
git checkout


Disscusion:
◦ On the other hand, checking out an old file
does affect the current state of your repository.
You can re-commit the old version in a new
snapshot as you would any other file
git checkout


Example:
◦ Viewing an Old Revision
 git log –oneline
 you can see the result like the following:
Checkout this
commit

This is
―commit‖
git checkout


Example:
◦ Viewing an Old Revision
 Use git checkout to view the ―Make some import
changes to hello.py‖
◦ git checkout a1e8fb5
 Get back to the ―current‖ state of your project
◦ git checkout master
git checkout


Example:
◦ Checking Out a File
 Only wanted to see the hello.py file from the old
commit
◦ git checkout a1e8fb5 hello.py
 Discard this change
◦ git checkout HEAD hello.py
git checkout


Exercise:
◦ Create a git repository, and add
1.txt, 2.txt, 3.txt for commit 1, b.txt for
commit 2

commit1

commit2
git checkout


Exercise:
◦
◦
◦
◦
◦
◦

checkout commit1
revert to master
checkout 2.txt of commit1
commit 2.txt
checkout 1.txt
discard the change of 1.txt
git revert
The git revert command undoes a
committed snapshot
 Appends a new commit with the resulting
content

git revert


Usage
◦ git revert <commit>
◦ Generate a new commit that undoes all of the
changes introduced in <commit>, then apply it
to the current branch
git revert


Discussion
◦ Reverting should be used when you want to
remove an entire commit from your project
history, such as fixing the bug of this
commit, then commit a new snapshot.
git revert


Discussion
◦ Reverting vs. Resetting
 It's important to understand that git
revert undoes a single commit—it does not
"revert" back to the previous state of a project by
removing all subsequent commits. In Git, this is
actually called a reset, not a revert
git revert


Discussion
◦ Reverting vs. Resetting
git revert


Example
◦ commits a snapshot, then immediately undoes
it with a revert
git revert


Example
◦ This can be visualized as the following:
git revert


Example
◦ commits a snapshot, then immediately undoes
it with a revert
git revert


Example
◦ Note that the 4th commit is still in the project
history after the revert. Instead of deleting
it, git revert added a new commit to undo its
changes. As a result, the 3rd and 5th commits
represent the exact same code base, and the
4th commit is still in our history just in case we
want to go back to it down the road
git reset


Discussion
◦ If git revert is a ―safe‖ way to undo
changes, you can think of git reset as
the dangerous method. When you undo
with git reset(and the commits are no longer
referenced by any ref or the reflog), there is no
way to retrieve the original copy—it is
a permanent undo
git reset


Usage
◦ git reset <file>
 Remove the specified file from the staging
area, but leave the working directory unchanged

◦ git reset
 Reset the staging area to match the most recent
commit, but leave the working directory
unchanged
git reset


Usage
◦ git reset –hard
 Reset the staging area and the working directory
to match the most recent commit

◦ git reset <commit>
 Move the current branch tip backward
to <commit>, reset the staging area to
match, but leave the working directory alone

◦ git reset --hard <commit>
git reset


Usage
◦ git reset --hard <commit>
 Move the current branch tip backward
to <commit> and reset both the staging area and
the working directory to match
git reset


Discussion
◦ Whereas reverting is designed to safely undo
a public commit, git reset is designed to undo
local changes
git reset


Don’t Reset Public History
◦ Removing a commit that other team members
have continued developing poses serious
problems for collaboration
git reset
git reset


Examples
◦ Unstaging a File
 Assumes you have two files
called hello.py and main.py that you’ve already
added to the repository
git reset


Examples
◦ Unstaging a File
git reset


Examples
◦ Removing Local Commits
git clean


The git clean command removes
untracked files from your working
directory
git clean


Usage
◦ git clean –n
 This will show you which files are going to be
removed without actually doing it

◦ git clean –f
 Remove untracked files from the current directory.
 The -f (force) flag is required unless
the clean.requireForce configuration option is set
to false (it's true by default).
 This will not remove untracked folders or files
specified by .gitignore.
git clean


Usage
◦ git clean -f <path>
 Remove untracked files, but limit the operation to
the specified path

◦ git clean –df
 Remove untracked files and untracked directories
from the current directory

◦ git clean –xf
 Remove untracked files from the current directory
as well as any files that Git usually ignores
git clean


Discussion
◦ The git reset --hard and git clean -f commands
are your best friends after you’ve made some
embarrassing developments in your local
repository and want to burn the evidence
◦ The git clean command can also be useful for
cleaning up the working directory after a build
git clean


Example
◦ Assumes you’ve already committed a few
snapshots and are experimenting with some
new developments
FAQ

More Related Content

PPTX
Techoalien git
PPTX
Git commands
PDF
A Simple Introduction to Git
PDF
Github git-cheat-sheet
DOCX
Git commands
PDF
Git for the absolute beginners
PPT
Git Cards - Keynote Format
PPT
Git Cards - Powerpoint Format
Techoalien git
Git commands
A Simple Introduction to Git
Github git-cheat-sheet
Git commands
Git for the absolute beginners
Git Cards - Keynote Format
Git Cards - Powerpoint Format

What's hot (19)

PPTX
Hacktoberfest intro to Git and GitHub
PDF
Git Developer Cheatsheet
PDF
Atlassian git cheatsheet
PPTX
Git undo
PDF
Advanced Git
DOCX
Git github
PDF
Git cheat sheet
DOCX
Git cheatsheet
ODP
Git presentation
PDF
Git cheat sheet
PDF
Git cheat sheet_dark
PDF
Git cheat sheet__white
PDF
Introduction to Git (part 2)
PPTX
Git for beginner
PPTX
Git basics
PDF
Git Tricks
KEY
Basic Git
PPTX
Linux GIT commands
PDF
Git 入门与实践
Hacktoberfest intro to Git and GitHub
Git Developer Cheatsheet
Atlassian git cheatsheet
Git undo
Advanced Git
Git github
Git cheat sheet
Git cheatsheet
Git presentation
Git cheat sheet
Git cheat sheet_dark
Git cheat sheet__white
Introduction to Git (part 2)
Git for beginner
Git basics
Git Tricks
Basic Git
Linux GIT commands
Git 入门与实践
Ad

Similar to Git tutorial undoing changes (20)

PPT
Learn Git Basics
PDF
Collaborative development with Git | Workshop
PPTX
Git hub abduallah abu nada
PPTX
Techoalien git
PPTX
Techoalien git
PDF
Git tutorial
PDF
How to git easily in day to-day work
PDF
Learning git
PPT
390a gitintro 12au
PDF
Git basics for beginners
PPTX
sample.pptx
PDF
Git with the flow
PPTX
Introduction to Git and Github
PDF
WPGR - Intro to Git (on the Command Line)
PPTX
Introduction To Git Workshop
PPTX
Introduction to git, a version control system
PDF
Intro to Git
PPT
Git basic
PDF
Git cheatsheet
PDF
Pro git - grasping it conceptually
Learn Git Basics
Collaborative development with Git | Workshop
Git hub abduallah abu nada
Techoalien git
Techoalien git
Git tutorial
How to git easily in day to-day work
Learning git
390a gitintro 12au
Git basics for beginners
sample.pptx
Git with the flow
Introduction to Git and Github
WPGR - Intro to Git (on the Command Line)
Introduction To Git Workshop
Introduction to git, a version control system
Intro to Git
Git basic
Git cheatsheet
Pro git - grasping it conceptually
Ad

More from LearningTech (20)

PPTX
PPTX
PostCss
PPTX
ReactJs
PPTX
Docker
PPTX
Semantic ui
PPTX
node.js errors
PPTX
Process control nodejs
PPTX
Expression tree
PPTX
SQL 效能調校
PPTX
flexbox report
PPTX
Vic weekly learning_20160504
PPTX
Reflection &amp; activator
PPTX
Peggy markdown
PPTX
Node child process
PPTX
20160415ken.lee
PPTX
Peggy elasticsearch應用
PPTX
Expression tree
PPTX
Vic weekly learning_20160325
PPTX
D3js learning tips
PPTX
git command
PostCss
ReactJs
Docker
Semantic ui
node.js errors
Process control nodejs
Expression tree
SQL 效能調校
flexbox report
Vic weekly learning_20160504
Reflection &amp; activator
Peggy markdown
Node child process
20160415ken.lee
Peggy elasticsearch應用
Expression tree
Vic weekly learning_20160325
D3js learning tips
git command

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Empathic Computing: Creating Shared Understanding
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Modernizing your data center with Dell and AMD
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PPT
Teaching material agriculture food technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Empathic Computing: Creating Shared Understanding
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
NewMind AI Monthly Chronicles - July 2025
The AUB Centre for AI in Media Proposal.docx

Git tutorial undoing changes

  • 1. Git Tutorial – Undoing Changes Bryan Lin 2013/11/25
  • 3. Overview  This tutorial provides all of the necessary skills to work with previous revisions of a software project. First, it shows you how to explore old commits, then it explains the difference between reverting public commits in the project history vs. resetting unpublished changes on your local machine.
  • 4. git checkout  Serves three distinct functions: ◦ checking out files ◦ checking out commits ◦ checking out branches
  • 5. git checkout  Usage: ◦ git checkout master  Return to the master branch ◦ git checkout <commit> <file>  Check out a previous version of a file ◦ git checkout <commit>  Update all files in the working directory to match the specified commit
  • 6. git checkout  Disscusion: ◦ git checkout is an easy way to ―load‖ any of these saved snapshots onto your development machine ◦ Checking out an old commit is a read-only operation
  • 7. git checkout  Disscusion: ◦ detached HEAD state:  when you check out a previous commit, HEAD no longer points to a branch—it points directly to a commit
  • 8. git checkout  Disscusion: ◦ On the other hand, checking out an old file does affect the current state of your repository. You can re-commit the old version in a new snapshot as you would any other file
  • 9. git checkout  Example: ◦ Viewing an Old Revision  git log –oneline  you can see the result like the following: Checkout this commit This is ―commit‖
  • 10. git checkout  Example: ◦ Viewing an Old Revision  Use git checkout to view the ―Make some import changes to hello.py‖ ◦ git checkout a1e8fb5  Get back to the ―current‖ state of your project ◦ git checkout master
  • 11. git checkout  Example: ◦ Checking Out a File  Only wanted to see the hello.py file from the old commit ◦ git checkout a1e8fb5 hello.py  Discard this change ◦ git checkout HEAD hello.py
  • 12. git checkout  Exercise: ◦ Create a git repository, and add 1.txt, 2.txt, 3.txt for commit 1, b.txt for commit 2 commit1 commit2
  • 13. git checkout  Exercise: ◦ ◦ ◦ ◦ ◦ ◦ checkout commit1 revert to master checkout 2.txt of commit1 commit 2.txt checkout 1.txt discard the change of 1.txt
  • 14. git revert The git revert command undoes a committed snapshot  Appends a new commit with the resulting content 
  • 15. git revert  Usage ◦ git revert <commit> ◦ Generate a new commit that undoes all of the changes introduced in <commit>, then apply it to the current branch
  • 16. git revert  Discussion ◦ Reverting should be used when you want to remove an entire commit from your project history, such as fixing the bug of this commit, then commit a new snapshot.
  • 17. git revert  Discussion ◦ Reverting vs. Resetting  It's important to understand that git revert undoes a single commit—it does not "revert" back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert
  • 19. git revert  Example ◦ commits a snapshot, then immediately undoes it with a revert
  • 20. git revert  Example ◦ This can be visualized as the following:
  • 21. git revert  Example ◦ commits a snapshot, then immediately undoes it with a revert
  • 22. git revert  Example ◦ Note that the 4th commit is still in the project history after the revert. Instead of deleting it, git revert added a new commit to undo its changes. As a result, the 3rd and 5th commits represent the exact same code base, and the 4th commit is still in our history just in case we want to go back to it down the road
  • 23. git reset  Discussion ◦ If git revert is a ―safe‖ way to undo changes, you can think of git reset as the dangerous method. When you undo with git reset(and the commits are no longer referenced by any ref or the reflog), there is no way to retrieve the original copy—it is a permanent undo
  • 24. git reset  Usage ◦ git reset <file>  Remove the specified file from the staging area, but leave the working directory unchanged ◦ git reset  Reset the staging area to match the most recent commit, but leave the working directory unchanged
  • 25. git reset  Usage ◦ git reset –hard  Reset the staging area and the working directory to match the most recent commit ◦ git reset <commit>  Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone ◦ git reset --hard <commit>
  • 26. git reset  Usage ◦ git reset --hard <commit>  Move the current branch tip backward to <commit> and reset both the staging area and the working directory to match
  • 27. git reset  Discussion ◦ Whereas reverting is designed to safely undo a public commit, git reset is designed to undo local changes
  • 28. git reset  Don’t Reset Public History ◦ Removing a commit that other team members have continued developing poses serious problems for collaboration
  • 30. git reset  Examples ◦ Unstaging a File  Assumes you have two files called hello.py and main.py that you’ve already added to the repository
  • 33. git clean  The git clean command removes untracked files from your working directory
  • 34. git clean  Usage ◦ git clean –n  This will show you which files are going to be removed without actually doing it ◦ git clean –f  Remove untracked files from the current directory.  The -f (force) flag is required unless the clean.requireForce configuration option is set to false (it's true by default).  This will not remove untracked folders or files specified by .gitignore.
  • 35. git clean  Usage ◦ git clean -f <path>  Remove untracked files, but limit the operation to the specified path ◦ git clean –df  Remove untracked files and untracked directories from the current directory ◦ git clean –xf  Remove untracked files from the current directory as well as any files that Git usually ignores
  • 36. git clean  Discussion ◦ The git reset --hard and git clean -f commands are your best friends after you’ve made some embarrassing developments in your local repository and want to burn the evidence ◦ The git clean command can also be useful for cleaning up the working directory after a build
  • 37. git clean  Example ◦ Assumes you’ve already committed a few snapshots and are experimenting with some new developments
  • 38. FAQ