Web Development Course:
It would be hard to work with any version control system, including Git if you could not return any changes to their original state. That would make every mistake very pricey as you would have to start the same work over and over again until you master the workflow to perfect. Luckily, the Git revert commit function and its variations allow to undo the changes and suggest several smart ways of how to secure them with a new commit.
Git undo commit options have been designed for different actions while working with Git files. With them, you can remove files from the staging area, edit recent commits, see and edit the timeline of commits or even invert changes and finalize them further with a new Git revert commit.
Learning how to use Git revert commit and its variations, such as Git undo commit, or Git revert is essential if you want to work with your Git files freely. You should note as well that some functions, for example, Git checkout are very broad and can be used for different purposes. In this tutorial, you will learn how to use it and other commands for undoing the undesired changes.
Git Revert Commit: Main Tips
- There are several options of how to edit changes and Git revert to previous commit in the system.
- Some of them, for example, Git checkout, can perform a number of actions.
- Git revert commands or Git undo commit is a convenient way to cancel the undesired changes.
Editing the Recent Commit
You can begin the changing process by editing a recent commit. Git has a specific command for it:
$ git commit --amend
It will allow you to add more or edit the changes of the recent commit. It can be used for alterations as little as editing the commit message.
The purpose of this command is to unstage the staged files in such a case if you staged a file and changed your mind or realized you chose a wrong file. Essentially, it performs Git undo add of the files you were planning to stage but changed your mind.
An exampe of Git undo add looks as the following:
$ git reset <file>
Git Checkout Command
Git checkout is used to go back to the file version of the previous commit - to the previous set of changes committed. Git checkout is run by this line:
$ git checkout -- <file>
In general, this command switches branches or restores files in the working tree. It can be used to discard changes in the working directory, for a particular file that you specify when you use this command. Git checkout is a broad command that can be used for actions other than undo.
Sometimes it might be useful to see the entire timeline of commits made. You can do that by running git log. By running it, you can select a specific commit you would like to go back to.
Cleaning Untracked Files from Repository
Git clean command is used for untracked changes, i.e., changes that are in the working directory but haven't been added to staging by Git add. It is a common practice to remove the untracked files from repositories.
For this purpose, git clean is used. You can use various options of it, for example:
$ git clean -n
This command will show you a list of files that are about to be removed. It is a good idea to run this list before actually removing the files just to be sure you have chosen the right ones.
Then, to initiate the actual cleaning, you can force it by using this line:
$ git clean -f
$ git clean --force
This command will remove the untracked files, except those marked with .gitignore suffix and the ones specified in the ignore file.
You can also specify the path to a specific file you would like to remove by this command:
$ git clean -f <filepath>
Git Undo Last Commit
Sometimes you might need the simple plain undo version of Git revert. For that, you can run this command:
$ git reset --soft HEAD~1
As you can see, it will bring the HEAD branch to a certain revision, in this particular case - one revision back. Note that --soft indicates that the changes in undone revisions are maintained. So basically, this function is useful for developers who need to Git undo last commit.
Securing Change with New Commit
Git revert command is a smart option of how to execute a process, similar to "undo" in many other systems. Instead of simply undoing the commit, it returns to the changes represented by a commit and secures a new commit with the redefined changes.
This command is useful for simple changes by referring to a certain commit in the past. That's why it is called Git revert commit sometimes:
$ git revert
To execute Git revert, the tree HEAD commit must be without any recent modifications.
A simple usage of this command is typing in git revert HEAD to go back to the last commit before the current one.
Git Revert Commit: Summary
- Git revert to previous commit and undoing changes in Git is a well-developed function.
- Git checkout command among others have a broader range of functions than a simple "undo".
- Git revert commands are the most convenient way to undo the changes.