SteelReed

Search

Search posts, questions…

DevOps & CloudQuestion 4 / 35

Junior

How do you undo changes in Git: how do reset, revert, amend and reflog differ?

Short answer

git revert creates a new commit that undoes another, safe on shared branches. git reset moves the branch pointer back, rewriting history, so use it only on unpushed commits. If you lose a commit, git reflog almost always saves you.

yesno, last commit onlyno, several commitsoopsWant to undo a commitPushed to a shared branch?git revert: inverse commitgit commit --amendgit reset--soft/--mixed/--hardgit reflog, reset back

3 points to say:

  1. Revert for shared history; reset, amend only for unpushed commits.
  2. --soft keeps index and files, --mixed (default) resets the index, --hard overwrites the working tree.
  3. Reflog recovers commits until they expire and gc runs, but not uncommitted changes.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.