My Git Cheatsheet
Updated
•3 min readThis is my Git cheatsheet. I share it in case it's of any use to anyone else.Stash Commands
Stashing
Basic Stashing
git stash # Stash changes quickly
git stash save "Description" # Stash changes with a description
Viewing Stashes
git stash list # List all stashes
git stash show stash@{n} # Show changes in a specific stash
git stash show -p stash@{n} # Show full diff of a specific stash
Applying Stashes
git stash apply # Apply the most recent stash
git stash apply stash@{n} # Apply a specific stash
git stash pop # Apply and remove the most recent stash
git stash pop stash@{n} # Apply and remove a specific stash
Managing Stashes
git stash drop stash@{n} # Remove a specific stash
git stash clear # Remove all stashes
Branching from a Stash
git stash branch new-branch # Create a new branch from the latest stash
Note: Replace n with the stash index number (0 is most recent).
Rebase Commands
Basic Rebasing
git rebase <base> # Rebase current branch onto <base>
git rebase -i <base> # Interactive rebase
Rebase Operations
git rebase --continue # Continue rebase after resolving conflicts
git rebase --abort # Abort the rebase operation
git rebase --skip # Skip the current commit and continue
Updating a Branch
git pull --rebase origin <branch> # Pull changes and rebase instead of merge
Rebase onto Another Branch
git rebase --onto <newbase> <oldbase> <branch>
Preserving Merges
git rebase -p <upstream> # Rebase preserving merge commits
Note: Replace <base>, <branch>, <newbase>, <oldbase>, and <upstream> with appropriate branch or commit references.
Reset Commands
Soft Reset
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --soft <commit> # Reset to specific commit, keep changes staged
Mixed Reset (Default)
git reset HEAD~1 # Undo last commit, keep changes unstaged
git reset <commit> # Reset to specific commit, keep changes unstaged
Hard Reset
git reset --hard HEAD~1 # Undo last commit, discard all changes
git reset --hard <commit> # Reset to specific commit, discard all changes
git reset --hard origin/<branch> # Reset to remote branch, discard all local changes
Reset Single File
git reset <file> # Unstage a file
git reset --hard <file> # Discard changes to a file (use with caution)
Reset to a Clean State
git reset --hard HEAD # Discard all local changes in working directory
Undo a Reset
git reflog # View history of HEAD changes
git reset --hard HEAD@{n} # Reset to a previous state (n is the reflog index)
Note: Be cautious with --hard resets as they discard changes permanently. Always commit or stash important changes before using --hard.
Replace <commit> with a commit hash, branch name, or relative reference (e.g., HEAD~3).


