Have you ever changed something in your Git repository and then regretted it? Maybe you changed anything that broke your code or removed a file by mistake. You can reverse changes and return your repository to an earlier state with the aid of Git's strong undo tools, so don't worry.
I'll explain two of these tools in this tutorial:
git revert and
git reset. These two commands are comparable in that they both let you roll back changes to your repository, but they operate somewhat differently and are most effective in various circumstances.
Before we move on, remember you can implement your websites or landing pages with or without coding on DoTenX for free. Make sure to check it out and even nominate your work to be showcased.DoTenX is open-source and you can find the repository here: github.com/dotenx/dotenx.
git revert command, you may roll back a specific commit. It works by generating a new commit that reverses the modifications made in the first commit. This is helpful if you accidentally commit anything but still want to preserve the remainder of your commit history.
You must specify the commit that you wish to undo in order to utilise git revert. Meaning, you should use the hash of the commit, which serves as a special identifier for every commit. You can use
git log command that displays a list of all the commits in your repository along with their hashes, allowing you to find the hash for a particular commit.
Let's take a look at an example of how to use
git revert to undo a specific commit:
$ git log commit abcdef0123456789 Author: John Doe <email@example.com> Date: Mon Jan 1 12:00:00 2023 -0600 Add new feature commit abcdef9876543210 Author: John Doe <firstname.lastname@example.org> Date: Sun Dec 31 12:00:00 2022 -0600 Initial commit
Let's say that you want to undo the commit with the hash
abcdef0123456789. To do this, you would run the following command:
$ git revert abcdef0123456789
Git will then create a new commit that undoes the changes made in the original commit. You'll need to enter a commit message for this new commit, explaining why you're undoing the change.
Now if we run the
git log command again, we'll see a new commit:
commit abcdef4567890123 Author: John Doe <email@example.com> Date: Mon Jan 1 12:00:00 2022 -0600 Revert "Add new feature"
Another alternative to undo the changes with git is
git reset. Its ability to entirely delete commits from your repository's history makes git reset a more powerful option than git revert. Particularly, if you want to totally undo a sequence of commits you made, or if you want to restore your repository to a former state, this command comes in handy.
With git reset, you should specify a commit by using its hash, much like with git revert. However, git reset offers a number of settings that may be used to alter its behaviour.
--hard are the most common selections.
Here is a brief description of what each of these choices do:
--soft: This option shifts the HEAD pointer, which currently links to the current commit, to the chosen commit while leaving the commit history intact. This makes it possible to roll back changes without really removing them from the repository's history.
--mixed (default): This option unstages any changes made after the given commit in addition to moving the HEAD pointer to that commit. This allows you to reverse commits and delete any modifications made after those commits.
--hard: This flag discards all modifications made after the given commit and moves the HEAD pointer to that commit. Since it entirely ignores any modifications made after the chosen commit, this choice is the harshest and should be done with extra care. When you wish to entirely restore your repository to a former state and toss out all modifications made after then, you can use this option.
Let's see what would happen if we used
git reset instead of
git revert in our example:
$ git reset --hard abcdef0123456789
This will move the HEAD to the commit
abcdef9876543210 and discard all the changes.
abcdef0123456789 will be completely removed from the repository's history after running this command.
When you make a mistake in
git there are multiple options to undo the actions, but remember when deciding between
reset commands, try to use revert if you can and only use reset if you really want to alter the history.
Also, if you use the
reset command, with
--mixed option you give yourself a chance to make modifications to the unstaged changes.