Git commands

There are some Git commands I often but not always use. Sometimes when I need these commands I need to search for them so I decided to store them here.

Assume remote name is origin.

Delete a branch

Delete a local branch

git branch -d <branch_name>

The -d option stands for --delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.

git branch -D <branch_name>

The -D option stands for --delete --force, which deletes the branch regardless of its push and merge status, so be careful using this one!

Delete a remote branch

git push origin --delete <branch_name>

or git push origin :<branch_name>

Push code to remote and delete local branch

git push -d origin <branch_name>

Rename branch

Rename

git branch -m <new-branch-name>

rename on a different branch

git branch -m <current-branch-name> <new-branch-name>

Push the new name

git push origin <new-branch-name>

New branch

Create and checkout new branch

git checkout -b <new-branch-name>

Push the new local branch to a remote

git push origin <new-branch-name>