📜  git delete local commit - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:41:25.867000             🧑  作者: Mango

Git Delete Local Commit - Shell/Bash

Have you ever made a commit locally and then realized that you made a mistake or that the commit was unnecessary? In this guide, we will explore how to delete a local commit in Git using the Shell/Bash command line.

Step 1: Determine the Commit to Delete

First, we need to determine the commit that we want to delete. We can use git log command to display a list of commits in reverse chronological order (most recent ones first).

git log

This will display a list of commits, each with a unique hash code, commit message, author, and timestamp. Identify the commit you want to delete and copy its hash code.

Step 2: Undo the Commit

Now that we have identified the commit that we want to delete, we can use the git reset command to undo it while leaving the changes intact in the working directory.

git reset --soft <commit-hash>

Replace <commit-hash> with the hash code of the commit you want to delete. This command will move the HEAD pointer to the specified commit, effectively "uncommitting" it. The changes made in that commit will still be in the working directory and can be modified or staged for another commit.

Step 3: Remove the Commit

If you want to completely remove the changes made in the commit, you can use the git reset command again with the --hard option.

git reset --hard <commit-hash>

This will move the HEAD pointer to the specified commit and also discard any changes made in that commit. Warning: This cannot be undone, so use it with caution.

Conclusion

By following these simple steps, you can easily delete a local commit in Git using the Shell/Bash command line. Remember to use the git log command to identify the commit you want to delete, and use the git reset command with the relevant options to undo or completely remove the commit. Happy coding!