Crafting Git Aliases to Enhance Daily Workflow
Git aliases are a powerful feature that allows you to reduce time spent with Git. So, instead of typing: git add .
, git commit -m "[COMMIT_MESSAGE]"
, and git push
, you will be able to type git [YOUR_ALIAS]
, and that's equivalent to the mentioned commands.
Let's delve into this simple concept and explore some game-changing aliases together.
Creating Git Alias
There is a fancy command git config
that allows to set alias globally. Take a look:
git config --global alias.wip '!f() { git commit -am "$1" && git add . && git push; }; f'
Explanation:
git config --global alias.wip
creates a global alias namedwip
.!f() { ... }; f
defines a shell function that executes the desired commands.git commit -am "$1"
commits all changes with the message provided as an argument.git add .
stages all changes.git push
pushes the changes to the remote repository.
Now, in your terminal, you need to type:
git wip "[COMMIT_MESSAGE]"
Here is the result:
All will work. Just for the sake of understanding: the git config
command adds a record inside a file called .gitconfig
, which is created for a specific git user
. The paths will be different based on the operating system:
- Linux/Mac:
/home/your_username/.gitconfig
(or~/.gitconfig
) - Windows:
C:\Users\Your_Username\.gitconfig
After using this command, you'll see that a new record is added:
[alias]
wip = "!f() { git commit -am \"$1\" && git add . && git push; }; f"
You can go to this file and modify it by yourself, or use the command. It's up to you. The result will be the same.
Sometimes when using an IDE like Visual Studio Code, you have the option to choose the terminal type. Make sure you are using
Powershell
orGit Bash
. Typing this command in theCmd
terminal will not work.
Summary
You can create even more complex aliases, such as ones for git rebase
or for renaming n
commits. The sky is the limit because any git
command can be aliased with a dedicated name.