Skip to content

Instantly share code, notes, and snippets.

@musemby
Last active April 20, 2017 15:16
Show Gist options
  • Save musemby/853e44357113cdad025bff3e3f643948 to your computer and use it in GitHub Desktop.
Save musemby/853e44357113cdad025bff3e3f643948 to your computer and use it in GitHub Desktop.
Git commands to fix common blunders

Resetting HEAD to some commit in time do either:

  1. git reset HEAD@{index}
  2. git reset --hard <commit-index>

Add to a commit a file change you had left out:

  • git add <file-name>/ <.>
  • git commit --amend // Use this to just ammend the last commit message

Reverse a commit to master and move into a new branch

(while in master do:)

  • git branch -b new-branch
  • git reset HEAD~ --hard //remove that commit from master
  • git checkout new-branch

Reverse commit to the wrong branch do either:

  1. Use stash
    • git reset HEAD~ --soft // reverses the commit but leaves the changes available
    • git stash
    • git checkout correct-branch
    • git stash pop
    • git commit
  2. Use cherry-pick
    • git reset HEAD~ --hard
    • git checkout correct-branch
    • git cherry-pick <commit-hash-from master>

Git diff staged changes

  • git diff --staged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment