b
Create a new local branch and check it out immediately.
git b feature/new-feature
CLI Command
git config --global alias.b "checkout -b"
.gitconfig Snippet
[alias]
b = checkout -b
Speed up your Git workflow with these handy aliases. Each alias can be added via command line or directly to your .gitconfig file.
Create a new local branch and check it out immediately.
git b feature/new-feature
git config --global alias.b "checkout -b"
[alias]
b = checkout -b
List all local branches.
git bs
git config --global alias.bs "branch --list"
[alias]
bs = branch --list
Check out a branch.
git co target-branch
git config --global alias.co "checkout"
[alias]
co = checkout
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.
git conf
git config --global alias.conf "config --global -e"
[alias]
conf = config --global -e
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.
git cop 12345
git config --global alias.cop "!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f"
[alias]
cop = !f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f
Delete a local branch. Use with caution as this permanently deletes the branch, even if it hasn't been merged.
git db unwanted-branch
git config --global alias.db "branch -D"
[alias]
db = branch -D
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.
git dev
git config --global alias.dev "!git pull origin dev && git merge dev && git fetch origin dev:dev"
[alias]
dev = !git pull origin dev && git merge dev && git fetch origin dev:dev
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.
git done
git config --global alias.done "!git fetch && git checkout dev && git pull && git merged && git status"
[alias]
done = !git fetch && git checkout dev && git pull && git merged && git status
Fetch latest changes from the server and remove references to deleted remote branches.
git f
git config --global alias.f "fetch --prune"
[alias]
f = fetch --prune
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.
git merged
git config --global alias.merged "!f() { git branch --merged dev | grep -v " dev$" | xargs -r git branch -d; }; f"
[alias]
merged = !f() { git branch --merged dev | grep -v " dev$" | xargs -r git branch -d; }; f
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.
git pl
git config --global alias.pl "pull"
[alias]
pl = pull
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.
git ps
git config --global alias.ps "push -u origin HEAD"
[alias]
ps = push -u origin HEAD
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
git pt abc123
git config --global alias.pt "!f() { BRANCH=`git symbolic-ref --short HEAD`; git push -u origin ${1}:refs/heads/$BRANCH; }; f"
[alias]
pt = !f() { BRANCH=`git symbolic-ref --short HEAD`; git push -u origin ${1}:refs/heads/$BRANCH; }; f
Rename the current branch.
git rename-branch new-branch-name
git config --global alias.rename-branch "branch -m"
[alias]
rename-branch = branch -m
Quick shorthand for git status to quickly check the current state of your working directory and staging area.
git s
git config --global alias.s "status"
[alias]
s = status
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'.
git squash feature/12345
git config --global alias.squash "!f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout ${1}; git merge --squash $BRANCH; }; f"
[alias]
squash = !f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout ${1}; git merge --squash $BRANCH; }; f
View commits on the current local branch which have not yet been pushed to the server.
git unpushed
git config --global alias.unpushed "cherry -v"
[alias]
unpushed = cherry -v