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
Ad

More Related Content

What's hot (19)

Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
DSC GVP
 
Git Developer Cheatsheet
Git Developer CheatsheetGit Developer Cheatsheet
Git Developer Cheatsheet
Abdul Basit
 
Atlassian git cheatsheet
Atlassian git cheatsheetAtlassian git cheatsheet
Atlassian git cheatsheet
Abdul Basit
 
Git undo
Git undoGit undo
Git undo
Avilay Parekh
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
Sergiu-Ioan Ungur
 
Git github
Git githubGit github
Git github
Anurag Deb
 
Git cheat sheet
Git cheat sheetGit cheat sheet
Git cheat sheet
Lam Hoang
 
Git cheatsheet
Git cheatsheetGit cheatsheet
Git cheatsheet
synapsefre
 
Git presentation
Git presentationGit presentation
Git presentation
Vikas Yaligar
 
Git cheat sheet
Git cheat sheetGit cheat sheet
Git cheat sheet
Piyush Mittal
 
Git cheat sheet_dark
Git cheat sheet_darkGit cheat sheet_dark
Git cheat sheet_dark
King Hom
 
Git cheat sheet__white
Git cheat sheet__whiteGit cheat sheet__white
Git cheat sheet__white
King Hom
 
Introduction to Git (part 2)
Introduction to Git (part 2)Introduction to Git (part 2)
Introduction to Git (part 2)
Salvatore Cordiano
 
Git for beginner
Git for beginnerGit for beginner
Git for beginner
Trung Huynh
 
Git basics
Git basicsGit basics
Git basics
Denys Haryachyy
 
Git Tricks
Git TricksGit Tricks
Git Tricks
Ivelina Dimova
 
Basic Git
Basic GitBasic Git
Basic Git
Knut Haugen
 
Linux GIT commands
Linux GIT commandsLinux GIT commands
Linux GIT commands
RajKumar Rampelli
 
Git 入门与实践
Git 入门与实践Git 入门与实践
Git 入门与实践
Terry Wang
 

Similar to Git tutorial undoing changes (20)

Learn Git Basics
Learn Git BasicsLearn Git Basics
Learn Git Basics
Prakash Dantuluri
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
Anuchit Chalothorn
 
Git hub abduallah abu nada
Git hub   abduallah abu nadaGit hub   abduallah abu nada
Git hub abduallah abu nada
Lama K Banna
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
Aditya Tiwari
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
Aditya Tiwari
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
mobaires
 
How to git easily in day to-day work
How to git easily in day to-day workHow to git easily in day to-day work
How to git easily in day to-day work
Alena Radkevich
 
Learning git
Learning gitLearning git
Learning git
Sid Anand
 
390a gitintro 12au
390a gitintro 12au390a gitintro 12au
390a gitintro 12au
Nguyen Van Hung
 
Git basics for beginners
Git basics for beginnersGit basics for beginners
Git basics for beginners
PravallikaTammisetty
 
sample.pptx
sample.pptxsample.pptx
sample.pptx
UshaSuray
 
Git with the flow
Git with the flowGit with the flow
Git with the flow
Dana White
 
Introduction to Git and Github
Introduction to Git and Github Introduction to Git and Github
Introduction to Git and Github
Max Claus Nunes
 
WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)
Brian Richards
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
themystic_ca
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control system
Kumaresh Chandra Baruri
 
Intro to Git
Intro to GitIntro to Git
Intro to Git
Shadab Khan
 
Git basic
Git basicGit basic
Git basic
Emran Ul Hadi
 
Git cheatsheet
Git cheatsheetGit cheatsheet
Git cheatsheet
Weghlis Azzariou
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
seungzzang Kim
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
Anuchit Chalothorn
 
Git hub abduallah abu nada
Git hub   abduallah abu nadaGit hub   abduallah abu nada
Git hub abduallah abu nada
Lama K Banna
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
mobaires
 
How to git easily in day to-day work
How to git easily in day to-day workHow to git easily in day to-day work
How to git easily in day to-day work
Alena Radkevich
 
Learning git
Learning gitLearning git
Learning git
Sid Anand
 
Git with the flow
Git with the flowGit with the flow
Git with the flow
Dana White
 
Introduction to Git and Github
Introduction to Git and Github Introduction to Git and Github
Introduction to Git and Github
Max Claus Nunes
 
WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)
Brian Richards
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
themystic_ca
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control system
Kumaresh Chandra Baruri
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
seungzzang Kim
 
Ad

More from LearningTech (20)

vim
vimvim
vim
LearningTech
 
PostCss
PostCssPostCss
PostCss
LearningTech
 
ReactJs
ReactJsReactJs
ReactJs
LearningTech
 
Docker
DockerDocker
Docker
LearningTech
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
LearningTech
 
node.js errors
node.js errorsnode.js errors
node.js errors
LearningTech
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
LearningTech
 
Expression tree
Expression treeExpression tree
Expression tree
LearningTech
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
LearningTech
 
flexbox report
flexbox reportflexbox report
flexbox report
LearningTech
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
LearningTech
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
LearningTech
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
LearningTech
 
Node child process
Node child processNode child process
Node child process
LearningTech
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
LearningTech
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
LearningTech
 
Expression tree
Expression treeExpression tree
Expression tree
LearningTech
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
LearningTech
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
LearningTech
 
git command
git commandgit command
git command
LearningTech
 
Ad

Recently uploaded (20)

Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 

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
  翻译: