【Git Snippets】Git aliases

300 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

The Git snippet collection contains a variety of short tips and tricks for all currently maintained versions of git. It includes most commonly-used commands and covers various use-cases in the form of simplified documentation, complete with multiple examples.

Short code snippets for all your development needs.

Use the command below to create aliases, replacing <alias> with the name of the alias and <command> with the command to be aliased:

$ git config --global alias.<alias> <command>

Additionally, you can use git config --global -e to open the git configuration file in the git text editor and add many aliases all at once.

$ git config --global -e

Example 1

$ git config --global alias.br branch

Example 2

$ git config --global alias.undo "reset HEAD~1"

Example 3

$ git config --global alias.cm "commit -m 'update on `date +%Y-%m-%d`'"

Useful aliases

[alias]
  co = checkout
  cob = checkout -b
  coo = !git fetch && git checkout
  br = branch
  brd = branch -d
  st = status
  aa = add -A .
  unstage = reset --soft HEAD^
  cm = commit -m
  amend = commit --amend -m
  fix = commit --fixup
  undo = reset HEAD~1
  rv = revert
  cp = cherry-pick
  pu = !git push origin `git branch --show-current`
  fush = push -f
  mg = merge --no-ff
  rb = rebase
  rbc = rebase --continue
  rba = rebase --abort
  rbs = rebase --skip
  rom = !git fetch && git rebase -i origin/master --autosquash
  save = stash push
  pop = stash pop
  apply = stash apply
  rl = reflog

List all git aliases

Prints a list of all git aliases.

  • Use git config -l to list all variables set in the configuration file.
  • Use the pipe operator (|) to pipe the output and grep alias to only keep aliases.
  • Use the pipe operator (|) to pipe the output and sed 's/^alias.//g' to remove the alias. part from each alias.

Example

$ git config -l | grep alias | sed 's/^alias.//g' 

🧠Wish you can benefit from this article.
🔥 And welcome to leave your thoughts in the comments section that we can discuss and share ideas with each other.