Discard file changes with Git

A common operation in Git is to discard changes made to files, to return to the state of the last commit.

Thanks to Git we can keep a detailed control of the changes in the files and a quite common operation is to discard changes in the files that we have just modified. It comes in very handy when, after making a series of changes, we realize that this was not a good path and we want to go back, to recover the state that our code had in the last confirmation (commit).

Actually, this operation has several working alternatives, depending on how you want to discard the changes made to the files, if you want to discard them forever, if you want to discard them but store them provisionally, etc. In this article we are going to shed some light and help you find the alternative that you need at all times.

Discard changes to a single file, forever

Imagine that you have modified several files since the last commit. Things are going well, but there is one of them that has some kind of problem. You want to back out that particular file but leave the changes in the rest of the modified files.

You can do this by using the “git checkout” command, followed by the name of the file you want to roll back.

git checkout –index.html

This command will leave the rest of the files in your workspace untouched, removing changes to only the file you needed. As you can see, in the command you indicate the name of the file to discard changes, which in our example was called “index.html”.

Note: remember that the most common use of checkout is to switch branches. You can get more information in the article.

Discard all changes in modified files

Now imagine that you have made changes to several files, but you didn’t like them and you want to return to the state of the last commit, permanently removing all changes to all files made.

You can do this with the “git reset” command. You can do it in the following way:

git reset –hard

The “–hard” option causes any file changes we track with Git to be removed, reverting to the state just after the last commit. This means that if there are files that have not yet been added to the repository (new files since the last commit), they will remain in the workspace.

Discard the changes of a file that is in the staging area

It could be the case that you have “git added” a file that you now intend to discard. In this case, this file is temporarily stored, in the space known as the “staging area”.

The previous reset operation will also work in this case.

git reset –hard

If you do “git reset –hard” those changes will also be removed, but it could happen that you have added several files to the staging area and only want to reset the changes of one of them. Then you would have to use the reset command with a different option (remember that “git reset –hard” discards changes to all files). To then remove these changes from a single file, we would first have to remove the particular file from the staging area and then discard the changes, with the combination of these two commands.

git reset HEAD file.txt git checkout — file.txt

Discard the changes, but temporarily save the changed files

There is an even more complex alternative with the “git stash” command to discard changes to a file, but without losing them completely, being able to revert to the previous state of those files later. This can be useful in many cases, such as when you have to change branches but you don’t want to commit the changes because you left your work half done. In that case you could discard the changes, but save them so that you can return to them later and pick up where you left off.

The “stash” command is complex enough to cover in a separate article, but let’s see a very simple operation so that we can solve this particular case.

git stash

Then we will see a message like this “Saved working directory and index state WIP on nombre_rama”. WIP stands for “work in progress.”

You will now see that the modified files are back to the state of the last commit. You could switch branches or do whatever, and when you want to go back to the state of your files stored as “work in progress”, you’ll run the following command:

git stash pop

This will cause the stash changes to be put back into your workspace.

If you want to see the status of your project, with information about files that you have in “Work In Progress” with stash, you can do a command like this:

git status –show-stash

conclusion

With what we have seen in this article you have a complete set of alternatives when you want to discard changes in one or several files in a repository and return to the previous state of the workspace, just after the last commit.

They will be quite helpful in many cases. Just make sure you’re sure you want to discard the changes, since the operation is permanent (since no commit was done on those changed files). Remember that if you want, for whatever reason, to leave the possibility of recovering those discarded changes open, you will have to use the stash operation. Consult our to continue learning other facets of the version control system.

See also  tracert Command
Loading Facebook Comments ...
Loading Disqus Comments ...