Skip to content

Git#

Setup aliases#

git config --global --edit

Reset last commit (keep changes staged)#

git reset --soft HEAD~1

Reset last commit (keep changes unstaged)#

git reset HEAD~1

Reset last commit (discard all changes)#

git reset --hard HEAD~1

Revert last commit#

git revert HEAD

Revert specific commit#

git revert <commit-hash>

Revert without auto-commit#

git revert -n <commit-hash>

Standard merge#

git checkout main
git merge feature

Squash merge#

git checkout main
git merge --squash feature
git commit -m "Add feature (squashed)"

Rebase and merge#

git checkout feature
git rebase main
git checkout main
git merge feature

Stash all changes#

git stash -u

Stash with name#

git stash save "work in progress on header"

List stashes#

git stash list

Apply stash (keep in list)#

git stash apply

Apply stash (remove from list)#

git stash pop

Stash specific files#

git stash -p

Show what would be deleted#

git clean -n

Remove untracked files#

git clean -f

Remove untracked files and directories#

git clean -fd

Untrack file (keep on disk)#

git rm --cached <filename>

Untrack folder (keep on disk)#

git rm -r --cached <folder>

Remove .git folder (Linux/macOS)#

rm -rf .git

Remove .git folder (Windows PowerShell)#

Remove-Item -Recurse -Force .git

Remove .git folder (Windows CMD)#

rmdir /s /q .git

Abort merge#

git merge --abort

Prune remote branches#

git remote prune origin

List tags#

git tag -l

Create tag#

git tag v2.0

Delete remote tag#

git push origin --delete tag v2.0

Pull with rebase#

git pull --rebase

Delete local branch#

git branch -d <branch-name>

Delete remote branch#

git push origin --delete <branch-name>