Can we reset a command to cut off?

2.4 Git basics - Undoing unwanted changes

Undo unwanted changes

You can undo a change at any time. Here we will discuss some basic tools used to undo changes made. Be careful because you cannot always undo all of these cancellations. This is one of the few areas in Git where you could lose some work if you did something wrong.

One of the most common undos occurs when you commit too early and possibly forget to add some files, or when you mess up your commit message. If you want to commit again, make the additional changes you forgot, stage them, and commit them again with the option:

This command takes over your staging area and uses it for the commit. If you haven't made any changes since your last commit (e.g. you run this command immediately after your previous commit) then your snapshot will look exactly the same; You just change your commit message.

The same commit message editor is called, but it already contains the message of your previous commit. You can edit the message as usual, but it overwrites the previous commit.

For example, if you forgot the changes in a file that you wanted to add to this transfer, you can do something similar:

You end up with a single commit - the second commit replaces the results of the first.

It's important to understand that if you change your most recent commit, you're less likely to fix it than a new, improved commit replace. The old commit is moved out of the way and the new commit is put in its place. In fact, it's like the last commit never happened and it no longer shows up in your repository history.

The most obvious use of changing commits is making small tweaks to your last commit without changing your repository history with commit messages like "Oops, forgot to add a file" or "Damn, fixed a typo on last commit" too cluttered.

Only change local commits that have not yet been pushed. Changing previously committed commits and pushing the branch hard causes problems for your colleagues. For more information on what happens when you do this, and how to get it straightened out once you're on the receiving end, see The Dangers of Rebasing.

Remove a file from the staging area

The next two sections explain how to work with your staging area and the changes to the working directory. The nice side effect is that the command you use to determine the state of these two areas also reminds you how to undo changes to them. For example, suppose you have changed two files and want to transfer them as two separate changes, but you accidentally enter them and then post them both in the staging area. How can you remove one of the two from the staging area? The command reports:

Directly below the text "Changes to be committed" it says that you should use to empty the staging area. So let's follow this advice and remove the file from the staging area:

The command sounds a bit strange, but it works. The file has been modified, but is again in the unstaged state.

It is true that command can be a risky one, especially if you pass the flag on. However, in the scenario described above, the file in your working directory will not be touched, so it is relatively safe.

For now, that call is all you need to know about the command. We'll go into much more detail about what does and how to master it to get really interesting tasks done, see Chapter 7 Git Reset.

Undo a change in a modified file

What if you find that you don't want to keep your changes to the file? How can you easily change it back - revert it back to what it was when it was last committed (or was initially cloned, or whatever you got it in your working directory)? Fortunately, it also tells you how to do that. In the last example, the unstaged area looks like this:

It tells you pretty clearly how to discard the changes you've made. Let's do what it says:

You can tell that the changes have been undone.

It is very important to understand that this is a risky command. Any local changes you made to this file are lost - Git simply replaced this file with the most recently committed or staged version. Never use this command unless you are absolutely certain that you do not want these unsaved local changes.

If you want to keep the changes you made to this file, but want to get them out of the way for now, we should go over the stashing and branching in Chapter 3 - Git Branching; these are generally the better ways of doing this.

Remember that everything is in Git committed will almost always be recoverable. Even commits that were on deleted branches or commits that have been overwritten with a commit can be restored (see Chapter 10 Data Recovery for restoring data). However, anything you've lost that was never committed will likely never be seen again.

Undo changes with git restore

Git version 2.23.0 introduced a new command:. It's basically an alternative to what we just covered. Starting with Git version 2.23.0, Git uses undos instead of.

Let's retrace our steps and use things instead of undo.

Unstages of a staged file with git restore

The next two sections show how you can work on changes in your staging area and in the working directory. The beauty of it is that the command you use to determine the status of these two areas also shows you how to undo changes to them. For example, suppose you've changed two files and want to commit them as two separate changes. However, you accidentally enter and provide both. How can you have one of the two unstays again? The command shows them:

Immediately below the text "Changes to be committed" is written on unstagen. Let's use this advice to unstag the file:

The file is changed but unstaged again.

Undo a changed file with git restore

What if you find that you don't want to keep your changes to the file? How can you just undo it - that is, reset it to what it was when you last committed it (or originally cloned it, or whatever you put it in your working directory)? Fortunately, in turn, tells you how to do that. In the last sample output, the unstaged area looks like this:

It shows you quite explicitly how to discard the changes you made. Let's do that:

It is important to understand that this is a dangerous command. Any local changes you made to this file are gone. Git replaced this file with the most recently committed or staged version. Use this command only if you are absolutely certain that you do not need these unsaved local changes.