Skip to content

Instantly share code, notes, and snippets.

@x0000ff
Last active April 22, 2024 08:41
Show Gist options
  • Save x0000ff/0ee176932d4f8ecd40af2abe886a354e to your computer and use it in GitHub Desktop.
Save x0000ff/0ee176932d4f8ecd40af2abe886a354e to your computer and use it in GitHub Desktop.
Git aliases

What is Git alias

Git aliases is a useful way to save your keyboard from typing a lot.

git status My alias for finishing code review
git status finish-code-review

Let's create one

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.

Edit aliases manually

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\"
        ...

😯 Any bash script could be alias

You can add any bash script as an alias.

Alias for generating a .gitignore file

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'

From https://docs.gitignore.io/install/command-line

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

Alias for "Finish code review"

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
finish-code-review

My aliases

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment