Undoing Changes in Git

Undoing Changes in Git

Undoing Changes in Git

Sometimes, you need to undo changes in Git—whether it's discarding uncommitted modifications, upstaging files, or rolling back commits. Below are different ways to undo changes based on your needs.

1. Undo Uncommitted Changes

Scenario 1: Discard Changes in a Tracked File

If you modified a file but haven’t staged it yet, you can discard the changes:

git restore filename

Example:

git restore index.html

🔹 This will reset index.html back to the last committed version.

Scenario 2: Undo Changes in All Tracked Files

If you want to discard all modifications in the working directory:

git restore .

🔹 This resets all modified files to their last committed state.

Scenario 3: Undo Staged Changes (Unstage a File)

If you accidentally staged a file (git add filename) but haven’t committed it yet:

git restore --staged filename

Example:

git restore --staged index.html

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

To unstage all staged files:

git restore --staged .

2. Undo a Commit

Scenario 4: Undo the Last Commit (Keep Changes)

If you already committed but haven’t pushed yet, you can undo the commit while keeping the changes:

git reset --soft HEAD~1

🔹 This moves the last commit back to the staging area, so you can make further modifications or recommit.

Scenario 5: Undo the Last Commit (Discard Staging, Keep Changes in Files)

If you want to remove the commit and unstaged changes, but keep the modifications in your working directory:

git reset --mixed HEAD~1

🔹 This removes the commit but does not delete file modifications.

Scenario 6: Undo the Last Commit (Discard Changes Completely)

If you don’t want to keep the changes and want to reset everything:

git reset --hard HEAD~1

🔹 This completely removes the last commit and all changes. 🚨 Use with caution!

3. Undo a Pushed Commit

Scenario 7: Remove the Last Commit from a Remote Repository

If you already pushed a commit and want to remove it:

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

🔹 Warning: This forces the remote repository to remove the commit and can affect collaborators.

Scenario 8: Revert a Commit (Recommended for Shared Repositories)

Instead of deleting a commit, you can create a new commit that undoes it:

git revert HEAD

🔹 This is safer because it creates a new commit that reverses the previous commit, keeping history intact.

To revert a specific commit (not just the last one), use:

git revert <commit-hash>

4. Undoing a Merge

Scenario 9: Undo a Merge Before Pushing

If you merged a branch but haven’t pushed it yet, you can undo it:

git reset --hard ORIG_HEAD

🔹 This resets your branch to the state before the merge.

Scenario 10: Revert a Merged Commit After Pushing

If you pushed a merge commit and want to undo it:

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

🔹 This creates a new commit that undoes the merge.

Conclusion

Undoing changes in Git depends on what you want to revert:

  • Unstaged changes?git restore filename
  • Unstage a file?git restore --staged filename
  • Undo last commit (keep changes)?git reset --soft HEAD~1
  • Undo last commit (discard changes)?git reset --hard HEAD~1
  • Undo a pushed commit?git revert HEAD (safer) or git reset --hard (forceful)

Would you like more details or examples? 🚀

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