Skip to content

Getting Confident with Git Part 3: Undoing

Polina Soshnin edited this page Dec 24, 2015 · 5 revisions

The tl;dr

  • Amend a commit: git commit --amend
  • Include files you forgot to add: git add newfile && git commit --amend -no-edit
  • Unstage a file: git reset newfile.md
  • Delete all changes since last commit: git checkout .
  • Remove your last commit: git reset --soft HEAD^

####Amending Commits

git commit --amend

This will open your editor with the commit message already populated. You can now edit as needed, and quit and save when ready. Git will then create a new commit that reuses the code from the previous version of our commit, but containing the updated commit message.

Including files you forgot to add to the previous commit:

  • git add newfile
  • git commit --amend --no-edit

This will take the currently-staged changes and incorporate them into the previous commit, reusing the existing commit message thanks to the --no-edit flag.

How to unstage a file

$ git status --short
A  newfile.md
$ git reset newfile.md
$ git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	newfile.md

Using checkout to undo any changes

If we ever make changes to a file but then realize we actually don't want those changes, we can use checkout to revert things to how they looked in the most recent commit on our current branch. This resets to how the code looked in the last commit.

  • Undo all changes since last commit: git checkout .
  • Undo changes to one file since last commit: git checkout package.json

###Undoing a Commit

  • If you ever want to remove a commit entirely: git reset --soft HEAD^