How do I undo my last commit without losing changes? #1426
-
|
I did |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
If you accidentally committed but forgot to add some files, don’t use git reset --hard—this discards your changes completely. Safe way to undo your last commit but keep your changes:git reset --soft HEAD~1What happens: --soft keeps your changes in the staging area (ready to commit again). You can now add any missed files and re-commit safely. Example workflow: ===================== Undo last commit but keep your changes stagedgit reset --soft HEAD~1 Add forgotten filesgit add forgotten-file.txt Commit againgit commit -m "Your improved commit message" Push (force if needed)git push --force =============== Why this is better: Allows you to fix the commit message or include missing files. Avoids data loss, unlike git reset --hard. This method is recommended for undoing the last commit locally before pushing. If you already pushed, prefer git revert to avoid rewriting history. Let me know if you want help with those commands! 😊 |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for your help. Let me check. |
Beta Was this translation helpful? Give feedback.
-
Step-by step:Check your Git status (Optional but recommended):git statusUndo the last commit while keeping changes staged:git reset --soft HEAD~1Verify the result:git status Recommit the changes (or modify them first):If you just want to change the commit message:Make your additional changesgit add .
git commit -m "Your new, correct commit message"& Now Push the new Recommit:git push origin main |
Beta Was this translation helpful? Give feedback.
If you accidentally committed but forgot to add some files, don’t use git reset --hard—this discards your changes completely.
Safe way to undo your last commit but keep your changes:
git reset --soft HEAD~1
What happens:
HEAD~1 moves your branch pointer back one commit.
--soft keeps your changes in the staging area (ready to commit again).
You can now add any missed files and re-commit safely.
Example workflow:
=====================
Undo last commit but keep your changes staged
git reset --soft HEAD~1
Add forgotten files
git add forgotten-file.txt
Commit again
git commit -m "Your improved commit message"
Push (force if needed)
git push --force
===============
Why this is better:
Keeps you…