Git/Aliase

From mediawiki.org
< Git
This page is a translated version of the page Git/aliases and the translation is 66% complete.
Outdated translations are marked like this.

Git ist ein mächtiges Werkzeug mit vielen nutzvollen Optionen. Wusstest du, dass du einen Alias für einen Befehl und einer Reihe von Optionen erstellen kannst? Du musst keine langen Shell-Einzeiler mehr schreiben. Unten ist eine Sammlung einiger nützlicher Aliase.

Wie man einen Alias einrichtet

Füge in deiner ~/.gitconfig einen [alias]-Abschnitt hinzu. Jede folgende Zeile hat dann das <tab>Schlüssel = Wert-Format.

Unten ist ein Ausschnitt des [alias]-Abschnitts einer .gitconfig-Datei:

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

Alles

Wenn du alles auf einmal haben möchtest, findest du hier eine Sammlung aller Aliase auf dieser Seite:

[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
Beispiel
git amend

Mit Gerrit musst du eventuell ein Patchset korrigieren. Dies bedeutet normalerweise, Dateien zu bearbeiten und dann die vorherige Commit-Nachricht korrigieren zu müssen. Der Ablauf wäre in etwa so:

$ 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
Beispiel
  • 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
Beispiel
git co master

ds

[alias]
	ds = diff --staged
Beispiel
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
Beispiel
git di

fetchall

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

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

Hinweis Hinweis:

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
Beispiel

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
Beispiel
git log-me -n20

log-nice

[alias]
	log-nice = log --graph --decorate --pretty=oneline --abbrev-commit
Beispiel
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
Beispiel
git st

wdiff

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