git cheatsheet
Worktrees
Create a new worktree with a new branch
git worktree add <path>/<branch>
Create a new worktree and checkout it to existing branch
git worktree add <path> <branch>
Remove 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>
Fetch Tags
git fetch --tags
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?
Reset current branch to the remote state.
git fetch && git reset --hard @{u}
Subtle difference in rebase command
When you want to rebase your local branch onto a remote one remember to do it like this:
git rebase origin/<remote_branch_name>
instead of:
git rebase origin <remote_branch_name>
The second one will not work as expected. It will switch you to whatever origin
points to and then will apply your local changes on top of it.
Stash
Stash only unstaged changes
git stash push --keep-index
- Worktrees
- Create a new worktree with a new branch
- Create a new worktree and checkout it to existing branch
- Remove worktree at path
- List your worktrees
- Tags
- Branches
- Change current branch name
- Change upstream branch
- Remove all branches that are already merged to master.
- Reset current branch to the remote state.
- Subtle difference in rebase command
- Stash