It has been a while since I created a new blog post. I was super busy with a new project on my new position. I had some time to spare finally, so I took the opportunity to write about some git aliases that I use everyday which make my life easier on git console.
I generally use Windows operating system for development and I gave following examples accordingly, but the aliases I give here should probably work for other operating systems as well.
Why
I suggest you to learn the git commands in a proper way first before even attempting to use aliases. With proper I mean learning each necessary command with it’s most useful switches. Atlassian and GitHub has good basic documentation for starters. Web is full of tutorials and cheatsheets to learn. But after you learn your way around repositories, it is good to invest some time and create some aliases that can make your interaction with the repositories faster.
How
If you have used git before, you probably have a .gitconfig
file under your %UserProfile%
folder. But even if you do not have, it is easy to create. Head over to First time git setup. I suggest to set at least your user name, e-mail and core editor with the following commands:
1 | git config --global user.name "John Doe" |
The first two are easy enough. The third command actually sets your core editor. I generally use VS Code, but you can set to notepad if it is not installed on your machine.
After the initial setup, you can enter the following command in your command line. This will popup your core editor with your .gitconfig
file to edit.
1 | git config --global --edit |
Aliases
I will go over some aliases I created, ordered by my daily usage of them. Here we go!
Add All and Commit
I like to commit as frequently as possible so I quickly check my status with git status
and then just use the following alias to add all my changes and commit them with a message.
Alias
1 | cm = !git add -A && git commit -m |
Usage
1 | git cm "Changed some stuff." |
Checkout
Helpful for switching between branches.
Alias
1 | co = checkout |
Usage
This command switches your current branch to master
or MyFeatureBranch
1 | git co master |
Checkout Branch
Alias to create a new branch.
Alias
1 | cob = checkout |
Usage
This command creates a new branch named as MyNewBranchName
.
1 | git cob MyNewBranchName |
Checkout and Track Branch
Helpful during code reviews. Sometimes when I review code of colleagues, I prefer to clone their branch and have a closer look to their changes.
Alias
1 | track = checkout --track |
Usage
This command creates a local branch NewFeatureBranch
and sets an upstream to the branch on remote repository. (origin/NewFeatureBranch
)
1 | git track origin/NewFeatureBranch |
Log with Format
Handy to quickly see what has changed recently.
Alias
1 | [log] |
Usage
This command shows the last 3 commits to the repository.
1 | git log -3 |
results with a screen like this:
History
This is nowhere near as what you can see from GitHub interface or any GUI, but it is sometimes useful to quickly see what is going on the repository.
Alias
1 | hist = log --oneline --abbrev-commit --all --graph --decorate --color |
Usage
1 | git hist |
Results with a screen like this:
Final Configuration
The .gitconfig
file looks like this when all these aliases are added:
1 | [user] |
Enjoy these aliases and happy coding!