# 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
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
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
git commit
(state to post-git add
)Undo the git commit
but keep the changes
git reset
without a --hard
or --soft
moves your HEAD
to point to the specified commit, without changing any filesgit 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
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
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
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
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
git checkout master~1 git checkout -b new_master git branch -D master git branch -mv new_master master
new_master
branch the local master branch