DEV Community

Satton
Satton

Posted on

How to set Alias for Git command?

Introduction

Describes how to set an alias for a Git command.
It all started when I happened to be using Git in my work and was curious about alias settings.

About Alias

This setting is used to omit commands used in Git.

Example

  • Before Abbreviation
git status
Enter fullscreen mode Exit fullscreen mode
  • After Abbreviation
git s
Enter fullscreen mode Exit fullscreen mode

Setup Procedure

Commands to set Alias settings

Perform Alias configuration for the following commands

git log --graph --all --color --pretty='%x09%h %s %Cred%d%Creset %C(green)- %ad%Creset c:%cn:%ce a:%an:%ae' --date=format:'%Y/%m/%d %H:%M:%S%z'
Enter fullscreen mode Exit fullscreen mode

This command makes the git log easier to read.
Examples of use are as follows.

% git log --graph --all --color --pretty="%x09%h %s %Cred%d%Creset %C(green)- %ad%Creset c:%cn:%ce a:%an:%ae" --date=format:"%Y/%m/%d %H:%M:%S%z"
*       4c001c1 Update READMD.md  (HEAD -> work, origin/work) - 2022/06/19 15:28:05+0900 c:masuwa:xxxxxxxxxxx@gmail.com a:masuwa:xxxxxxxxxxx@gmail.com 
*       20ac8ba Initial commit  (origin/main, origin/HEAD, main) - 2022/06/19 13:15:40+0900 c:GitHub:noreply@github.com a:masuwa9028:105219153+masuwa9028@users.noreply.github.com
%
Enter fullscreen mode Exit fullscreen mode

Alias Setting

Setting File

Range to be reflected Configuration file to be edited
Entire system /etc/gitconfig
User ~/.gitconfig or ~/.config/git/config
Repository Repository root directory/.git/config

※This time, modify ~/.gitconfig.

Setting

  • Modify ~/.gitconfig.

Add the following to ~/.gitconfig.

[alias]
  tree = log --graph --all --color --pretty='%x09%h %s %Cred%d%Creset %C(green)- %ad%Creset c:%cn:%ce a:%an:%ae' --date=format:'%Y/%m/%d %H:%M:%S%z'
Enter fullscreen mode Exit fullscreen mode
  • Check if the command is available.

Verify that the git tree command is working.

% git tree
*       4c001c1 Update READMD.md  (HEAD -> work, origin/work) - 2022/06/19 15:28:05+0900 c:masuwa:xxxxxxxxxxx@gmail.com a:masuwa:xxxxxxxxxxx@gmail.com 
*       20ac8ba Initial commit  (origin/main, origin/HEAD, main) - 2022/06/19 13:15:40+0900 c:GitHub:noreply@github.com a:masuwa9028:105219153+masuwa9028@users.noreply.github.com
%
Enter fullscreen mode Exit fullscreen mode

Reference

【git】aliasの設定方法
2.7 Git の基本 - Git エイリアス
ダブルクォートを含むコマンドをそのままaliasにすると失敗しがち

Top comments (0)