git reset
There may be times when you realize that the changes you made weren't that good. Modify a few files, add and delete several lines, and now you want to undo it. Simply put, you need to roll back the changes you made and go back to the original files. Developers can use "reset to HEAD" to fix bugs quite quickly.
You can use this command to rewrite the history to look as if you never made the commit. It is used when you have committed but haven't shared it with anyone else.
Many commands are available for resetting, such as:
$ git reset --hard HEAD -To go back to HEAD
$ git reset --hard HEAD^ -To go back to the commit before HEAD
$ git reset --hard HEAD~1 -equals to to "^."
$ git reset --hard HEAD~2 -To go back two commits before HEAD
To check what changes have been made, write “git log –oneline.”

To move the Head one step back, use the command: "$ git reset --hard Head~1".

We have undone the previous revert action "Revert changes to example file again.".

We have been able to retrieve the file's content by doing this.

git rebase
In software development, rebasing refers to moving or combining a series of commits into a single commit. Changing the base of a branch means turning it into if it had been made from a different commit. A Git commit is applied to a base by creating a new commit. Although the branch looks the same, it consists of new commits.
This is an alternative to merging because it gives us a cleaner history of the repository, and a clearer history can help us find bugs faster. There are two branches here, the master branch and the "new_branch."

Next, create a file "new_file.txt" in the "new_branch."

The contents of the "new_branch" are shown here.

After changing to the master branch, "new_file.txt" is no longer visible because it is no longer part of the master branch but only part of the "new_branch."

A new file has been added to the master branch named 'new_file_1'.

As soon as you make changes to the "master" branch, you can use the git rebase command to integrate the changes into "new_branch."
Before performing git rebase, the repository will look like this:

If you perform git rebase, you'll see that "new_file_1" is also added to "new_branch."

git reflog
The reflog maintains a record of when branch tips are modified. This command aims to maintain that information for about 90 days. In the reflog, you will find a record of every Git operation you perform where data is stored. Even though Git works hard to keep your data safe if, for some reason, you believe it has, chances are you can retrieve it using the git reflog command.
Thus, you wouldn't need to worry about losing your work if you merged, rebased, or used some other action since this command will bring it back.
You'll typically use this command if you just did a git reset and updated your HEAD a few commits ago. Then you realize that you left that bit of code in your second commit. What do you do? you do? You probably want to run git reflog as follows:

Check out most important Git Interview Questions here.
FAQs
-
How do git reverts and git resets differ?
Git reset, and git revert is two different commands that reset the branch state to a previous state by removing all the changes since the desired commit, whereas git revert will create new commits that reverse the selected commit. You should use git revert rather than git reset.
-
What are the differences between git merge and git rebase?
Git rebase and merge allow you to merge changes between two branches. The difference is that git rebase moves a feature branch into the master. Git merge preserves history while adding new commits.
-
Make a comparison between git log and git reflog.
Reflogs differ significantly from logs in that the log presents a public accounting of the repository's commit history, whereas reflogs offer a private and workspace-specific accounting.
-
What is soft and hard git resets?
Your files will not be deleted, and all changes will be staged back automatically when you run git reset --soft. With git reset --hard, all changes will be wiped out and removed from the local directory. Please only use this if you are confident of what you're doing.
Key Takeaways
In this blog, we have seen that git change commands are used to restore or delete the changes we had made in the files using various commands.
We hope that this blog has helped you enhance your knowledge about Git Change Commands and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow.
Happy Coding!