Git Exercises

Git Reset

The `git reset` command is used to undo or modify commits in your Git repository. It allows you to move the HEAD and branch pointers to a different commit, effectively resetting the state of your repository. Here are a few common use cases for `git reset`:


1. **Undo the Last Commit**: If you want to undo the most recent commit, you can use the following command:

git reset HEAD~1
This moves the HEAD and branch pointer to the commit before the current HEAD, effectively removing the last commit from the branch. The changes from the undone commit will remain in your working directory as uncommitted changes.


2. **Reset to a Specific Commit**: If you want to reset your branch to a specific commit, you can provide the commit hash as an argument to `git reset`:

git reset <commit>
Replace `<commit>` with the hash of the desired commit. This will move the HEAD and branch pointer to the specified commit, discarding any commits between the current HEAD and the target commit. Any changes introduced in the discarded commits will be lost.


3. **Reset with Different Modes**: `git reset` provides different modes that determine how it affects the staging area and working directory:

- `--soft`: Moves the HEAD and branch pointer to the target commit without modifying the staging area or working directory. Changes from the undone commits are preserved as uncommitted changes. Use this mode if you want to "undo" commits but keep the changes in your working directory.
- `--mixed` (default): Moves the HEAD and branch pointer to the target commit and resets the staging area, but leaves the working directory untouched. Changes from the undone commits are unstaged. Use this mode if you want to unstage changes and keep them in your working directory.
- `--hard`: Moves the HEAD and branch pointer to the target commit and resets both the staging area and working directory to match the target commit. Changes from the undone commits are discarded. Use this mode if you want to completely discard changes.


4. **Use Caution with Public Repositories**: If you're working with a repository that others have already cloned or pulled from, be cautious when using `git reset`. Rewriting the commit history can cause issues for others who have based their work on the existing commits. In such cases, it's generally recommended to use `git revert` instead, which creates new commits that undo changes while preserving the commit history.


It's important to note that `git reset` modifies the commit history, so use it with caution and make sure to back up any important changes before performing a reset.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext