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 theHEAD
--
is used to remove ambiguity between afile path
and abranch name
--
indicates the following to be afile 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
(state to post-git add
)
Undo the git commit
but keep the changes
git reset
without a--hard
or--soft
moves yourHEAD
to point to the specified commit, without changing any files
git reset HEAD~1
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
git revert
Undo command to back out from commit
git revert
Back out a "specific hash" after commit
git revert commit_sha
Removing commits from history (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 # deleted hash_2 pick commit_hash_3 commit to keep
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 everything (Hard Resets)
Safely revert
to the previous commit
# Create a new branch git checkout -b new_branch_name # push the code to remote with new branch 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 git switch new_branch_name # original state backup
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