To undo the last commit in Git while keeping the changes staged, use the command:
git reset --soft HEAD~1
Understanding Git Commits
What is a Git Commit?
A commit in Git represents a snapshot of your project at a particular point in time. Each commit contains a unique identifier (a hash), a commit message describing the changes made, and metadata such as the author and timestamp. Commits allow you to track changes, review history, and collaborate with team members efficiently.
Importance of Commits
Commits are crucial for collaborative efforts because they create a clear history of the project, showcasing who made which changes and why. This organized history enables developers to review or revert to any prior state, facilitating teamwork, accountability, and project management.

Methods to Undo a Commit
Undo the Last Commit
Using `git reset`
If you want to undo your very last commit, you can use the `git reset` command. This command moves the current branch pointer back to the specified state, effectively "removing" the last commit from the history:
git reset HEAD~
In this command, `HEAD~` refers to the commit prior to the latest one. Using `git reset` will affect your staged changes, meaning your files will revert, removing the last commit while keeping your changes in the working directory.
Using `git reset --soft`
If you only want to remove the last commit but keep all your changes staged, the `--soft` option is your friend:
git reset --soft HEAD~
This command allows you to undo the commit while preserving the changes in the staging area, enabling you to modify and commit them again later if necessary.
Using `git reset --mixed`
The `--mixed` option is similar to the default `git reset` behavior but makes a clear distinction in how your workspace is affected:
git reset --mixed HEAD~
Using this option, the last commit is removed like in the previous example, but your changes remain un-staged in your working directory. This is a great way to edit the changes before staging them again.
Undo a Specific Commit
Using `git revert`
To undo a specific commit that is not necessarily the last one made, you can utilize the `git revert` command. This command is particularly useful because it maintains the project's commit history by creating a new commit that undoes the changes of a specified commit:
git revert <commit_hash>
To find the commit hash, you can use `git log`, demonstrating the power of Git in traceability. The `<commit_hash>` is replaced with the actual hash of the commit you want to undo. This method is highly recommended if you're working in a shared repository where others may be affected by your changes.
Use Case for `git revert`
The `git revert` command shines in collaborative environments. By creating a new commit to undo the changes, it preserves the integrity of the project history, making it easier for others to track what has occurred.
Restore Uncommitted Changes
Using `git checkout`
If you have uncommitted changes in your working directory, and you'd like to discard them, `git checkout` can come to your rescue:
git checkout -- <file_name>
This command lets you specify the file you wish to restore. Keep in mind that this action is irreversible; the changes will be lost.
Using `git stash`
For situations where you want to temporarily set aside your work without committing it, `git stash` offers a simple yet effective solution:
git stash
This command saves your uncommitted changes into a "stash," allowing you to work on something else and apply the stashed changes later. To retrieve your stashed changes, you would use:
git stash apply

Scenarios for Undoing Commits
Common Use Cases
Mistaken Commit with Wrong Changes
If you accidentally commit changes that were meant for a different branch or are irrelevant, using `git reset` or `git revert` can rectify the situation quickly. Depending on your collaborative workflow, choose the method that best suits your project's history.
Commits Made to Wrong Branch
In cases where you committed changes to the wrong branch, the recommended approach is to reset the commit in the incorrect branch and then cherry-pick or reapply the changes to the correct branch, ensuring that no work is lost.
Recovering Lost Work
How to Find a Lost Commit
If you've lost track of your commits, using `git reflog` is a lifesaver. This command shows you a log of all actions performed in your repository, including those that are not shown in the standard `git log`.
git reflog
To restore a lost commit, find the relevant commit hash in the reflog output, and then you can either reset your branch or create a new branch from that commit hash.

Best Practices for Undoing Commits
Commit Messages and History
Writing clear and descriptive commit messages is paramount. A well-articulated commit message aids in navigating your project history, making it easier to identify the purpose of each change and the chronology of development.
Avoiding Common Pitfalls
When undoing commits, it's crucial to understand the differences between resetting and reverting. Resetting affects the commit history and can lead to lost changes, while reverting guarantees that changes are tracked accordingly. Always double-check your commands before executing them to prevent unintended consequences.

Conclusion
Knowing git how to undo commit is essential for any Git user, from beginners to advanced developers. With various methods at your disposal, from `git reset` for the last commit to `git revert` for specific ones, you can effectively manage changes in your projects. Remember, practice makes perfect, so don’t hesitate to experiment in a safe environment to build your confidence with these commands.

Additional Resources
For further learning, consult the [official Git documentation](https://git-scm.com/doc) and look for recommended tutorials that deepen your understanding of undoing commits and other Git functionalities.

FAQs
What happens if I accidentally erase commits?
If you mistakenly remove commits, don't panic! You can often recover them using `git reflog` to find their hashed values and restore them appropriately.
Can I undo multiple commits at once?
Yes, you can undo multiple commits utilizing `git reset` with `<number_of_commits>` to specify how many commits you'd like to revert. However, be cautious as this affects your commit history, and employing `git revert` with multiple commit hashes can also be a strategy to consider for preserving history.