Working with branches

Creating

git checkout -b $branchname
git push origin $branchname --set-upstream

Creates a new branch locally then pushes it.

Getting from remote

git fetch origin
git checkout --track origin/$branchname

Gets a branch in a remote.

Delete local remote-tracking branches

git remote prune origin

Deletes origin/* branches in your local copy. Doesn't affect the remote.

List existing branches

git branch --list

Existing branches are listed. Current branch will be highlighted with an asterisk.

List merged branches

git branch -a --merged

List outdated branches that have been merged into the current one.

Delete a local branch

git branch -d $branchname

Deletes the branch only if its changes are merged into the current branch.

Delete branch forcefully

Force delete

git branch -D $branchname

Safe delete

git branch -d $branchname

-D is --delete --force: it removes unmerged branches too.

Delete remote branch

git push origin --delete $branchname

Works for tags, too!

Get current commit ID

git rev-parse HEAD

Reset branch and remove all changes

git reset --hard

Undo commits to a specific commit

git reset --hard $commit_id
# Now push safely to your branch
git push --force-with-lease
# Or push brutally to your branch
git push --force

Worktrees

Rename, delete, or undo

0 Comments for this cheatsheet. Write yours!