Git

How to git revert –no-commit Without Staging

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:

$ cd "C:\Users\nazma\Git"

 
Step 2: Check Git Commit SHA-Hash

Then, display the log history of the current working repository by running the “git log” command:

$ git log --oneline -5

 
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:

$ git revert -n bee2ca6 && git reset HEAD

 
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:

$ git log --oneline -5

 
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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.