Git is a powerful tool, but it can sometimes be overwhelming with the number of commands required to perform common tasks. If you’ve ever found yourself typing out long, complex Git commands and wondered if there’s a faster way to get things done, you’re not alone. One way to streamline your workflow and reduce repetitive typing is by using Git aliases. These are shorthand commands that allow you to perform lengthy Git operations with just a few characters.
In this post, we’ll explore some useful Git aliases that can help you maximize your productivity, speed up common workflows, and maintain a clean Git history.
How To Add Aliases To Your Git Config File
To start using Git aliases, you need to add them to your
.gitconfig
file. This file is typically located in your home directory, and it contains various configurations for your Git setup, including user details and aliases.Here’s how to add aliases:
- Open the .gitconfig file:
- On Linux/MacOS, the
.gitconfig
file is typically located in your home directory (~/.gitconfig
). - On Windows, it is located at
C:Users<YourUsername>.gitconfig
.
- On Linux/MacOS, the
- Edit the .gitconfig file: You can manually add aliases to the
[alias]
section. If this section doesn’t already exist, simply add it at the top or bottom of the file. Below is an example of how your.gitconfig
file should look once you add the aliases that we will cover in this post:[alias] # --- Branching --- co = checkout cob = checkout -b br = branch # --- Working Directory Status --- st = status df = diff # --- Commit & Push --- amod = "!f() { git add -u && git commit -m "$1" && git push; }; f" acp = "!f() { git add -A && git commit -m "$1" && git push; }; f" # --- Stash --- ss = stash ssd = stash drop # --- Reset / Cleanup --- nuke = reset --hard resetremote = !git reset --hard origin/main # --- Rebase Helpers --- rbc = rebase --continue rba = rebase --abort rbi = rebase -i # --- Log / History --- hist = log --oneline --graph --decorate --all ln = log --name-status # --- Fetch & Sync --- fetch = fetch --all --prune pullr = pull --rebase up = !git fetch --prune && git rebase origin/$(git rev-parse --abbrev-ref HEAD) cp = cherry-pick
- Save and close the file: Once you’ve added your aliases, save the file, and your new aliases will be available the next time you run Git commands in your terminal.
- Test the aliases: After saving your
.gitconfig
file, you can use your new aliases immediately. For example, try usinggit co
to switch branches orgit amod "your commit message"
to commit your changes.
- Open the .gitconfig file:
Explanation of the Aliases
I find these to be very helpful in my day-to-day work as a web developer. Here are some explanations of the aliases that I have added:
Branching
co = checkout
When switching between branches, this alias saves you from typing
git checkout <branch_name>
. With co
, switching is as simple as:git co <branch_name>
cob = checkout -b
Creating and switching to a new branch is easier with this alias. Instead of
git checkout -b <new_branch_name>
, simply use: git cob <new_branch_name>
br = branch
If you need to quickly list all branches, whether local or remote, this alias is a fast way to do so:
git br
Working Directory Status
st = status
One of the most frequently used commands in Git,
git status
shows the current state of your working directory. By aliasing it as st
, you save time while checking what’s been staged or modified:git st
df = diff
If you want to view the changes you’ve made compared to the last commit, use
df
for a quick comparison:git df
Commit and Push
amod = “!f() { git add -u && git commit -m ”$1” && git push; }; f”
For quick commits, this alias allows you to add modified and deleted files (but not new untracked files), commit, and push all in one command! It’s perfect for when you want to keep things simple and focus on committing changes:
git amod "Your commit message"
acp = “!f() { git add -A && git commit -m ”$1” && git push; }; f”
Similar to
amod
, but this version adds all changes, including untracked files, commits them, and pushes to the remote. It’s ideal when you’re working with a full set of changes:git acp "Your commit message"
Stash
ss = stash
When you’re in the middle of something but need to quickly save your uncommitted changes to come back to later,
git stash
comes to the rescue. With this alias, you can stash your changes with ease:git ss
ssd = stash drop
Sometimes, after stashing, you may want to drop the stashed changes. With
ssd
, you can easily discard a stash:git ssd
Reset / Cleanup
nuke = reset –hard
This alias will discard all local changes and reset your working directory to the last commit. It’s especially helpful when you want to start fresh or undo your recent changes:
git nuke
resetremote = !git reset –hard origin/main
When your local branch has diverged from the remote and you want to match it exactly, this alias will discard local changes and reset to the remote branch. It’s a lifesaver when you need to restore your local branch to match the remote:
git resetremote
Rebase Helpers
rbc = rebase –continue
If you’re in the middle of a rebase and have resolved any conflicts,
git rebase --continue
lets you proceed. The rbc
alias lets you continue the rebase without typing the full command: git rbc
rba = rebase –abort
If something goes wrong during a rebase and you want to abandon the process,
git rebase --abort
will undo all changes from the rebase. This alias makes it quick and easy to abort a rebase:git rba
rbi = rebase -i
For an interactive rebase, where you can squash or reorder commits,
git rebase -i
is an essential command. The rbi
alias will save you from typing the whole command:git rbi
Log / History
hist = log –oneline –graph –decorate –all
For a good-looking, concise view of your commit history, this alias combines the best of
git log
. It shows commits in a graph format, with decoration to show branch names and tags, all while keeping the output short:git hist
ln = log –name-status
When you need to see what files were changed in each commit (with their status: added, modified, deleted),
git log --name-status
is invaluable. The ln
alias helps you inspect commit changes more easily:git ln
Fetch and Sync
fetch = fetch –all –prune
Fetching updates from all remotes and cleaning up any deleted branches with
git fetch --all --prune
is essential for keeping your remotes organized. The fetch
alias makes this task a single command:git fetch
pullr = pull –rebase
When removing changes from the remote, rebase are often better than merges. This keeps your history linear and avoids unnecessary merge commits. The
pullr
alias performs a pull with a rebase:git pullr
up = !git fetch –prune && git rebase origin/$(git rev-parse –abbrev-ref HEAD)
This alias is a great shortcut if you want to quickly rebase your current branch onto its remote counterpart. It first fetches the latest updates from the remote and prunes any deleted remote-tracking branches, ensuring your local references are clean and up to date. Then it rebases your branch onto the corresponding remote, keeping your history in sync:
git up
cp = cherry-pick
Cherry-picking allows you to apply a specific commit from another branch to your current branch. This alias makes it easier to run:
git cp <commit-hash>
Final Thoughts
By setting up these Git aliases, you can reduce repetitive typing, speed up your development process, and make your Git usage more efficient. Once you’ve incorporated a few into your routine, they become second nature. Don’t hesitate to experiment and add your own based on the commands you use most. Put these in your
.gitconfig
file today and start enjoying the benefits of a more productive workflow!Source: Read MoreÂ