âčïž Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things |
| Last Crawled | 2026-04-06 11:50:29 (4 hours ago) |
| First Indexed | 2014-10-28 00:37:13 (11 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Git - Undoing Things |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | At any stage, you may want to undo something.
Here, weâll review a few basic tools for undoing changes that youâve made.
Be careful, because you canât always undo some of these undos.
This is one of the few areas in Git where you may lose some work if you do it wrong.
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message.
If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the
--amend
option:
$ git commit --amend
This command takes your staging area and uses it for the commit.
If youâve made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same, and all youâll change is your commit message.
The same commit-message editor fires up, but it already contains the message of your previous commit.
You can edit the message the same as always, but it overwrites your previous commit.
As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
You end up with a single commitâââthe second commit replaces the results of the first.
Note
Itâs important to understand that when youâre amending your last commit, youâre not so much fixing it as
replacing
it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place.
Effectively, itâs as if the previous commit never happened, and it wonât show up in your repository history.
The obvious value to amending commits is to make minor improvements to your last commit, without cluttering your repository history with commit messages of the form, âOops, forgot to add a fileâ or âDarn, fixing a typo in last commitâ.
Note
Only amend commits that are still local and have not been pushed somewhere.
Amending previously pushed commits and force pushing the branch will cause problems for your collaborators.
For more on what happens when you do this and how to recover if youâre on the receiving end read
The Perils of Rebasing
.
Unstaging a Staged File
The next two sections demonstrate how to work with your staging area and working directory changes.
The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them.
For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type
git add *
and stage them both.
How can you unstage one of the two?
The
git status
command reminds you:
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
Right below the âChanges to be committedâ text, it says use
git reset HEAD <file>âŠâ
to unstage.
So, letâs use that advice to unstage the
CONTRIBUTING.md
file:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
The command is a bit strange, but it works.
The
CONTRIBUTING.md
file is modified but once again unstaged.
Note
Itâs true that
git reset
can be a dangerous command, especially if you provide the
--hard
flag.
However, in the scenario described above, the file in your working directory is not touched, so itâs relatively safe.
For now this magic invocation is all you need to know about the
git reset
command.
Weâll go into much more detail about what
reset
does and how to master it to do really interesting things in
Reset Demystified
.
Unmodifying a Modified File
What if you realize that you donât want to keep your changes to the
CONTRIBUTING.md
file?
How can you easily unmodify itââârevert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)?
Luckily,
git status
tells you how to do that, too.
In the last example output, the unstaged area looks like this:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
It tells you pretty explicitly how to discard the changes youâve made.
Letâs do what it says:
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
You can see that the changes have been reverted.
Important
Itâs important to understand that
git checkout -- <file>
is a dangerous command.
Any local changes you made to that file are goneâââGit just replaced that file with the last staged or committed version.
Donât ever use this command unless you absolutely know that you donât want those unsaved local changes.
If you would like to keep the changes youâve made to that file but still need to get it out of the way for now, weâll go over stashing and branching in
Git Branching
; these are generally better ways to go.
Remember, anything that is
committed
in Git can almost always be recovered.
Even commits that were on branches that were deleted or commits that were overwritten with an
--amend
commit can be recovered (see
Data Recovery
for data recovery).
However, anything you lose that was never committed is likely never to be seen again.
Undoing things with git restore
Git version 2.23.0 introduced a new command:
git restore
.
Itâs basically an alternative to
git reset
which we just covered.
From Git version 2.23.0 onwards, Git will use
git restore
instead of
git reset
for many undo operations.
Letâs retrace our steps, and undo things with
git restore
instead of
git reset
.
Unstaging a Staged File with git restore
The next two sections demonstrate how to work with your staging area and working directory changes with
git restore
.
The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them.
For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type
git add *
and stage them both.
How can you unstage one of the two?
The
git status
command reminds you:
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: CONTRIBUTING.md
renamed: README.md -> README
Right below the âChanges to be committedâ text, it says use
git restore --staged <file>âŠâ
to unstage.
So, letâs use that advice to unstage the
CONTRIBUTING.md
file:
$ git restore --staged CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
The
CONTRIBUTING.md
file is modified but once again unstaged.
Unmodifying a Modified File with git restore
What if you realize that you donât want to keep your changes to the
CONTRIBUTING.md
file?
How can you easily unmodify itââârevert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)?
Luckily,
git status
tells you how to do that, too.
In the last example output, the unstaged area looks like this:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
It tells you pretty explicitly how to discard the changes youâve made.
Letâs do what it says:
$ git restore CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
Important
Itâs important to understand that
git restore <file>
is a dangerous command.
Any local changes you made to that file are goneâââGit just replaced that file with the last staged or committed version.
Donât ever use this command unless you absolutely know that you donât want those unsaved local changes. |
| Markdown | [](https://git-scm.com/) \--distributed-is-the-new-centralized

- [About](https://git-scm.com/about)
- [Trademark](https://git-scm.com/about/trademark)
- [Learn](https://git-scm.com/learn)
- [Book](https://git-scm.com/book)
- [Cheat Sheet](https://git-scm.com/cheat-sheet)
- [Videos](https://git-scm.com/videos)
- [External Links](https://git-scm.com/doc/ext)
- [Tools](https://git-scm.com/tools)
- [Command Line](https://git-scm.com/tools/command-line)
- [GUIs](https://git-scm.com/tools/guis)
- [Hosting](https://git-scm.com/tools/hosting)
- [Reference](https://git-scm.com/docs)
- [Install](https://git-scm.com/install/linux)
- [Community](https://git-scm.com/community)
***
This book is available in [English](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things).
Full translation available in
| |
|---|
| [azÉrbaycan dili](https://git-scm.com/book/az/v2/Git%E2%80%99in-%C6%8Fsaslar%C4%B1-L%C9%99%C4%9Fv-Edil%C9%99n-%C4%B0%C5%9Fl%C9%99r-Geri-qaytar%C4%B1lan), |
| [бŃлгаŃŃĐșĐž ДзОĐș](https://git-scm.com/book/bg/v2/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%B8-%D0%BD%D0%B0-Git-%D0%92%D1%8A%D0%B7%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D1%8F%D0%B2%D0%B0%D0%BD%D0%B5-%D0%BD%D0%B0-%D0%BD%D0%B0%D0%BF%D1%80%D0%B0%D0%B2%D0%B5%D0%BD%D0%B8-%D0%B4%D0%B5%D0%B9%D1%81%D1%82%D0%B2%D0%B8%D1%8F), |
| [Deutsch](https://git-scm.com/book/de/v2/Git-Grundlagen-Ungewollte-%C3%84nderungen-r%C3%BCckg%C3%A4ngig-machen), |
| [Español](https://git-scm.com/book/es/v2/Fundamentos-de-Git-Deshacer-Cosas), |
| [Ùۧ۱۳Û](https://git-scm.com/book/fa/v2/%D9%85%D9%82%D8%AF%D9%85%D8%A7%D8%AA-%DA%AF%DB%8C%D8%AA-git-basics-chapter-%D8%A8%D8%A7%D8%B2%DA%AF%D8%B1%D8%AF%D8%A7%D9%86%D8%AF%D9%86-%D8%AA%D8%BA%DB%8C%DB%8C%D8%B1%D8%A7%D8%AA-Undoing-Things), |
| [Français](https://git-scm.com/book/fr/v2/Les-bases-de-Git-Annuler-des-actions), |
| [ÎλληΜÎčÎșÎŹ](https://git-scm.com/book/gr/v2/%CE%A4%CE%B1-%CE%B8%CE%B5%CE%BC%CE%B5%CE%BB%CE%B9%CF%8E%CE%B4%CE%B7-%CF%83%CF%84%CE%BF%CE%B9%CF%87%CE%B5%CE%AF%CE%B1-%CF%84%CE%BF%CF%85-Git-%CE%91%CE%BD%CE%B1%CE%B9%CF%81%CE%AD%CF%83%CE%B5%CE%B9%CF%82-undoing), |
| [æ„æŹèȘ](https://git-scm.com/book/ja/v2/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-%E4%BD%9C%E6%A5%AD%E3%81%AE%E3%82%84%E3%82%8A%E7%9B%B4%E3%81%97), |
| [íê”ìŽ](https://git-scm.com/book/ko/v2/Git%EC%9D%98-%EA%B8%B0%EC%B4%88-%EB%90%98%EB%8F%8C%EB%A6%AC%EA%B8%B0), |
| [Nederlands](https://git-scm.com/book/nl/v2/Git-Basics-Dingen-ongedaan-maken), |
| [Đ ŃŃŃĐșĐžĐč](https://git-scm.com/book/ru/v2/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D1%8B-Git-%D0%9E%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D0%B8-%D0%BE%D1%82%D0%BC%D0%B5%D0%BD%D1%8B), |
| [SlovenĆĄÄina](https://git-scm.com/book/sl/v2/Osnove-Git-Razveljavljanje-stvari), |
| [ĐĄŃĐżŃĐșĐž](https://git-scm.com/book/sr/v2/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%B5-%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%B0-%D0%93%D0%B8%D1%82-%D0%9E%D0%BF%D0%BE%D0%B7%D0%B8%D0%B2), |
| [Svenska](https://git-scm.com/book/sv/v2/Grunderna-i-Git-%C3%85ngra-saker), |
| [Tagalog](https://git-scm.com/book/tl/v2/Mga-Pangunahing-Kaalaman-sa-Git-Pag-Undo-ng-mga-Bagay), |
| [TĂŒrkçe](https://git-scm.com/book/tr/v2/Git-Temelleri-De%C4%9Fi%C5%9Fiklikleri-Geri-Alma). |
| [ĐŁĐșŃаŃĐœŃŃĐșа](https://git-scm.com/book/uk/v2/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%B8-Git-%D0%A1%D0%BA%D0%B0%D1%81%D1%83%D0%B2%D0%B0%D0%BD%D0%BD%D1%8F-%D1%80%D0%B5%D1%87%D0%B5%D0%B9), |
| [çźäœäžæ](https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%92%A4%E6%B6%88%E6%93%8D%E4%BD%9C), |
Partial translations available in
| |
|---|
| [ÄeĆĄtina](https://git-scm.com/book/cs/v2/Z%C3%A1klady-pr%C3%A1ce-se-syst%C3%A9mem-Git-N%C3%A1vrat-do-p%C5%99edchoz%C3%ADho-stavu), |
| [ĐаĐșĐ”ĐŽĐŸĐœŃĐșĐž](https://git-scm.com/book/mk/v2/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D0%B5-%D0%BD%D0%B0-Git-%D0%9F%D0%BE%D0%BD%D0%B8%D1%88%D1%82%D1%83%D0%B2%D0%B0%D1%9A%D0%B5-%D0%BD%D0%B0-%D0%BD%D0%B5%D1%88%D1%82%D0%B0%D1%82%D0%B0), |
| [Polski](https://git-scm.com/book/pl/v2/Podstawy-Gita-Cofanie-zmian), |
| [PortuguĂȘs (Brasil)](https://git-scm.com/book/pt-br/v2/Fundamentos-de-Git-Desfazendo-coisas), |
| [ĐзбДĐșŃа](https://git-scm.com/book/uz/v2/Git-%D0%B0%D1%81%D0%BE%D1%81%D0%BB%D0%B0%D1%80%D0%B8-%D0%8E%D0%B7%D0%B3%D0%B0%D1%80%D0%B8%D1%88%D0%BB%D0%B0%D1%80%D0%BD%D0%B8-%D0%B1%D0%B5%D0%BA%D0%BE%D1%80-%D2%9B%D0%B8%D0%BB%D0%B8%D1%88), |
| [çčé«äžæ](https://git-scm.com/book/zh-tw/v2/Git-%E5%9F%BA%E7%A4%8E-%E5%BE%A9%E5%8E%9F), |
Translations started for
| |
|---|
| [ĐДлаŃŃŃĐșаŃ](https://git-scm.com/book/be/v2/Git-Basics-Undoing-Things), |
| [Indonesian](https://git-scm.com/book/id/v2/Git-Basics-Undoing-Things), |
| [Italiano](https://git-scm.com/book/it/v2/Git-Basics-Undoing-Things), |
| [Bahasa Melayu](https://git-scm.com/book/ms/v2/Git-Basics-Undoing-Things), |
| [PortuguĂȘs (Portugal)](https://git-scm.com/book/pt-pt/v2/No%C3%A7%C3%B5es-B%C3%A1sicas-do-Git-Desfazer-Coisas). |
***
The source of this book is [hosted on GitHub.](https://github.com/progit/progit2)
Patches, suggestions and comments are welcome.
[Chapters âŸ](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things)
1. ## 1\. [Getting Started](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
1. 1\.1 [About Version Control](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
2. 1\.2 [A Short History of Git](https://git-scm.com/book/en/v2/Getting-Started-A-Short-History-of-Git)
3. 1\.3 [What is Git?](https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F)
4. 1\.4 [The Command Line](https://git-scm.com/book/en/v2/Getting-Started-The-Command-Line)
5. 1\.5 [Installing Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
6. 1\.6 [First-Time Git Setup](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup)
7. 1\.7 [Getting Help](https://git-scm.com/book/en/v2/Getting-Started-Getting-Help)
8. 1\.8 [Summary](https://git-scm.com/book/en/v2/Getting-Started-Summary)
2. ## 2\. [Git Basics](https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository)
1. 2\.1 [Getting a Git Repository](https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository)
2. 2\.2 [Recording Changes to the Repository](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository)
3. 2\.3 [Viewing the Commit History](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)
4. 2\.4 [Undoing Things](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things)
5. 2\.5 [Working with Remotes](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)
6. 2\.6 [Tagging](https://git-scm.com/book/en/v2/Git-Basics-Tagging)
7. 2\.7 [Git Aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)
8. 2\.8 [Summary](https://git-scm.com/book/en/v2/Git-Basics-Summary)
3. ## 3\. [Git Branching](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)
1. 3\.1 [Branches in a Nutshell](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)
2. 3\.2 [Basic Branching and Merging](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging)
3. 3\.3 [Branch Management](https://git-scm.com/book/en/v2/Git-Branching-Branch-Management)
4. 3\.4 [Branching Workflows](https://git-scm.com/book/en/v2/Git-Branching-Branching-Workflows)
5. 3\.5 [Remote Branches](https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches)
6. 3\.6 [Rebasing](https://git-scm.com/book/en/v2/Git-Branching-Rebasing)
7. 3\.7 [Summary](https://git-scm.com/book/en/v2/Git-Branching-Summary)
4. ## 4\. [Git on the Server](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols)
1. 4\.1 [The Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols)
2. 4\.2 [Getting Git on a Server](https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server)
3. 4\.3 [Generating Your SSH Public Key](https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key)
4. 4\.4 [Setting Up the Server](https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server)
5. 4\.5 [Git Daemon](https://git-scm.com/book/en/v2/Git-on-the-Server-Git-Daemon)
6. 4\.6 [Smart HTTP](https://git-scm.com/book/en/v2/Git-on-the-Server-Smart-HTTP)
7. 4\.7 [GitWeb](https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb)
8. 4\.8 [GitLab](https://git-scm.com/book/en/v2/Git-on-the-Server-GitLab)
9. 4\.9 [Third Party Hosted Options](https://git-scm.com/book/en/v2/Git-on-the-Server-Third-Party-Hosted-Options)
10. 4\.10 [Summary](https://git-scm.com/book/en/v2/Git-on-the-Server-Summary)
5. ## 5\. [Distributed Git](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows)
1. 5\.1 [Distributed Workflows](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows)
2. 5\.2 [Contributing to a Project](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project)
3. 5\.3 [Maintaining a Project](https://git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project)
4. 5\.4 [Summary](https://git-scm.com/book/en/v2/Distributed-Git-Summary)
1. ## 6\. [GitHub](https://git-scm.com/book/en/v2/GitHub-Account-Setup-and-Configuration)
1. 6\.1 [Account Setup and Configuration](https://git-scm.com/book/en/v2/GitHub-Account-Setup-and-Configuration)
2. 6\.2 [Contributing to a Project](https://git-scm.com/book/en/v2/GitHub-Contributing-to-a-Project)
3. 6\.3 [Maintaining a Project](https://git-scm.com/book/en/v2/GitHub-Maintaining-a-Project)
4. 6\.4 [Managing an organization](https://git-scm.com/book/en/v2/GitHub-Managing-an-organization)
5. 6\.5 [Scripting GitHub](https://git-scm.com/book/en/v2/GitHub-Scripting-GitHub)
6. 6\.6 [Summary](https://git-scm.com/book/en/v2/GitHub-Summary)
2. ## 7\. [Git Tools](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection)
1. 7\.1 [Revision Selection](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection)
2. 7\.2 [Interactive Staging](https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging)
3. 7\.3 [Stashing and Cleaning](https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning)
4. 7\.4 [Signing Your Work](https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work)
5. 7\.5 [Searching](https://git-scm.com/book/en/v2/Git-Tools-Searching)
6. 7\.6 [Rewriting History](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History)
7. 7\.7 [Reset Demystified](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified)
8. 7\.8 [Advanced Merging](https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging)
9. 7\.9 [Rerere](https://git-scm.com/book/en/v2/Git-Tools-Rerere)
10. 7\.10 [Debugging with Git](https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git)
11. 7\.11 [Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules)
12. 7\.12 [Bundling](https://git-scm.com/book/en/v2/Git-Tools-Bundling)
13. 7\.13 [Replace](https://git-scm.com/book/en/v2/Git-Tools-Replace)
14. 7\.14 [Credential Storage](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage)
15. 7\.15 [Summary](https://git-scm.com/book/en/v2/Git-Tools-Summary)
3. ## 8\. [Customizing Git](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)
1. 8\.1 [Git Configuration](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)
2. 8\.2 [Git Attributes](https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes)
3. 8\.3 [Git Hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
4. 8\.4 [An Example Git-Enforced Policy](https://git-scm.com/book/en/v2/Customizing-Git-An-Example-Git-Enforced-Policy)
5. 8\.5 [Summary](https://git-scm.com/book/en/v2/Customizing-Git-Summary)
4. ## 9\. [Git and Other Systems](https://git-scm.com/book/en/v2/Git-and-Other-Systems-Git-as-a-Client)
1. 9\.1 [Git as a Client](https://git-scm.com/book/en/v2/Git-and-Other-Systems-Git-as-a-Client)
2. 9\.2 [Migrating to Git](https://git-scm.com/book/en/v2/Git-and-Other-Systems-Migrating-to-Git)
3. 9\.3 [Summary](https://git-scm.com/book/en/v2/Git-and-Other-Systems-Summary)
5. ## 10\. [Git Internals](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain)
1. 10\.1 [Plumbing and Porcelain](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain)
2. 10\.2 [Git Objects](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects)
3. 10\.3 [Git References](https://git-scm.com/book/en/v2/Git-Internals-Git-References)
4. 10\.4 [Packfiles](https://git-scm.com/book/en/v2/Git-Internals-Packfiles)
5. 10\.5 [The Refspec](https://git-scm.com/book/en/v2/Git-Internals-The-Refspec)
6. 10\.6 [Transfer Protocols](https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols)
7. 10\.7 [Maintenance and Data Recovery](https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery)
8. 10\.8 [Environment Variables](https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables)
9. 10\.9 [Summary](https://git-scm.com/book/en/v2/Git-Internals-Summary)
1. ## A1. [Appendix A: Git in Other Environments](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Graphical-Interfaces)
1. A1.1 [Graphical Interfaces](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Graphical-Interfaces)
2. A1.2 [Git in Visual Studio](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Visual-Studio)
3. A1.3 [Git in Visual Studio Code](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Visual-Studio-Code)
4. A1.4 [Git in IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-IntelliJ-/-PyCharm-/-WebStorm-/-PhpStorm-/-RubyMine)
5. A1.5 [Git in Sublime Text](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Sublime-Text)
6. A1.6 [Git in Bash](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Bash)
7. A1.7 [Git in Zsh](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh)
8. A1.8 [Git in PowerShell](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-PowerShell)
9. A1.9 [Summary](https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Summary)
2. ## A2. [Appendix B: Embedding Git in your Applications](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-Command-line-Git)
1. A2.1 [Command-line Git](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-Command-line-Git)
2. A2.2 [Libgit2](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-Libgit2)
3. A2.3 [JGit](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-JGit)
4. A2.4 [go-git](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-go-git)
5. A2.5 [Dulwich](https://git-scm.com/book/en/v2/Appendix-B:-Embedding-Git-in-your-Applications-Dulwich)
3. ## A3. [Appendix C: Git Commands](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Setup-and-Config)
1. A3.1 [Setup and Config](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Setup-and-Config)
2. A3.2 [Getting and Creating Projects](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Getting-and-Creating-Projects)
3. A3.3 [Basic Snapshotting](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Basic-Snapshotting)
4. A3.4 [Branching and Merging](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Branching-and-Merging)
5. A3.5 [Sharing and Updating Projects](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Sharing-and-Updating-Projects)
6. A3.6 [Inspection and Comparison](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Inspection-and-Comparison)
7. A3.7 [Debugging](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Debugging)
8. A3.8 [Patching](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Patching)
9. A3.9 [Email](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Email)
10. A3.10 [External Systems](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-External-Systems)
11. A3.11 [Administration](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Administration)
12. A3.12 [Plumbing Commands](https://git-scm.com/book/en/v2/Appendix-C:-Git-Commands-Plumbing-Commands)
2nd Edition
# 2\.4 Git Basics - Undoing Things
## Undoing Things
At any stage, you may want to undo something. Here, weâll review a few basic tools for undoing changes that youâve made. Be careful, because you canât always undo some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the `--amend` option:
```
$ git commit --amend
```
This command takes your staging area and uses it for the commit. If youâve made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same, and all youâll change is your commit message.
The same commit-message editor fires up, but it already contains the message of your previous commit. You can edit the message the same as always, but it overwrites your previous commit.
As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:
```
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
```
You end up with a single commit â the second commit replaces the results of the first.
| | |
|---|---|
| Note | Itâs important to understand that when youâre amending your last commit, youâre not so much fixing it as *replacing* it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place. Effectively, itâs as if the previous commit never happened, and it wonât show up in your repository history. The obvious value to amending commits is to make minor improvements to your last commit, without cluttering your repository history with commit messages of the form, âOops, forgot to add a fileâ or âDarn, fixing a typo in last commitâ. |
| | |
|---|---|
| Note | Only amend commits that are still local and have not been pushed somewhere. Amending previously pushed commits and force pushing the branch will cause problems for your collaborators. For more on what happens when you do this and how to recover if youâre on the receiving end read [The Perils of Rebasing](https://git-scm.com/book/en/v2/ch00/_rebase_peril). |
### Unstaging a Staged File
The next two sections demonstrate how to work with your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. How can you unstage one of the two? The `git status` command reminds you:
```
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
```
Right below the âChanges to be committedâ text, it says use `git reset HEAD <file>âŠâ` to unstage. So, letâs use that advice to unstage the `CONTRIBUTING.md` file:
```
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
The command is a bit strange, but it works. The `CONTRIBUTING.md` file is modified but once again unstaged.
| | |
|---|---|
| Note | Itâs true that `git reset` can be a dangerous command, especially if you provide the `--hard` flag. However, in the scenario described above, the file in your working directory is not touched, so itâs relatively safe. |
For now this magic invocation is all you need to know about the `git reset` command. Weâll go into much more detail about what `reset` does and how to master it to do really interesting things in [Reset Demystified](https://git-scm.com/book/en/v2/ch00/_git_reset).
### Unmodifying a Modified File
What if you realize that you donât want to keep your changes to the `CONTRIBUTING.md` file? How can you easily unmodify it â revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, `git status` tells you how to do that, too. In the last example output, the unstaged area looks like this:
```
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
It tells you pretty explicitly how to discard the changes youâve made. Letâs do what it says:
```
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
```
You can see that the changes have been reverted.
| | |
|---|---|
| Important | Itâs important to understand that `git checkout -- <file>` is a dangerous command. Any local changes you made to that file are gone â Git just replaced that file with the last staged or committed version. Donât ever use this command unless you absolutely know that you donât want those unsaved local changes. |
If you would like to keep the changes youâve made to that file but still need to get it out of the way for now, weâll go over stashing and branching in [Git Branching](https://git-scm.com/book/en/v2/ch00/ch03-git-branching); these are generally better ways to go.
Remember, anything that is *committed* in Git can almost always be recovered. Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see [Data Recovery](https://git-scm.com/book/en/v2/ch00/_data_recovery) for data recovery). However, anything you lose that was never committed is likely never to be seen again.
### Undoing things with git restore
Git version 2.23.0 introduced a new command: `git restore`. Itâs basically an alternative to `git reset` which we just covered. From Git version 2.23.0 onwards, Git will use `git restore` instead of `git reset` for many undo operations.
Letâs retrace our steps, and undo things with `git restore` instead of `git reset`.
#### Unstaging a Staged File with git restore
The next two sections demonstrate how to work with your staging area and working directory changes with `git restore`. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. How can you unstage one of the two? The `git status` command reminds you:
```
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: CONTRIBUTING.md
renamed: README.md -> README
```
Right below the âChanges to be committedâ text, it says use `git restore --staged <file>âŠâ` to unstage. So, letâs use that advice to unstage the `CONTRIBUTING.md` file:
```
$ git restore --staged CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
The `CONTRIBUTING.md` file is modified but once again unstaged.
#### Unmodifying a Modified File with git restore
What if you realize that you donât want to keep your changes to the `CONTRIBUTING.md` file? How can you easily unmodify it â revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, `git status` tells you how to do that, too. In the last example output, the unstaged area looks like this:
```
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
It tells you pretty explicitly how to discard the changes youâve made. Letâs do what it says:
```
$ git restore CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
```
| | |
|---|---|
| Important | Itâs important to understand that `git restore <file>` is a dangerous command. Any local changes you made to that file are gone â Git just replaced that file with the last staged or committed version. Donât ever use this command unless you absolutely know that you donât want those unsaved local changes. |
[prev](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History) \| [next](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)
[About this site](https://git-scm.com/site)
Patches, suggestions, and comments are welcome.
Git is a member of [Software Freedom Conservancy](https://git-scm.com/sfc) |
| Readable Markdown | At any stage, you may want to undo something. Here, weâll review a few basic tools for undoing changes that youâve made. Be careful, because you canât always undo some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the `--amend` option:
```
$ git commit --amend
```
This command takes your staging area and uses it for the commit. If youâve made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same, and all youâll change is your commit message.
The same commit-message editor fires up, but it already contains the message of your previous commit. You can edit the message the same as always, but it overwrites your previous commit.
As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:
```
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
```
You end up with a single commit â the second commit replaces the results of the first.
| | |
|---|---|
| Note | Itâs important to understand that when youâre amending your last commit, youâre not so much fixing it as *replacing* it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place. Effectively, itâs as if the previous commit never happened, and it wonât show up in your repository history. The obvious value to amending commits is to make minor improvements to your last commit, without cluttering your repository history with commit messages of the form, âOops, forgot to add a fileâ or âDarn, fixing a typo in last commitâ. |
| | |
|---|---|
| Note | Only amend commits that are still local and have not been pushed somewhere. Amending previously pushed commits and force pushing the branch will cause problems for your collaborators. For more on what happens when you do this and how to recover if youâre on the receiving end read [The Perils of Rebasing](https://git-scm.com/book/en/v2/ch00/_rebase_peril). |
### Unstaging a Staged File
The next two sections demonstrate how to work with your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. How can you unstage one of the two? The `git status` command reminds you:
```
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
```
Right below the âChanges to be committedâ text, it says use `git reset HEAD <file>âŠâ` to unstage. So, letâs use that advice to unstage the `CONTRIBUTING.md` file:
```
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
The command is a bit strange, but it works. The `CONTRIBUTING.md` file is modified but once again unstaged.
| | |
|---|---|
| Note | Itâs true that `git reset` can be a dangerous command, especially if you provide the `--hard` flag. However, in the scenario described above, the file in your working directory is not touched, so itâs relatively safe. |
For now this magic invocation is all you need to know about the `git reset` command. Weâll go into much more detail about what `reset` does and how to master it to do really interesting things in [Reset Demystified](https://git-scm.com/book/en/v2/ch00/_git_reset).
### Unmodifying a Modified File
What if you realize that you donât want to keep your changes to the `CONTRIBUTING.md` file? How can you easily unmodify it â revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, `git status` tells you how to do that, too. In the last example output, the unstaged area looks like this:
```
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
It tells you pretty explicitly how to discard the changes youâve made. Letâs do what it says:
```
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
```
You can see that the changes have been reverted.
| | |
|---|---|
| Important | Itâs important to understand that `git checkout -- <file>` is a dangerous command. Any local changes you made to that file are gone â Git just replaced that file with the last staged or committed version. Donât ever use this command unless you absolutely know that you donât want those unsaved local changes. |
If you would like to keep the changes youâve made to that file but still need to get it out of the way for now, weâll go over stashing and branching in [Git Branching](https://git-scm.com/book/en/v2/ch00/ch03-git-branching); these are generally better ways to go.
Remember, anything that is *committed* in Git can almost always be recovered. Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see [Data Recovery](https://git-scm.com/book/en/v2/ch00/_data_recovery) for data recovery). However, anything you lose that was never committed is likely never to be seen again.
### Undoing things with git restore
Git version 2.23.0 introduced a new command: `git restore`. Itâs basically an alternative to `git reset` which we just covered. From Git version 2.23.0 onwards, Git will use `git restore` instead of `git reset` for many undo operations.
Letâs retrace our steps, and undo things with `git restore` instead of `git reset`.
#### Unstaging a Staged File with git restore
The next two sections demonstrate how to work with your staging area and working directory changes with `git restore`. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, letâs say youâve changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. How can you unstage one of the two? The `git status` command reminds you:
```
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: CONTRIBUTING.md
renamed: README.md -> README
```
Right below the âChanges to be committedâ text, it says use `git restore --staged <file>âŠâ` to unstage. So, letâs use that advice to unstage the `CONTRIBUTING.md` file:
```
$ git restore --staged CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
The `CONTRIBUTING.md` file is modified but once again unstaged.
#### Unmodifying a Modified File with git restore
What if you realize that you donât want to keep your changes to the `CONTRIBUTING.md` file? How can you easily unmodify it â revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, `git status` tells you how to do that, too. In the last example output, the unstaged area looks like this:
```
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
```
It tells you pretty explicitly how to discard the changes youâve made. Letâs do what it says:
```
$ git restore CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
```
| | |
|---|---|
| Important | Itâs important to understand that `git restore <file>` is a dangerous command. Any local changes you made to that file are gone â Git just replaced that file with the last staged or committed version. Donât ever use this command unless you absolutely know that you donât want those unsaved local changes. | |
| Shard | 54 (laksa) |
| Root Hash | 7104038400628677254 |
| Unparsed URL | com,git-scm!/book/en/v2/Git-Basics-Undoing-Things s443 |