gitpractice.dev

brewly — Ch.4 · Undo a commit, keep the work

Exercise 2 of 9

Before you start

Moving the branch back

git reset <commit> moves your branch to another commit. HEAD~1 means "one commit before where I am".

The mode decides what happens to the changes you leave behind. --soft keeps them staged. The default, --mixed, keeps them in your files but unstaged. Neither loses work.

Like amending, this rewrites history, so only do it with commits you have not pushed.

git reset HEAD~1
undo the last commit; changes stay in your files
git reset --soft HEAD~1
undo it; changes stay staged
git status
see what is back
git add <file>
stage one part
git commit -m "What you did"
commit it
You can reopen this any time with “Lesson”.

Continue on a desktop

Typing git on a small screen is no fun. Open this page on a laptop or desktop to practise.

Back to the path