Git Commands Cheatsheet
Table of contents
Open Table of contents
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>
Revert 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>