Como Voltar Um Commit-Git Trick Developers Love
To go back one commit in Git, the safest default is to use git revert if the commit was already shared, or git reset if the commit is still local and you want to rewrite history. In practice, git revert creates a new commit that undoes the old one, while git reset moves your branch pointer back to an earlier commit and can remove commits from the visible history.
What "going back" means
The right command depends on whether the commit is private or already pushed to a remote repository. Git's own documentation explains that revert records a new commit that reverses the changes, whereas reset moves references and history pointers to another commit. That distinction matters because rewriting shared history can confuse teammates and break synchronization.
- Use revert for commits that are already pushed or shared.
- Use reset for local commits you have not shared yet.
- Use reflog when you need to find a commit that is no longer visible in normal history.
Fast commands
If you want the shortest practical answer, these are the most common commands people use to undo one commit. The exact choice depends on whether you want to preserve history, remove the commit entirely, or keep the file changes in your working tree.
| Goal | Command | Effect |
|---|---|---|
| Undo a shared commit safely | git revert <commit-hash> |
Creates a new commit that negates the target commit. |
| Move branch back and keep changes staged | git reset --soft <commit-hash> |
Moves HEAD back, keeps file changes in the staging area. |
| Move branch back and keep changes unstaged | git reset --mixed <commit-hash> |
Moves HEAD back, keeps file changes in the working tree. |
| Delete commit and discard file changes | git reset --hard <commit-hash> |
Moves HEAD back and discards uncommitted content. |
Recommended workflow
The safest workflow is to identify the commit, choose the undo method, and verify the result before pushing anything else. Git documentation and common Git guides consistently recommend revert for public history and reset for local cleanup.
- Find the commit hash with
git log --oneline. - Decide whether the commit is local or already pushed.
- Run
git revert <hash>for shared history, orgit resetfor local history. - Check the result with
git statusandgit log --oneline. - Push only after confirming the branch is in the state you want.
Best command by scenario
For a solo project, developers often use reset because it is fast and direct. For a team project, revert is usually the better choice because it protects other clones from history mismatch and keeps collaboration predictable.
In Git, the safest undo is usually the one that does not surprise anyone else working on the same branch.
Practical examples
Suppose your last commit introduced a bug, but the branch is already on GitHub and teammates may have pulled it. In that case, git revert HEAD is the cleanest fix because it adds a new commit that undoes the bad one without rewriting the branch.
Suppose you committed the wrong files locally and have not pushed yet. In that case, git reset --soft HEAD~1 lets you step back one commit while preserving the changes so you can correct them and recommit immediately.
Suppose you want the branch to match an older commit exactly, including removing later changes. In that case, git reset --hard <older-hash> does that, but it should be used carefully because it can destroy uncommitted work.
Common mistakes
The most common mistake is using hard reset on a shared branch and then forcing others to recover from broken history. Another common mistake is using revert when you actually want to edit the latest local commit, which can create unnecessary extra commits.
- Do not use
git reset --hardcasually on branches used by other people. - Do not confuse
HEAD~1with the commit hash itself; it means "one commit before HEAD." - Do not forget that revert creates a new commit rather than deleting the old one.
- Do not force-push rewritten history unless your team has agreed to it.
Why teams prefer revert
Team workflows usually favor revert because it is reversible, auditable, and easy to review in pull requests. Industry practice has long treated history preservation as a reliability feature, especially on release branches and main branches where many people collaborate.
When a repository is shared, the question is not only "How do I remove this commit?" but also "How do I avoid breaking everyone else's clone?" That is why revert is the standard answer for public history and reset is the standard answer for local cleanup.
Checklist before you act
Before undoing a commit, confirm whether the commit is local, pushed, merged, or part of a release branch. That one decision determines whether you should preserve history or rewrite it.
- Check whether anyone else has pulled the branch.
- Inspect the commit with
git log --oneline. - Choose revert for shared history.
- Choose reset for local-only changes.
- Verify the final state before pushing.
Summary table
The quickest way to remember the difference is this: revert adds an undo commit, while reset moves the branch. That single distinction explains most Git confusion around "going back one commit."
| Situation | Best choice | Why |
|---|---|---|
| Commit already pushed | git revert |
Protects shared history. |
| Commit only local | git reset |
Lets you clean up quickly. |
| You need the files back for editing | git reset --soft |
Preserves staged changes. |
| You need the repo exactly as before | git reset --hard |
Removes later changes completely. |
FAQ
Final guidance
If you need to "voltar um commit" in a shared repository, use git revert. If the commit is still local and you want a clean rewrite, use git reset with the level of caution that matches how much history or data you are willing to lose.
What are the most common questions about Como Voltar Um Commit Git Trick Developers Love?
How do I undo the last commit?
If the last commit is local and you want to keep the changes, use git reset --soft HEAD~1. If you want to discard the commit but keep the file edits in your working directory, use git reset --mixed HEAD~1. If the commit is already pushed, use git revert HEAD instead, because it preserves shared history.
How do I undo a commit already pushed?
Use git revert <commit-hash> to create a new commit that cancels the earlier one. This is the standard choice for public branches because it avoids rewriting history that other developers may already have pulled.
How do I go back to an older commit?
Use git reset --hard <commit-hash> only if you are sure the branch is local or disposable. If you need to keep collaboration safe, revert the commits instead of moving the branch pointer backward.
What is the safest way to undo a commit?
The safest way is usually git revert, because it preserves history and creates a new commit that cancels the old one.
Does reset delete my files?
git reset --soft does not delete file changes, git reset --mixed keeps them in the working tree, and git reset --hard can discard them.
Can I undo a commit after pushing?
Yes, and the recommended method is usually git revert, not reset, because revert avoids rewriting shared branch history.
How do I find the commit hash?
Use git log --oneline to list recent commits in a compact format, then copy the hash for the commit you want to undo.
Can I undo multiple commits at once?
Yes, you can revert a range of commits or reset to an earlier point, but the safer option on shared branches is usually multiple revert commits rather than history rewriting.