Speed up your Git workflow with these handy aliases. Each alias can be added via command line or directly to your .gitconfig file.
Get all aliases
Copy or download all aliases as a single block ready to paste into your .gitconfig file. The exported snippet already includes the [alias] header, so you can paste the whole block as-is. If you're adding these entries to an existing [alias] section, remove the first [alias] line before pasting.
[alias]
abort-stash = !git reset --merge
apply-stash = !f() { stash=$(git stash list | grep -i "$1" | head -n 1); if [ -n "$stash" ]; then stash_id=$(echo "$stash" | awk -F: '{print $1}'); echo "Applying $stash"; git stash apply "$stash_id"; else echo "No stash found matching: $1"; fi; }; f
b = checkout -b
bs = branch --list
cdp = !git checkout dev && git pull
co = checkout
com = !git add -A && git commit -m
conf = config --global -e
conflicts = !git ls-files --unmerged | cut -f2 | sort -u
cop = !f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f
cor = !f() { git checkout releases/sprint-$1 && git pull origin releases/sprint-$1; }; f
db = branch -D
dev = !git pull origin dev && git merge dev && git fetch origin dev:dev
done = !f() { branch=${1:-dev}; git fetch && git checkout "$branch" && git pull && git merged && git status; }; f
f = fetch --prune
merged = !f() { git branch --merged dev | grep -v " dev$" | xargs -r git branch -d; }; f
merged-into = !f() { git branch --merged "$1" | grep -v " $1$" | xargs -r git branch -d; }; f
npmauth = !f() { root=$(git rev-parse --show-toplevel); first=1; git ls-files "*/.npmrc" | sed "s|/[^/]*$||" | sort -u | while IFS= read -r dir; do echo "Running vsts-npm-auth in $dir" && cd "$root/$dir" && if [ "$first" = "1" ]; then vsts-npm-auth -F -config .npmrc; first=0; else vsts-npm-auth -config .npmrc; fi; done; }; f
npmci = !f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm ci in {}' && cd {} && npm ci"; }; f
npmi = !f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm i in {}' && cd {} && npm i"; }; f
open = !f() { root=$(git rev-parse --show-toplevel); if command -v grep >/dev/null 2>&1; then file=$(git ls-files | grep -m1 "/[^/]*$1[^/]*$"); else file=$(git ls-files | findstr /R "[^/]*$1[^/]*$" | head -1); fi; if [ -n "$file" ]; then start "" "C:\Program Files\Notepad++\notepad++.exe" "$root/$file"; else echo "No file found matching: $1"; fi; }; f
pl = pull
ps = push -u origin HEAD
pt = !f() { BRANCH=`git symbolic-ref --short HEAD`; git push -u origin ${1}:refs/heads/$BRANCH; }; f
rename-branch = branch -m
s = status
squash = !f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout ${1}; git merge --squash $BRANCH; }; f
start = !git add -A && git apply-stash config
stash-config = !f() { git stash -u -m "$1 config $(date +%Y-%m-%d' '%H:%M)"; }; f
stop = !git checkout -- . && git clean -fd
uncommit = reset --mixed HEAD~1
unpushed = cherry -v
Scenario: You apply a stash but there's a merge conflict. You want to discard all changes so that you can start from a blank slate.
Note that this will discard staged changes, so only use this if you're prepared to discard everything.
Apply a stash by searching for a partial match in the stash message. Instead of remembering stash numbers, you can use part of the stash description to apply the correct stash. For example, git apply-stash config will apply the most recent stash with "config" in its message.
Usage:git apply-stash <partial-name>
CLI Command
git config --global alias.apply-stash "!f() { stash=$(git stash list | grep -i "$1" | head -n 1); if [ -n "$stash" ]; then stash_id=$(echo "$stash" | awk -F: '{print $1}'); echo "Applying $stash"; git stash apply "$stash_id"; else echo "No stash found matching: $1"; fi; }; f"
.gitconfig Snippet
[alias]
apply-stash = !f() { stash=$(git stash list | grep -i "$1" | head -n 1); if [ -n "$stash" ]; then stash_id=$(echo "$stash" | awk -F: '{print $1}'); echo "Applying $stash"; git stash apply "$stash_id"; else echo "No stash found matching: $1"; fi; }; f
Checkout the dev branch and pull the latest changes from the remote. This is a quick way to switch to your main development branch and ensure you have the latest code before starting new work or reviewing changes.
Usage:git cdp
CLI Command
git config --global alias.cdp "!git checkout dev && git pull"
Stage all changes (including new files, modifications, and deletions) and open the commit message editor. This is a quick way to commit all your work from the command line without having to run separate git add and git commit commands.
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.
List all files that currently have merge conflicts. This is helpful during merge or rebase operations to quickly see which files need conflict resolution, without having to parse through the full output of git status.
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.
Checkout a release branch based on a releases/sprint-x naming convention, and pull the latest changes from the remote. Similar to the cdp alias but for the release branch, this quickly switches you to the release branch and ensures you have the most up-to-date code.
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
Switch from a feature branch to the latest code in a base branch (defaults to dev), and delete branches which are fully merged into that 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 branch code. Pass an optional branch name to use a different base branch (e.g. git done main).
Delete local branches which have been fully merged into dev branch (excluding the dev branch itself). Useful for cleaning up completed feature branches.
Delete all local branches that have been fully merged into the specified branch. Similar to the merged alias but allows you to specify any branch name as the target. For example, git merged-into main will delete all branches merged into main.
Run vsts-npm-auth -config .npmrc in all directories containing a .npmrc file. Forces new credentials for the first .npmrc found (useful when multiple directories share the same npm registries), then authenticates the remaining directories without forcing. This is helpful in monorepo setups where multiple packages authenticate against the same Azure Artifacts npm registries.
Run npm ci in all directories that contain a package.json file. This is useful in monorepo setups or projects with multiple npm packages, allowing you to install all dependencies in a single command with clean installs.
Usage:git npmci
CLI Command
git config --global alias.npmci "!f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm ci in {}' && cd {} && npm ci"; }; f"
.gitconfig Snippet
[alias]
npmci = !f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm ci in {}' && cd {} && npm ci"; }; f
Run npm install in all directories that contain a package.json file. Similar to npmci but uses npm install instead of npm ci, allowing package-lock.json to be updated if needed. Useful for updating dependencies across multiple packages in a monorepo.
Usage:git npmi
CLI Command
git config --global alias.npmi "!f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm i in {}' && cd {} && npm i"; }; f"
.gitconfig Snippet
[alias]
npmi = !f() { git ls-files "*/package.json" | sed "s|/[^/]*$||" | sort -u | xargs -I {} sh -c "echo 'Running npm i in {}' && cd {} && npm i"; }; f
Open a file in Notepad++ directly from the Git command line.
Explanation
Open the first file whose filename (not path) contains the specified search term.
Intended usage is for a complete or partial file name - does not match against directory names.
File is opened in Notepad++ and the alias assumes this is installed in C:/Program Files/Notepad++
Tested in powershell, command prompt, and git bash.
Explanation:
root is the file path to the git repository.
file is the file path within the repository for the first file found matching the search term.
Uses grep/findstr to match only against the basename (filename) portion of the path, not directories.
Checks if grep is available and uses it, otherwise falls back to a Windows-compatible approach.
Validates that a file was found before attempting to open it.
Calculates root and file, then starts notepad++.exe and passes the absolute file path in.
Note that it uses start so that the command finishes executing without waiting for notepad++ to close.
Usage:git open ExportFxTransactionCommand.cs
CLI Command
git config --global alias.open "!f() { root=$(git rev-parse --show-toplevel); if command -v grep >/dev/null 2>&1; then file=$(git ls-files | grep -m1 "/[^/]*$1[^/]*$"); else file=$(git ls-files | findstr /R "[^/]*$1[^/]*$" | head -1); fi; if [ -n "$file" ]; then start "" "C:\Program Files\Notepad++\notepad++.exe" "$root/$file"; else echo "No file found matching: $1"; fi; }; f"
.gitconfig Snippet
[alias]
open = !f() { root=$(git rev-parse --show-toplevel); if command -v grep >/dev/null 2>&1; then file=$(git ls-files | grep -m1 "/[^/]*$1[^/]*$"); else file=$(git ls-files | findstr /R "[^/]*$1[^/]*$" | head -1); fi; if [ -n "$file" ]; then start "" "C:\Program Files\Notepad++\notepad++.exe" "$root/$file"; else echo "No file found matching: $1"; fi; }; f
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.
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.
Stages all changes (including new, modified, deleted, and renamed files) and applies the latest stash. Intended usage is when starting a project where configuration changes are stored in a stash. Before starting the application, we want to apply the latest stash containing the word 'config' but keep them separate from the changes under test (which is why they are staged).
Reverts all unstaged changes to tracked files, and delete all untracked files and directories. Intended usage is when stopping a project where configuration changes are stored in a stash. When we stop the application, we want to revert the unstaged changes (which we are expecting to just be the configuration changes) so we have a clean working directory.