

Instead, it reverts the effects of a certain commit, effectively undoing it.

However, the command doesn't delete any commits. Using the "git revert" command is one possibility to undo a previous commit. when you notice that your changes were wrong, when you introduced a bug, or simply when the customer has decided he doesn't want this anymore. Sometimes you'll want to undo a certain commit. Therefore, Git has no chance to restore this kind of changes.Īlways keep this in mind when discarding local changes. This is because they have never been saved in your repository. This tells Git to replace the files in your working copy with the "HEAD" revision (which is the last committed version), discarding all local changes.ĭiscarding uncommitted changes cannot be undone. If you need to discard all current changes in your working copy and want to restore the last committed version of your complete project, the "git reset" command is your friend: $ git reset -hard HEAD However, if you use it with the HEAD reference and the path to a file, it will discard any uncommitted changes in that file. You already know that the "checkout" command is mainly used to switch branches. To restore a file to its last committed version, you use the "git checkout" command: $ git checkout HEAD file/to/restore.ext These are the times when you want to discard these changes and start fresh with the last committed version. Replacing it with an amended version will cause problems.Ĭhanges are called "local" when they haven't been committed, yet: all the modifications that are currently present in your working directory are "local", uncommitted changes. However, after publishing the original commit on a remote, other people might already have based new work on this commit. If you're the only person who had this commit, doing this is safe. (b) You should never "amend" a commit that has already been published / pushed to a remote repository! This is because "amend" effectively produces a completely new commit object in the background that replaces the old one.Older commits can't be modified with "amend". (a) It can only be used to fix the very last commit.

However, you'll need to keep the following things in mind when using it: Using the "amend" option is a great little helper that you'll come to appreciate yourself very quickly.
