Git Commands Cheatsheet
Generate SSH Keys#
Generate SSH keys using the RSA algorithm.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
After this command, two files will be generated in the following location by default: ~/.ssh/id_rsa:
id_rsa(your private key)id_rsa.pub(your public key)
To copy the public key to clipboard, use the following command:
cat ~/.ssh/id_rsa.pub | pbcopy
Add or change Origin URL#
# Add a new Origin
git remote add origin <url>
# Change the existing Origin
git remote set-url origin <new-url>
# View existing origins
git remote -v
Cherry Pick#
cherry-pick is a command that allows you to pick a specific commit from one branch to another.
git cherry-pick <commit_hash>
Git Rebase#
rebase is a command that allows you to reapply commits on top of another base tip.
# Rebase commits from the current branch on top of the selected branch
git rebase <branch_name>
# Enable interactive mode for more control
git rebase -i <branch_name>
# Continue the rebase process after resolving conflicts
git rebase --continue
# Abort the rebase process
git rebase --abort
Git Undo Last Commit#
# undo the commit but keep the changes staged
git reset --soft HEAD~1
# undo the commit and unstage the changes (recommended)
git reset HEAD~1
# completely discard the commit and all changes
git reset --hard HEAD~1
# use force push to push reverted commit
git push -f origin <branch-name>
Reset to a specific commit#
# Revert to a specific commit and keep the changes staged
git reset --soft <commit_hash>
# Revert to a specific commit and unstage the changes (recommended)
git reset <commit_hash>
# Revert to a specific commit and completely discard the commit and all changes
git reset --hard <commit_hash>
# Use force push to push reverted commit
git push -f origin <branch-name>
Revert a specific commit#
git revert is a command that undoes the changes from a specific commit by creating a new commit that reverses them.
The key thing to understand: it doesn't delete or rewrite history. Instead, it adds a new commit on top that cancels out whatever the target commit did.
# View commit history with 5 latest commits
git log --oneline -n 5
# Pull the latest changes from the remote repository
git pull origin <branch-name>
# Revert a specific commit
git revert <commit_hash>
# Revert the last commit
git revert HEAD
# Skips the commit message editor and uses the default message
git revert <commit_hash> --no-edit
# Revert a specific commit without creating a new commit.
# Changes will be in staged state and need to be committed manually.
# Use git status to check the changes.
git revert <commit_hash> --no-commit
# Abort the revert process
git revert --abort
# check the changes
git diff <commit_hash>
# Push the reverted commit
git push origin <branch-name>
Differences between revert and reset:
git revert: keeps history intact and adds a new commit. Safe to use on shared/public branches because you're not changing existing commits, just adding to them.git reset: moves the branch pointer backward and can erase commits from history. Dangerous on shared branches because it rewrites what others may have already pulled.
The rule of thumb: use revert when the commit has already been pushed and shared with others; use reset only for local commits you haven't shared yet.
Revert merge request with a new merge request#
# 1. Get the merge commit hash
git log --oneline --merges
# 2. Make a new branch off the target branch (e.g. main)
git checkout main
git pull origin main
git checkout -b revert-feature-x
# 3. Revert the merge commit — note the -m 1
git revert -m 1 <merge_commit_hash>
# 4. Push and open a new MR
git push origin revert-feature-x
-m 1 is used to specify the merge commit to revert.
A merge commit has two parents, so git can't guess which side to treat as "the version to keep."
-m 1 says "keep the first parent" — for a feature merged into main, the first parent is main, so this undoes the feature's changes while preserving main's line of history.