Skip to content

Instantly share code, notes, and snippets.

@sauvikdolui
Created January 6, 2022 12:12
Show Gist options
  • Save sauvikdolui/cdb017c80e0737f7e52cfa077bd7d3cd to your computer and use it in GitHub Desktop.
Save sauvikdolui/cdb017c80e0737f7e52cfa077bd7d3cd to your computer and use it in GitHub Desktop.

Undoing chages in git

AMENDMENT (git commit --amend)

  • Fix typo in last commit message
git commit --amemd -m 'Your new commit message
  • Add more changes
# do more changes
git add file1 file2
git commit --amend
  • Only change files, no change in commit message
git add file1 file2 ...
git commit --amend --no-edit

CHECKOUT/SWITCH(git checkout)

  • Reset changes done in WORKING AREA, if you have already stagged the changes(git add), you need to reset.
  git checkout -- file
  • Checkout to a specific commit(deattached head).
git checkout SHACommit

NOTE: Do not change anything here, should be useful for debuging purpose, avoid creating new commits here.

RESET (git reset)

  • Moving file from staging area to working area.
  git reset filename
  • Deleting previous commits
A -> B -> C -> D

Suppose there was a bug which was introduced in commit C. You can move back to commit B which is most stable version while

  • keeping changes done in both commit C & D in working area
git reset --soft B
  • deleting every change done in both commit C & D
git reset --hard B

Now you can do the required changes and and commit. You may need to push using -f flag.

REVERT (git revert)

  • Reverts changes done in a specific commit, this reversion is added as a separate commit on top of current HEAD.
git revert SHA_of_your_commit_you_want_to_revert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment