Git Alias
Git is a powerful version control system with a plethora of commands to streamline your
development workflow. However, typing lengthy commands repeatedly can be time-consuming. Git
provides a solution—aliases. We'll explore various ways to use the
git alias
command, enabling you to create custom shortcuts for common Git
commands and enhance your productivity.
1. Setting a Simple Alias:
To create a simple Git alias, use the git config
command:
bashgit config --global alias.co checkout
This creates an alias co
for the checkout
command. Now, you can use
git co
instead of git checkout
.
2. Creating Aliases with Parameters:
You can create aliases with parameters to make them more versatile. For example:
bashgit config --global alias.lg "log --oneline --graph --all"
This alias, named lg
, displays a compact, graph-based log of all branches. Use
it with:
bashgit lg
3. Chain Commands in an Alias:
Aliases can also execute multiple commands in sequence. For instance:
bashgit config --global alias.sync "fetch --prune origin && pull"
The sync
alias fetches changes from the remote repository, prunes deleted
branches, and pulls the changes into your local branch.
4. Alias for Creating a New Branch and Switching to It:
Combine commands to create a custom alias that streamlines creating a new branch and switching to it:
bashgit config --global alias.nb "checkout -b"
Now, you can create and switch to a new branch with:
bashgit nb feature-branch
5. Listing All Aliases:
To view a list of all your configured aliases, use:
bashgit config --get-regexp alias
This command displays a list of aliases and their corresponding commands.
6. Editing Aliases Directly in Config File:
You can edit the Git configuration file directly to add or modify aliases:
bashnano ~/.gitconfig
Under the alias
section, add your aliases:
plaintextalias co = checkout lg = log --oneline --graph --all
Save the file to apply the changes.
7. Removing an Alias:
If you want to remove an alias, use the --unset
option:
bashgit config --global --unset alias.alias-name
Replace alias-name
with the name of the alias you want to remove.
8. Creating Shell Aliases for Git Commands:
You can also create shell aliases for Git commands directly in your shell configuration file
(e.g., .bashrc
or .zshrc
):
bashalias gs="git status"
alias ga="git add"
alias gc="git commit"
This allows you to use shortened versions of Git commands in your terminal.
9. Global and Local Aliases:
Git aliases can be configured globally or locally within a specific repository. Use the
--global
flag for global aliases and omit it for local ones:
bashgit config --global alias.g "log --oneline --graph --all"
git config alias.l "log --oneline"
10. Creating a Shortcut for Force Push:
To create an alias for force-pushing to the remote repository:
bashgit config --global alias.fp "push --force-with-lease"
Now, you can force-push with:
bashgit fp
11. Aliases with Options:
You can create aliases with options to customize their behavior. For example:
bashgit config --global alias.undo "reset --soft HEAD^"
This alias, named undo
, soft resets the last commit. Use it with:
bashgit undo
12. Alias for Showing the Last Commit:
To create an alias for showing the details of the last commit:
bashgit config --global alias.last "log -1 --stat"
Now, you can view the last commit with:
bashgit last