b

branching

Create a new local branch and check it out immediately.

Usage: git b feature/new-feature

CLI Command

git config --global alias.b "checkout -b"

.gitconfig Snippet

[alias]
  b = checkout -b

bs

branching

List all local branches.

Usage: git bs

CLI Command

git config --global alias.bs "branch --list"

.gitconfig Snippet

[alias]
  bs = branch --list

co

branching

Check out a branch.

Usage: git co target-branch

CLI Command

git config --global alias.co "checkout"

.gitconfig Snippet

[alias]
  co = checkout

conf

configuration

Open your git config in your default editor so you can easily review and update it. Save any changes you make, then close the text editor when you're finished. This command will not finish until you close the text editor.

Usage: git conf

CLI Command

git config --global alias.conf "config --global -e"

.gitconfig Snippet

[alias]
  conf = config --global -e

cop

branching

Check out a branch by a partial branch name. This function searches for branches containing the provided pattern and checks out the first match.

I name branches with a reference to a work item number, then use the work item number to checkout the right branch. For example, the branch might be called feature/12345-updated-invoice-structure and I can check it out with git cop 12345 or git cop invoice.

From Stack Overflow.

Usage: git cop 12345

CLI Command

git config --global alias.cop "!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f"

.gitconfig Snippet

[alias]
  cop = !f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f

db

branching

Delete a local branch. Use with caution as this permanently deletes the branch, even if it hasn't been merged.

Usage: git db unwanted-branch

CLI Command

git config --global alias.db "branch -D"

.gitconfig Snippet

[alias]
  db = branch -D

dev

branching merging workflow

Merge latest dev branch code into your current branch and update your local dev branch. Assumes you have a branch called dev both locally and on the origin remote. This alias is useful for keeping your feature branches up-to-date with the latest development work.

This alias pulls the latest dev branch from origin, merges it into your current branch, and updates your local dev branch to match origin/dev.

Usage: git dev

CLI Command

git config --global alias.dev "!git pull origin dev && git merge dev && git fetch origin dev:dev"

.gitconfig Snippet

[alias]
  dev = !git pull origin dev && git merge dev && git fetch origin dev:dev

done

workflow branching

Switch from a feature branch to the latest code in dev branch, and delete branches which are fully merged into dev branch. This alias is intended for use after pushing a code-complete feature branch to the server, when ready to start a new feature based on the latest dev branch code.

Usage: git done

CLI Command

git config --global alias.done "!git fetch && git checkout dev && git pull && git merged && git status"

.gitconfig Snippet

[alias]
  done = !git fetch && git checkout dev && git pull && git merged && git status

f

remote

Fetch latest changes from the server and remove references to deleted remote branches.

Usage: git f

CLI Command

git config --global alias.f "fetch --prune"

.gitconfig Snippet

[alias]
  f = fetch --prune

merged

workflow branching

Delete local branches which have been fully merged into dev branch (excluding the dev branch itself). Useful for cleaning up completed feature branches.

From Advanced Git Aliases published by Atlassian.

Usage: git merged

CLI Command

git config --global alias.merged "!f() { git branch --merged dev | grep -v " dev$" | xargs -r git branch -d; }; f"

.gitconfig Snippet

[alias]
  merged = !f() { git branch --merged dev | grep -v " dev$" | xargs -r git branch -d; }; f

pl

remote pull

Pull latest changes. Shorthand for git pull to fetch and merge changes from the upstream branch. Added for consistency with the 2-character ps alias.

Usage: git pl

CLI Command

git config --global alias.pl "pull"

.gitconfig Snippet

[alias]
  pl = pull

ps

remote push

Push commits to a remote branch on the origin remote. If there is no upstream branch on origin, one will be created.

I prefer this alias to running git push because it works regardless of whether or not there is an existing upstream branch.

Usage: git ps

CLI Command

git config --global alias.ps "push -u origin HEAD"

.gitconfig Snippet

[alias]
  ps = push -u origin HEAD

pt

remote push advanced

Push up to a specific commit. This function pushes the current branch up to a specific commit hash. My main use case for this is when making changes to a branch which already has a Pull Request in Azure DevOps. The changes in each push can be reviewed as separate updates to the PR, so I can make a series of commits then push them as multiple logical updates for easier review.

I based this alias on https://coderwall.com/p/hexinq/git-push-up-to-a-certain-commit

Usage: git pt abc123

CLI Command

git config --global alias.pt "!f() { BRANCH=`git symbolic-ref --short HEAD`; git push -u origin ${1}:refs/heads/$BRANCH; }; f"

.gitconfig Snippet

[alias]
  pt = !f() { BRANCH=`git symbolic-ref --short HEAD`; git push -u origin ${1}:refs/heads/$BRANCH; }; f

rename-branch

branching

Rename the current branch.

Usage: git rename-branch new-branch-name

CLI Command

git config --global alias.rename-branch "branch -m"

.gitconfig Snippet

[alias]
  rename-branch = branch -m

s

status workflow

Quick shorthand for git status to quickly check the current state of your working directory and staging area.

Usage: git s

CLI Command

git config --global alias.s "status"

.gitconfig Snippet

[alias]
  s = status

squash

branching merge workflow

Squash commits from the current branch into another branch. This function takes all commits from the current branch and squashes them into a single commit on the target branch. The result is not committed automatically, allowing you to review and write a commit message.

Modified from https://stackoverflow.com/a/37209562/4051181.

Usage: while on 'wip/12345', wanting to squash everything into 'feature/12345', run 'git squash feature/12345'.

Usage: git squash feature/12345

CLI Command

git config --global alias.squash "!f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout ${1}; git merge --squash $BRANCH; }; f"

.gitconfig Snippet

[alias]
  squash = !f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout ${1}; git merge --squash $BRANCH; }; f

unpushed

remote status

View commits on the current local branch which have not yet been pushed to the server.

Usage: git unpushed

CLI Command

git config --global alias.unpushed "cherry -v"

.gitconfig Snippet

[alias]
  unpushed = cherry -v