G2Labs Grzegorz Grzęda
My git aliases
April 18, 2023
When embarking on the journey of code development, one tool that becomes indispensable is Git. It’s like a magic diary that keeps a record of all your changes and organizes your project’s history. But typing out full commands in the console can sometimes feel tedious. That’s where Git aliases come into play—they’re like nicknames for those longer commands you frequently use. They save time and reduce the chance of errors.
Let’s say you’re working on a project and you want to check the current status of your files. Normally, you’d type git status
. But with an alias, you can simply type git st
. Here’s how you can set this up and some more examples to make your coding life easier:
Status Check: Instead of typing
git status
, you can just usegit st
.To set up this alias, run the following command in your console:
1
git config --global alias.st "status"
Commit Changes: To save a snapshot of your changes with a message, you might use
git commit -m "Your message"
. With an alias, you can shorten this togit cm "Your message"
.Set it up like this:
1
git config --global alias.cm "commit -m"
Switching Branches: The command to switch branches in Git is
git checkout <branch-name>
. An alias could begit co <branch-name>
.Here’s the command to create the alias:
1
git config --global alias.co "checkout"
Creating and Switching Branches: If you want to create a new branch and switch to it in one go, you’d normally use
git checkout -b <branch-name>
. A handy alias for this isgit cob <branch-name>
.Create this alias by typing:
1
git config --global alias.cob "checkout -b"
Pulling Changes: To update your local repository to the newest commit, execute
git pull
. An alias? Trygit pl
.Set it up with:
1
git config --global alias.pl "pull"
Pushing Changes: When you’re ready to send your local branch commits to the remote repository, you’d use
git push
. An alias can make itgit ph
.To create this alias, enter:
1
git config --global alias.ph "push"
Force Pushing Changes: Sometimes you need to overwrite the remote repository with your local changes using
git push --force
. This is risky but can be aliased togit phf
.Here’s how to alias it:
1
git config --global alias.phf "push --force"
Stylized Log: Looking at the log of changes can be made easier and more informative with
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
. An alias might begit lg
.Set this alias with:
1
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
I especially like the
log
alias, which produces comprehensive output:
By setting up aliases, you can customize your Git experience to suit your workflow. To create an alias, you use the git config --global
command followed by alias.<your-alias>
and then the Git command you want to shorten. The --global
flag means that the alias will be available across all your Git projects. If you want an alias to be specific to a single project, omit the --global
flag and run the command within the project’s directory.
Remember, while git push --force
(aliased to git phf
) is powerful, it should be used with caution as it can overwrite changes in your remote repository.
Get creative and build a suite of aliases that fits your coding style!
I configured my git aliases as following:
Alias | Meaning |
---|---|
git st | git status |
git cm "<message>" | git commit -m "<message>" |
git co <name> | git checkout <name> |
git cob <name> | git checkout -b <name> |
git pl | git pull |
git ph | git push |
git phf | git push --force |
git lg | git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -- |