Jump to content

Git/別名

From mediawiki.org
< Git
This page is a translated version of the page Git/aliases and the translation is 55% complete.

Git は多くの便利なオプションを持つ強力なツールです。 コマンドとオプションの集合体に別名を作成できることをご存知ですか? もう長いシェルのワンライナーを書く必要はないでしょう。 下記は便利な別名を集めたものです。

別名をセットアップする方法

~/.gitconfig 内に [alias] 節を追加します。 各行は <タブ文字>キー = 値 の書式です。

Below is a snippet of the [alias] section of a .gitconfig file

[alias]
    short-name = actual (long?) command

すべて

一度にすべてを取得したい方のために、このページにあるすべての別名を以下に示します:

[alias]
	amend     = commit --amend -a
	br        = branch
	branch-cleanup = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -n 1 git branch -d; }; f"
	co        = checkout
	ds        = diff --staged
	di        = diff
	fetchall  = fetch -v --all
	lg = log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
	l = ! git lg -n25
	log-me    = !UN=$(git config user.name)&& git log --author="\"$UN\"" --pretty=format:'%h %cd %s' --date=short
	log-nice  = log --graph --decorate --pretty=oneline --abbrev-commit
	panic     = !tar cvf ../git_panic.tar *
	st        = status
	wdiff     = diff --word-diff=plain

amend

[alias]
	amend = commit --amend -a
git amend

With Gerrit you may have to correct a patchset. This usually means editing files and then having to correct the previous commit message. The workflow would be something like:

$ git review -d 1234 // gerrit changeset id
<edit files>
$ git add <files>
$ git commit --amend
<update commit-msg>

If you only changed files (not added new ones), the last two commands can be grouped by using git commit --amend -a.

br

[alias]
	br = branch
  • git br foo --track origin/foo
  • git br -D some/old/branch

branch-cleanup

[alias]
        branch-cleanup = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -n 1 git branch -d; }; f"

This deletes all branches-references that are fully merged into master (or some other branch, if you specify).

co

[alias]
	co = checkout
git co master

ds

[alias]
	ds = diff --staged
git ds

A plain git diff shows the diff between the (unstaged) working copy and the staged working copy. In most cases this is the same as the diff between the working copy and the last committed revision of the current branch (HEAD).

However, if you have already done git add and then made some more changes, then the changes at time of last git add no longer show up in git diff. Use git diff --staged to create a diff between the already staged (but uncommitted) changes and the current HEAD.

di

[alias]
	di = diff
git di

fetchall

[alias]
	fetchall = fetch -v --all
git fetchall

In this case, both origin and gerrit are the same, good enough for the above screenshot.

You can also use git remote update

lg

[alias]
	lg = log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
	l = ! git lg -n25
Example : git lg (default, with pager) or git l (last 25 only)

log-me

The author name is case-sensitive.

[alias]
	log-me  = !UN=$(git config user.name)&& git log --author="\"$UN\"" --pretty=format:'%h %cd %s' --date=short
git log-me -n20

log-nice

[alias]
	log-nice = log --graph --decorate --pretty=oneline --abbrev-commit
git log-nice -n20

panic

[alias]
	panic = !tar cvf ../git_panic.tar *

Sometimes, you might have done something wrong in git. You think you've lost your commits, or something like that. Chances are, the information is still there — so the best course of action is to make an immediate backup, before you risk actually losing data.

st

[alias]
	st = status
git st

wdiff

[alias]
	wdiff    = diff --word-diff=plain
git wdiff