HomeToolsAbout a20k

Undo Things in Git

Resets

# undo the git commit ## Changes still exist in the working tree(the project folder) + the index (--cached) git reset --soft HEAD~1 # undo git commit + git add ## Changes still exist in the working tree git reset HEAD^ --mixed # Like you never made these changes to the codebase ## Changes are gone from the working tree. git reset HEAD^ --hard

Undo git add (Unstaging)

git restore (recommended)

Equivalent to the git checkout -- file_or_all, except it does not delete the files.

# remove everything out of the index git restore --staged . # remove specific file out of the index git restore --staged path_to_file

Checkout to HEAD

# all files to HEAD git checkout @ -- . # specified file to HEAD git checkout @ -- path_to_file # can omit branch name to reference HEAD git checkout -- path_to_file
  • @ is used to refer to the HEAD
  • -- is used to remove ambiguity between a file path and a branch name
    • -- indicates the following to be a file path

git clean

# Delete file out of the index permanently git clean # See dry run output (unexecuted output) of git clean git clean -n # Interactive git clean git clean -i

Force delete files or directories

# Force delete untracked file git clean -f # Force delete untracked directories (this cannot be reverted) git clean -df

git rm

Remove the files from index and file system

git rm --cached dir_or_file

Undo git commit after git add

Using git restore --staged

git restore --staged file_name

Undo git commit

Revert a single file to previous commit

# revert to origin git checkout origin/branch_name -- path_to_file # revert to specific commit version for specific file git checkout commit_hash -- path_to_file # revert to previous commit count for specific file git checkout HEAD~commit_count -- path_to_file

Removing commits (dangerous)

Using Interactive Rebase

git rebase -i HEAD~commit_count

Enter the interactive mode and delete the commits you don't want to apply from the history

# before pick commit_hash_1 initial commit pick commit_hash_2 commit to remove pick commit_hash_3 commit to keep # after pick commit_hash_1 initial commit pick commit_hash_3 commit to keep

git revert

Undo command to back out from commit

git revert

Back out a specific hash after it was committed

git revert commit_sha

Undo the undo git revert (re-applying the removed commit)

Un-do a git revert that un-did the commit using cherry-pick

git cherry-pick reverted_commit_sha

Undo all (Hard Resets)

How to safely revert to the previous commit

# Create new branch git checkout -b new_branch_name # push the code to remote git push # go back to the original branch git switch previous_branch # Reset hard local to previous commit git reset --hard commit_hash # Fallback to newly created remote branch when needed

When need to restore safely to previous state

git reset --hard origin/new_branch_name

Safely Nuking Local Repo and Reset to Remote

git commit -a -m "Saving my work, just in case" git branch my-saved-work git fetch origin git reset --hard origin/same_branch_name
  • Save current work before nuking by creating a new branch from current
  • Fetch from remote origin the original commit
  • Set your local branch to match the the remote just pulled down

Reset Current Local master Branch to a previous master commit

git checkout master~1 git checkout -b new_master git branch -D master git branch -mv new_master master
  • checkout previous local commit
  • create a new_master branch that will be uploaded to the remote
  • delete the old master branch on remote
  • make new_master branch the local master branch

Dangerously Hard Reset

Reset

git reset is a command to fix uncommitted mistakes

  • unstages the index and leaves your working directory untouched.

Unstage all files

git reset .

Reset to previous commit

git reset HEAD~1

Identical to git reset HEAD, if you want to unstage the file, you can use rm --cached. you unstage the entire file from index, but you don't remove it from the working dir

git rm --cached added_file_to_unstage

Reset Types

git reset --hard commit_sha
  • --hard resets everything. Use this to undo merged. your current directory would be exactly as it would if you had been following that branch all along. the working directory and the index are changed to that commit.
git reset --soft commit_sha
  • --soft does not reset the working tree nor the index. It only moves the HEAD pointer. This leaves your current state with any changes different than the commit you are switching to in place in your directory, and “staged” for committing. If you make a commit locally but haven’t pushed the commit to the git server, you can reset to the previous commit, and recommit with a good commit message.
git reset --mixed commit_sha
  • --mixed resets the index, but not the working tree. so the changes are still there, but are unstaged and would need to be git added. we use this sometimes if we committed more than we meant to. we can back out the commit with git reset --mixed, add the things that we want to commit and just commit those
© VincentVanKoh