Skip to content
Discussion options

You must be logged in to vote

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…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by navinightwork
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
CA Q&A
Labels
None yet
3 participants