Bash
Git Revert
Undo commits and revert.
By EZ4Code Team
gitrollback
Code
#!/bin/bash
# revert: create reverse commit (safe)
git revert <commit-hash>
git revert HEAD # Undo last commit
git revert HEAD~2 # Undo last 2 commits
git revert --no-commit HEAD # Undo but don't auto-commit
# Undo multiple commits
git revert HEAD~3..HEAD # Undo last 3 commits
git revert --no-commit HEAD~3..HEAD # Undo all at once
# reset: move HEAD (dangerous)
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset --mixed HEAD~1 # Undo commit, keep changes in working dir (default)
git reset --hard HEAD~1 # Undo commit, discard all changes
# checkout: restore file
git checkout HEAD~1 -- file.txt # Restore file to historical version
git checkout <hash> -- file.txt # Restore to specified commit
# restore: (new command)
git restore --staged file.txt # Unstage
git restore --source=HEAD~1 file.txt # Restore to historical version
# reflog: view operation history
git reflog
git reset --hard HEAD@{2} # Restore to 2 steps ago
# clean: delete untracked files
git clean -n # Preview
git clean -f # Delete untracked files
git clean -fd # Delete untracked files and dirs
git clean -x # Include .gitignore files
# Recover lost commits
git fsck --lost-found
git show <dangling-commit-hash>Explanation
git revert creates a reverse commit for safe undo; reset moves HEAD; reflog recovers mistakes; clean removes untracked files.
More Bash Snippets
Variables and Arrays in Bash
Assign variables, use command substitution, and work with arrays in Bash.
File Operations
File and directory management.
Text Processing
Text processing with grep, sed, awk.
Loops
for and while loops.
Conditionals
if and case conditional statements.
Functions
Function definition and parameters.