Git

Uncommit Git’s Last Commit Example

Software engineers or developers push a lot of commits to the Git repository after every minimal modification in the project source code. At the same time, they may want to commit some files that should not be pushed to the Git repository. As a consequence, it is required to undo or revert the commit to the previous commit from their Git repository. The “$ git stash” command has come to facilitate the developers in this regard.

This manual will elaborate on the procedure to uncommit Git’s last commit with examples.

Example: How to Uncommit Git’s Last Commit?

To revert or uncommit Git’s last commit, first, move to the Git directory, then create and update the file. Next, add the file to the Git directory and check the current directory status. After that, commit all changes and view the Git reference log history. Again, make and commit changes in the file and check the status. Lastly, uncommit the last commit and check the repository status.

Now, move toward the implementation of the above-discussed procedure!

Step 1: Move to Directory

Execute the “cd” command to move inside the required Git directory:

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

Step 2: Create and Update File

Next, create and make changes in the Git repository simultaneously utilizing the below command:

$ touch file1.txt && echo "file1 added"> file1.txt

For instance, we have created a file name “file1.txt” and added the required text to it using the “echo” command and the redirect operator “>”:

Step 3: Add File

Now, add the created file into the Git repository by exciting the “git add” command:

$ git add file1.txt

Step 4: Check Status

Run the given command to view the repository’s current status:

$ git status .

As you can see, our “file1.txt” is added successfully:

Step 5: Commit Changes

Next, commit all changes into the repository with the help of the below command:

$ git commit -m "1 file added"

Here, you can also specify a commit message using the “-m” option:

Step 6: View Repository Log Reference History

Then, run the “git log” command with the “–oneline” flag to view the reference log history of repository:

$ git log --oneline

Step 7: Update File

Execute the “echo” command to make changes in the created file:

$ echo "it's not my first file"> file1.txt

Step 8: Commit Changes

Again, commit changes to the Git repository using the below-given command:

$ git commit -m "file1.txt updated"

Step 9: Check Repository Reference Log History

Check the reference log history of the repository through the following command:

$ git log --oneline

Step 10: Uncommit Changes

Finally, execute the “git reset” command with the“–soft HEAD~1” option to revert the recently made changes:

$ git reset --soft HEAD~1

Step 11: Verify Uncommit Operation

Lastly, run the “git log” command to verify the revert or uncommit operation:

$ git log --oneline

As you see, the recently made changes are reverted successfully and HEAD point towards the previous commit reference:

This study led you to uncommit Git most recent commit with an example.

Conclusion

To revert Git’s last commit, first, move to the Git directory, then create and update the required file. Next, add the file to the Git directory and check the current directory status. After that, commit all changes and view the Git reference log history. Again, make and commit changes in the file and check the status. At last, uncommit the last commit by executing the “$ git reset –soft HEAD~1” command. This manual illustrated the method to uncommit Git’s last commit.

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.