Git aliases is a useful way to save your keyboard from typing a lot.
git status |
My alias for finishing code review |
---|---|
You can create an alias type in yor terminal:
$ git config --global alias.s status
This command add alias "s" for git command "status".
$ git s
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Git stores aliases in the global .gitconfig
located in the root folder of current user:
$ less ~/.gitconfig
If you skip
--global
argument, the alias will created inly for the current repository.
In this cases they are stored in.gitconfig
file in the root folder of your repository.
You always can edit .gitconfig
manually with your favorite text editor:
$ vi ~/.gitconfig
[alias]
l = log --oneline
s = status
c = commit
cm = commit -m
ri = rebase --interactive
oops = commit --amend --no-edit
initial-commit = commit --allow-empty -m \"Initial commit\"
...
You can add any bash script as an alias.
For example, you can make a curl request to https://gitignore.io for create a .gitginore
file.
This alias gets content for the .gitignore
file and stores it in file .gitignore
in current folder
git config --global alias.ignore '!gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ > .gitignore;}; gi'
Let's create a .gitconfig
file for a Swift project using content from here: https://www.toptal.com/developers/gitignore/api/swift,xcode
$ git ignore swift,xcode
Normally, when I finish reviewing a Pull Request, I check out main
branch and remove the PR branch.
So, I tired to type it manully all the time 🫠
$ git branch # to copy current branch name to pasteboard
* feature/login
main
$ git checkout main
# Remove PR branch
$ git branch --delete --force feature/login
In action |
---|
Feel free to steal
[alias]
l = log --oneline
r = rebase
b = branch
s = status
c = commit
co = checkout
cp = cherry-pick
cm = commit -m
fa = fetch --all
ri = rebase --interactive
rzt = reset --hard HEAD
oops = commit --amend --no-edit
back = reset --soft head~1
get-head = rev-parse --short HEAD
initial-commit = commit --allow-empty -m \"Initial commit\"
ignore = "!gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ > .gitignore;}; gi"
finish-code-review = "!finish() { CURRENT=$(git rev-parse --abbrev-ref HEAD); git checkout main; git branch --delete --force $CURRENT; }; finish"
# Interactive checkout. Install "fzf" before: `brew install fzf`.
# Credits: https://www.amitmerchant.com/how-to-checkout-git-branches-interactively/
ci = !git checkout $(git branch -a | fzf | xargs)