Git undo

Aus SDQ-Wiki
Zur Navigation springen Zur Suche springen

git undo operations

This page gives you a very brief overview on different possibilities to undo changes with git. There is also a detailed tutorial by Atlassian.

git revert

Undoes an old commit by adding a new commit with appropriate changes. The right choice if you want to document the undo and necessary if the commit to be undone was already pushed.

git revert commit-id

git reset

Can undo local commits or unstage files.

git reset file-or-commit-id

permanently undoes a local commit without leaving any traces or possibilities to recover. Only to be used if the commit was not pushed and will never be needed again.

git reset file-not-path

unstages a file that was previously added to the staging area but not yet commited.

The extent to which the reset is performed can be controlled. It grows for the following options and defaults to mixed.

--soft

to only reset the head

--mixed

to also reset the staging area

--hard

to also reset the files

git checkout

A checkout can also be used to permanently undo uncommited changes to files that are already tracked.

git checkout file-or-folder

git clean

Permanently undoes uncommited changes to files that are not yet tracked.

git clean optional-file-or-folder

temporary undo and redo

You can put changes that you did not yet commit (but that you may have added to the index or not) temporarily aside by stashing them. More details can be found in the chapter on stashing of the Pro Git book.

git stash

and reapply them after a checkout or pull:

git stash apply

(If you also stashed indexed changes you have to specify the option --index to also apply these indexed changes.) If the reapplication worked you should remove the stashed changes from the stack:

git stash drop

You can use

git stash pop

to apply and drop at once.