How To: Cancel changes
related commands:
checkout reset status
In this post, you will learn how to cancel files modifications, before or after staging them.
After staging
Current repository state:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_1>
modified: <file_2>
modified: <file_3>
Did you noticed? status
output tell us how to do and we’ll follow the tip:
$ git reset HEAD <file_1>
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_2>
modified: <file_3>
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
we also can do:
$ git reset HEAD <file_1> <file_2>
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_3>
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
modified: <file_2>
or, obviously:
$ git reset HEAD
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
modified: <file_2>
modified: <file_3>
Before staging
… or after removed from index
This time, the repository state is:
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
modified: <file_2>
modified: <file_3>
Again, let’s use the tip:
$ git checkout <file_1>
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_2>
modified: <file_3>
for multiple files:
$ git checkout <file_1> <file_2>
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_3>
or for all files:
$ git checkout master
$ git status
On branch master
nothing to commit (working directory clean)
It’s wierd but also obvious to use checkout
: somewhere, undo changes is a “navigation” to the last commit.
Reminder
checkout
Go to a previous commit
$ git checkout <hash>
Go to a tag
$ git checkout <tag>
Go back to last commit
$ git checkout # or $ git checkout master
Go back to
HEAD
for a file$ git checkout <file>
reset
Unstage a file
$ git reset HEAD <file>
Cleanup the stage
$ git reset HEAD
status
Shows one to four usefull informations:
. the working branch
. the staged changed files if any
. the unstaged changed files if any
. the untracked files if any$ git status