Git commit contains the complete information of the applied changes in the repository, which can be used later for viewing all modifications. Each commit has a unique commit I’d called SHA-hash. Developers can roll back the committed changes with a new commit id and message. Moreover, if they don’t want to add new commits and unstaged changes, they can use the “-n” as the “–no-commit” option along with the “git revert <sha-hash>” command.
This write-up discusses the procedure of reverting changes without staging them and the new commit.
How to git revert –no-commit Without Staging?
Follow the below-stated steps for reverting the committed changes without new commits and place them in the working area:
-
- Go to the Git root directory.
- Check the short commit SHA-hash.
- Choose the SHA-hash of a particular commit that needs to modify without staging.
- Run the “git revert -n <sha-hash> && git reset HEAD” command.
Step 1: Move to Root Directory
Type out the following command and redirect to the Git root directory:
Step 2: Check Git Commit SHA-Hash
Then, display the log history of the current working repository by running the “git log” command:
Here, the “–oneline” option will show the short commit id, and “-5” is the range of commits that needs to display. We have selected the below-highlighted commit for reverting changes:
Step 3: Git Revert Without Staging Changes
Finally, execute the “git revert” and “git reset” commands along with the selected “-n” option, previously selected commit hash, and HEAD pointer:
The “git revert” command usually generates the automatic commit message, to avoid this the “-n” option is used which represents the no commit. As you can see, the changes are reverted without a new commit and move to the working area:
Step 4: Ensure Reverted Operation
Lastly, use the “git log” command to view the log history of the current working branch:
As you can see, the HEAD pointer successfully moved to the selected commit SHA-hash and changes are reverted to the working area without a new commit:
That’s all! We have provided the process of reverting changes from the Git repository to the working area without new commits.
Conclusion
To revert the committed changes without new commits and place them in the working area, first, move to the Git root directory and display the short commit SHA-hash. Select the SHA-hash of a particular commit that needs to modify without staging. Then, execute the “git revert -n <sha-hash> && git reset HEAD” command. This write-up explained the method of reverting changes without staging them and the new commits.