Git Spicker

H. Högl, 2012, 2015, 2018, 2019

URL: http://hhoegl.informatik.hs-augsburg.de/hhwiki/Git

Ich habe git in der Version 2.17.1 verwendet. Das Kommando git --version gibt die Version aus.

1   Hilfe

Wenn man auf der Kommandozeile git eingibt, dann bekommt man u.a. eine gruppierte Übersicht zu den verfügbaren Subkommandos:

start a working area (see also: git help tutorial)
  clone      Clone a repository into a new directory
  init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
  add        Add file contents to the index
  mv         Move or rename a file, a directory, or a symlink
  reset      Reset current HEAD to the specified state
  rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
  bisect     Use binary search to find the commit that introduced a bug
  grep       Print lines matching a pattern
  log        Show commit logs
  show       Show various types of objects
  status     Show the working tree status

grow, mark and tweak your common history
  branch     List, create, or delete branches
  checkout   Switch branches or restore working tree files
  commit     Record changes to the repository
  diff       Show changes between commits, commit and working tree, etc
  merge      Join two or more development histories together
  rebase     Reapply commits on top of another base tip
  tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
  fetch      Download objects and refs from another repository
  pull       Fetch from and integrate with another repository or a local branch
  push       Update remote refs along with associated objects

Zu allen Git Subkommandos gibt es Manual-Seiten. Diese bekommt man immer durch folgende Eingaben

git <subcommand> --help
git help <subcommand>

Mit git help -a bekommt man eine Liste aller Subkommandos.

Mit git help -g bekommt man einen Liste der Git Guides, auch Concept Guides genannt:

attributes   Defining attributes per path
everyday     Everyday Git With 20 Commands Or So
glossary     A Git glossary
ignore       Specifies intentionally untracked files to ignore
modules      Defining submodule properties
revisions    Specifying revisions and ranges for Git
tutorial     A tutorial introduction to Git (for version 1.5.1 or newer)
workflows    An overview of recommended workflows with Git

Einen Guide betrachtet man mit git help <concept>.

2   Konfigurieren

git config -l    # show configuration variables defined in .git/config
user.email=Hubert.Hoegl@hs-augsburg.de
user.name=Hubert Hoegl
push.default=simple
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.srv.url=ssh://hhoegl.informatik.hs-augsburg.de:2222/data/tgit/00repos/oss
remote.srv.fetch=+refs/heads/master:refs/remotes/srv/master
branch.master.remote=srv
branch.master.merge=refs/heads/master

# set some variables
git config --global user.name <your name>
git config --global user.email <your e-mail>
git config --global core.editor <your preferred editor, eg. vi>
git config --global sendemail.smtpserver <your smtp server>
git config --global sendemail.smtpserverport <your smtp server port>
git config --global sendemail.smtpuser <your user name>
git config --global sendemail.smtppass <your password>

All possible variables are described in man page ``git config --help``.

3   Initialisieren

git init
git add <file>
git add .

Dateien ignorieren durch .gitignore.

4   Status

git status
git status -uno         # don't show untracked files

5   Commit

git commit -a               # stage all changed files (-a)
git commit -a -s            # sign-off commit message (-s, --signoff)
git commit -a -s -m <msg>   # commit message <msg>

6   Log

git log
git log --pretty=oneline -3     # on one line, max. 3 commits, see 'git log --help'

7   Änderungen herausfinden

Alle Änderungen von allen Commits im Patch-Format auf die Standardausgabe ausgeben:

git log -p         # in early days this was 'git whatchanged'

Aktuelle Änderungen des letzten Commit

git show           # identisch mit git show HEAD

Änderungen von einem bestimmten Commit ausgeben:

git show 1931ec4d8592cd5599771dd65f1a5d0dfc7237cb

Man kann die lange ID des Commit (ein SHA-1 Hash) auch abkürzen, solange sie eindeutig ist. Im konkreten Beispiel würde auch folgendes funktionieren:

git show 1931ec4

Welche Commits sind noch nicht mit push auf das entfernte Repository übertragen worden?

