Concursos

How to undo a commit in git

Undoing a commit in Git depends on whether you want to keep or discard the changes. Here are several ways to undo a commit:

1. Undo the last commit but keep the changes:

git reset --soft HEAD~1
  • Effect: Removes the last commit but keeps the changes staged.
  • Use case: If you committed too soon but still want to edit files or add more changes.

2. Undo the last commit and unstage changes:

git reset --mixed HEAD~1
  • Effect: Removes the last commit and unstages the changes (they stay in your working directory).
  • Use case: If you want to redo the commit but need to modify what was staged.

3. Undo the last commit and discard changes permanently:

git reset --hard HEAD~1
  • Effect: Completely removes the last commit and deletes any changes.
  • Use case: If the commit was a mistake and you don’t need those changes.

4. Undo a commit that has been pushed (without rewriting history for others):

git revert <commit_hash>
  • Effect: Creates a new commit that undoes the changes of a previous commit.
  • Use case: If you already pushed the commit and need to undo it safely.

5. Undo a pushed commit by rewriting history (dangerous for shared branches):

git reset --hard HEAD~1
git push --force
  • Effect: Deletes the commit and forces the branch to a previous state.
  • Warning: This rewrites history and should be used cautiously, especially on shared branches.

Mostrar mais

Artigos relacionados

Faça seu comentário:

Esse site utiliza o Akismet para reduzir spam. Aprenda como seus dados de comentários são processados.

Botão Voltar ao topo

Adblock detectado

Ajude a manter este trabalho prestigiando nossos patrocinadores.
%d blogueiros gostam disto: