git cheatsheet
Worktrees
Creates a new worktree with a new branch:
git worktree add <path>/<branch>
Creates a new worktree and checkouts it to existing branch:
git worktree add <path> <branch>
Remove a worktree at path:
git worktree remove <path>
List your worktrees:
git worktree list
Tags
Create a tag on last commit with name:
git tag <name>
Push a single tag to remote:
git push origin <tag_name>
Branches
Change current branch name:
git branch -m <new_name>
Change upstream branch:
git branch <branch_name> -u <your_new_remote>/<branch_name>
The remote branch needs to be present on the remote, so if you changed the local branch name, first do git push origin HEAD
.
Remove all branches that are already merged to master. Note: also removes local master branch:
git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d
grep -v '^\*'
- makes sure that currently checked out branch is not deleted. Is is even possible to remove currently checked out branch?
Stash
Stash only unstaged changes:
git stash push --keep-index