git log remotes/origin/hhoegl..hhoegl
commit 9330d1aba6a7a9d64795e4af906c1a995e5eddd9
Author: Hubert Hoegl <Hubert.Hoegl@hs-augsburg.de>
Date:   Thu Apr 26 10:13:38 2012 +0200

    GPIO > 15 aktiviert

Vorsicht: Wenn man die beiden Argumente links und rechts vom .. vertauscht, funktioniert es nicht.

8   Tag

In folgendem Beispiel wird dem Commit 1931ec4 (abgekürzter SHA-1) die Markierung "release-1" gegeben:

git tag release-1  1931ec4

9   Branch

git branch <branch-name>      # create new branch...
git checkout <branch-name>    # and switch to the new branch

oder in einem Schritt:

git checkout -b <branch-name>

10   Merge

git checkout master           # switch to "receiving" branch
git pull                      # fetch and merge changes
git merge <other-branch>      # merge <other-branch> into master

11   Push

git push <remote> <local-branch>
git push <remote> <local-branch>:<remote-branch>

12   Patches

Normalerweise erstellt git für jeden Commit einen eigenen Patch.

Einen einzigen Patch für alle Commits zwischen dem aktuellen Entwicklungsbranch und dem Branch master erstellt man so:

git format-patch master --stdout > my.patch

Man kann auch zusätzlich die Option -s (sign-off) verwenden. Mit -o <dir> gibt man das Verzeichnis an, in das der Patch abgelegt werden soll, wenn man nicht auf Standardausgabe ausgibt.

In der Schreibweise

git format-patch master..develop

werden zwei Branches angegeben, zwischen denen die Patches erzeugt werden.

Patch anwenden

git apply --check my.patch    # just check, don't apply
git apply my.patch            # apply w/o sign-off
git apply --stat my.patch     # print patch status
git am --signoff < my.patch   # apply with sign-off ("apply mailbox")

Letzteres Kommando führt zu einer "Signed-off-by" Meldung bei git log. Die sieht so aus:

Signed-off-by: Your Name <your@email.com>

Man kann auch noch die Option --whitespace=strip angeben.

Der Name und die Email-Adresse können in den Umgebungsvariablen GIT_COMMITTER_NAME und GIT_COMMITTER_EMAIL abgelegt werden.

13   Commit Meldungen nachträglich ändern

Die Spitze ("tip") des aktuellen Zweiges ("branch") ändern.

git commit --amend -m "New commit message"

Änderungen, die weiter zurück liegen, werden mit git rebase gemacht. Möchte man den vorletzten Commit bearbeiten, muss man sich mit folgendem Kommando die letzten zwei Commits besorgen:

git rebase -i HEAD~2

Danach landet man in einem Editor mit folgendem Text:

pick f58e28b Ein paar Aenderungen in .config
pick b71ec7a GPIO > 15 aktiviert

# Rebase 4ad3a02..b71ec7a onto 4ad3a02
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

Wenn man die zwei Zeilen mit "pick" löscht und aus dem Editor herausgeht, dann erscheint "Nothing to do". Wenn man eine der Zeilen von "pick" auf "reword" ändert, dann kann man die Commit Meldung abändern.

14   Archivierung

git archive -l
git archive --prefix=glunz/ HEAD | gzip >glunz-20080628.tar.gz
git archive --prefix=glunz/ v0.1 | gzip >glunz-0.1.tar.gz

15   Grafische Werkzeuge

Git GUIs

Repository browser

  • gitk
  • gitg

16   Umgebungsvariable

Eine Liste aller Variablen findet man in man git. Hier sind einige gebräuchliche angegeben:

  • PREFIX
  • HOME
  • GIT_EXEC_PATH
  • GIT_PAGER
  • GIT_EDITOR
  • GIT_DIR
  • GIT_WORK_TREE
  • GIT_AUTHOR_NAME
  • GIT_AUTHOR_EMAIL
  • GIT_AUTHOR_DATE
  • GIT_COMMITTER_NAME
  • GIT_COMMITTER_EMAIL
  • GIT_COMMITTER_DATE

GitSpicker (zuletzt geƤndert am 2019-05-09 12:10:05 durch HubertHoegl)