Git reset

Git reset

Git Reset: Undoing Changes in Git

The git reset command is a powerful tool that lets you undo commits, unstage files, and reset your working directory. It is often used to move the HEAD pointer and adjust the state of your repository.

1. Understanding git reset Modes

Reset ModeKeeps Changes in Working Directory?Keeps Changes in Staging Area?Moves HEAD?
--soft✅ Yes✅ Yes✅ Yes
--mixed (default)✅ Yes❌ No✅ Yes
--hard❌ No❌ No✅ Yes

What is HEAD?

  • HEAD is the reference to the latest commit on your current branch.
  • Resetting moves HEAD to a different commit.

2. Reset the Last Commit

1. Undo the Last Commit (Keep Changes & Staging)

If you want to undo the last commit but keep the changes in the staging area:

git reset --soft HEAD~1

🔹 This moves HEAD back by one commit but keeps all file changes staged.
🔹 You can make more edits and commit again.

2. Undo the Last Commit (Unstage Changes, Keep in Working Directory)

If you want to undo the last commit and unstage changes but keep them in the working directory:

git reset --mixed HEAD~1

🔹 This removes the last commit, unstages the changes, but keeps the modified files.
🔹 Now, you can re-add files (git add .) or modify them further before committing.

3. Undo the Last Commit (Remove Everything, No Recovery!)

If you want to completely remove the last commit and delete all changes:

git reset --hard HEAD~1

🚨 Warning: This permanently deletes changes! Use this only if you're sure.

3. Reset to a Specific Commit

If you need to reset your branch to a specific commit, find the commit hash using:

git log --oneline

1. Reset to a Specific Commit (Keep Changes & Staging)

git reset --soft <commit-hash>

2. Reset to a Specific Commit (Unstage Changes, Keep in Working Directory)

git reset --mixed <commit-hash>

3. Reset to a Specific Commit (Erase Everything)

git reset --hard <commit-hash>

🚨 Be careful! If you use --hard, all changes after that commit will be permanently removed.

4. Unstage Files Without Losing Changes

If you staged a file but haven’t committed it yet and want to unstage it:

git reset HEAD filename

🔹 This removes the file from the staging area but keeps changes in your working directory.

To unstage all files:

git reset

5. Reset a Remote Commit (Undo Pushed Commits)

If you've already pushed a commit but need to undo it:

1. Reset a Commit and Force Push (Destructive!)

git reset --hard HEAD~1 git push --force

🚨 Warning: This rewrites history and affects other collaborators!

2. Revert a Commit (Safer Alternative)

Instead of resetting, create a new commit that undoes the previous one:

git revert HEAD

🔹 This is safer because it preserves commit history.

6. Reset a Merged Commit

Undo a Merge Before Pushing

git reset --hard ORIG_HEAD

🔹 This resets your branch to before the merge.

Undo a Merge After Pushing

git revert -m 1 <merge-commit-hash>

🔹 This creates a new commit that undoes the merge.

Conclusion

The a git reset command is powerful and should be used carefully. Here's a quick guide:

ScenarioCommand
Undo the last commit (keep changes & staging)git reset --soft HEAD~1
Undo the last commit (keep changes, unstage files)git reset --mixed HEAD~1
Undo the last commit (discard everything)git reset --hard HEAD~1
Reset to a specific commit (keep changes)git reset --soft <commit-hash>
Reset to a specific commit (unstage changes)git reset --mixed <commit-hash>
Reset to a specific commit (erase everything)git reset --hard <commit-hash>
Unstage a filegit reset HEAD filename
Unstage all filesgit reset
Undo pushed commit (force push)git reset --hard HEAD~1 && git push --force
Revert a commit safelygit revert HEAD

Would you like additional examples or explanations? 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close