✏ Collection of my most used Git commands
Initialise Repo
git init
Remote vs Local
Create local copy of a remote repo:
git clone <repourl>
Merge remote master/main branch with local master:
git pull origin master
Push to remote from local:
git push origin <branch>
Set origin alias:
git remote add origin <url>
Branches
Show all branches:
git branch -a
Switch to branch:
git checkout <branch>
Create new branch:
git branch <branch>
Create new branch and switch to branch:
git checkout -b <branch>
Delete a branch (needs to be merged with master branch):
git branch -d <branch>
Delete an unmerged branch:
git branch -D <branch>
Stage/Commit
Log commits (without/with short ID)
git log
git log --oneline
Show tracked/modified/staged files:
git status
Add tracked file / all files to staging area:
git add <filename>
git add .
Remove file / all files from staging area:
git rm --cached <filename>
git rm --cached . -r
Commit with message:
git commit -m "message"
Rewind to certain commit:
git checkout <shortID>
Undoing things
My favourite image to illustrate the situation:
(image credit and solution strategies)
Remove commit from history:
git revert <shortID>
Cancel revert (in case of conflict):
git revert --abort
Erase commits (changes are still visible in editor/can be re-commited as one bundled commit)
git reset <shortID>
git reset HEAD^ --soft
Permanently erase commits:
git reset <shortID> --hard
Merging
Merge branch into current branch:
git merge <branch>
Undo latest merge (keep changes in editor):
git reset --merge
Permanently undo latest merge:
git reset --hard ORIG_HEAD
✏ Thanks for reading!
I do my best to thoroughly research the things I learn, but if you find any errors or have additions, please leave a comment below, or @ me on Twitter. If you liked this post, I invite you to subscribe to my newsletter. Until next time 👋
✏ Previous Posts
You can find an overview of all previous posts with tags and tag search here: