Benutzer-Werkzeuge

Webseiten-Werkzeuge


git

Dies ist eine alte Version des Dokuments!


Git

Git repository klonen (SSH & HTTPS):

> git clone git@github.com:torvalds/linux.git
> git clone https://github.com/Masterflitzer/HelloWorld.git

Der neue defaultBranch Name wurde auf GitHub und GitLab zu main geändert. Die Änderung erfolgt lokal wie folgt:

> git config --system init.defaultBranch main

Es gibt 3 Konfigurationsmöglichkeiten:

  • –system schreibt in die Datei /etc/gitconfig
  • –global schreibt in die Datei ~/.gitconfig
  • –local schreibt direkt in das aktuelle repo

Git Konfiguration

schönes git log

git log - pretty formats

git log --graph --format='%C(yellow)%h%C(auto)%d%C(yellow):%Creset %s %C(brightblue)(%cr) %C(brightmagenta)<%an>'

beispielhafte gitconfig für den alias

.gitconfig
[include]
	path = .gitconfig.d/.gitalias
[core]
	abbrev = 16
[log]
	abbrevCommit = true
.gitconfig.d/.gitalias
[alias]
	l = log --format='%C(yellow)%h%C(auto)%d%C(yellow):%Creset %s %C(brightblue)(%cr) %C(brightmagenta)<%an>'
	la = l --all
	lag = l --all --graph
	last = l --all --graph -7
	lg = l --graph
$ git l
$ git lg

gitattributes

.gitattributes
# Common settings that generally should always be used with your language specific settings
 
# Auto detect text files and perform LF normalization
# https://www.davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
*          text=auto
 
#
# The above will handle all files NOT found below
#
 
# Documents
*.bibtex   text diff=bibtex
*.doc           diff=astextplain
*.DOC           diff=astextplain
*.docx          diff=astextplain
*.DOCX          diff=astextplain
*.dot           diff=astextplain
*.DOT           diff=astextplain
*.pdf           diff=astextplain
*.PDF           diff=astextplain
*.rtf           diff=astextplain
*.RTF           diff=astextplain
*.md       text
*.tex      text diff=tex
*.adoc     text
*.textile  text
*.mustache text
*.csv      text
*.tab      text
*.tsv      text
*.txt      text
*.sql      text
 
# Graphics
*.png      binary
*.jpg      binary
*.jpeg     binary
*.gif      binary
*.tif      binary
*.tiff     binary
*.ico      binary
# SVG treated as an asset (binary) by default.
*.svg      text
# If you want to treat it as binary,
# use the following line instead.
# *.svg    binary
*.eps      binary
 
# Scripts
*.bash     text eol=lf
*.fish     text eol=lf
*.sh       text eol=lf
# These are explicitly windows files and should use crlf
*.ps1      text eol=crlf
*.cmd      text eol=crlf
*.bat      text eol=crlf
 
# Serialisation
*.json     text
*.toml     text
*.xml      text
*.yaml     text
*.yml      text
 
# Archives
*.7z       binary
*.gz       binary
*.tar      binary
*.tgz      binary
*.zip      binary
 
# Text files where line endings should be preserved
*.patch    -text
 
#
# Exclude files from exporting
#
 
.gitattributes export-ignore
.gitignore     export-ignore
 
#
# development files
#
 
*.cs           text diff=csharp
*.java         text diff=java
*.pxd          text diff=python
*.py           text diff=python
*.py3          text diff=python
*.pyw          text diff=python
*.pyx          text diff=python
*.pyz          text diff=python
*.ipynb        text
 
*.exe          binary
*.class        binary
*.dll          binary
*.so           binary
*.ear          binary
*.jar          binary
*.war          binary
*.pyc          binary
*.pyd          binary
*.pyo          binary
 
*.sln          text eol=crlf
*.csproj       text eol=crlf
*.fsproj       text eol=crlf
*.vbproj       text eol=crlf
*.vcxproj      text eol=crlf
*.vcproj       text eol=crlf
*.dbproj       text eol=crlf
*.lsproj       text eol=crlf
*.wixproj      text eol=crlf
*.modelproj    text eol=crlf
*.sqlproj      text eol=crlf
*.wmaproj      text eol=crlf
*.xproj        text eol=crlf
*.props        text eol=crlf
*.filters      text eol=crlf
*.vcxitems     text eol=crlf
*.vssettings   text eol=crlf
*.ascx         text eol=crlf
*.ashx         text eol=crlf
*.asmx         text eol=crlf
*.aspx         text eol=crlf

Linux

~/.gitconfig
[include]
	path = .gitconfig.d/.gitalias
	path = .gitconfig.d/.gituser
[core]
	abbrev = 16
	eol = lf
	autocrlf = false
	safecrlf = false
	symlinks = true
	fscache = true
	preloadindex = true
	editor = code --wait
	attributesfile = ~/.gitattributes
[init]
	defaultBranch = main
[commit]
	gpgsign = true
[pull]
	rebase = true
[credential]
	helper = manager-core
[format]
	pretty = oneline
[log]
	abbrevCommit = true
[http]
	sslbackend = openssl
[gpg]
	program = gpg
[diff]
	tool = vscode
[diff "astextplain"]
	textconv = astextplain
[difftool]
	prompt = true
[difftool "vscode"]
	cmd = code --wait --diff $LOCAL $REMOTE
	keepBackup = false
[merge]
	tool = vscode
[mergetool]
	prompt = true
[mergetool "vscode"]
	cmd = code --wait $MERGED
	keepBackup = false
	trustExitCode = true
.gitconfig.d/.gitalias
[alias]
	a = add
	b = branch
	c = commit
	cam = commit --amend
	camne = commit --amend --no-edit
	diffs = diff --staged
	f = fetch
	l = log --format='%C(yellow)%h%C(auto)%d%C(yellow):%Creset %s %C(brightblue)(%cr) %C(brightmagenta)<%an>'
	la = l --all
	lag = l --all --graph
	last = l --all --graph -7
	lg = l --graph
	p = push
	s = status
	t = tag
	unstage = reset HEAD --
.gitconfig.d/.gituser
[user]
    name = Username
    email = 00000000+username@users.noreply.github.com
    signingkey = XXXXXXXXXXXXXXXX

Windows

folgende Änderungen an der Linux Konfiguration vornehmen (sslbackend ist optional, da openssl auch unter win funktioniert):

[http]
	sslbackend = schannel
[gpg]
	program = "C:/Program Files (x86)/GnuPG/bin/gpg.exe"

Sonstiges

# git --help
Verwendung: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

Allgemeine Git-Kommandos, verwendet in verschiedenen Situationen:

Arbeitsverzeichnis anlegen (siehe auch: git help tutorial)
   clone      ein Repository in einem neuen Verzeichnis klonen
   init       ein leeres Git-Repository erstellen oder ein bestehendes neuinitialisieren

an aktuellen Änderungen arbeiten (siehe auch: git help everyday)
   add        Dateiinhalte zum Commit vormerken
   mv         eine Datei, ein Verzeichnis, oder eine symbolische Verknüpfung verschieben oder umbenennen
   reset      aktuellen HEAD zu einem spezifizierten Zustand setzen
   rm         Dateien im Arbeitsverzeichnis und vom Index löschen

Historie und Status untersuchen (siehe auch: git help revisions)
   bisect     Binärsuche verwenden, um den Commit zu finden, der einen Fehler verursacht hat
   grep       Zeilen darstellen, die einem Muster entsprechen
   log        Commit-Historie anzeigen
   show       verschiedene Arten von Objekten anzeigen
   status     den Zustand des Arbeitsverzeichnisses anzeigen

Historie erweitern und bearbeiten
   branch     Branches anzeigen, erstellen oder entfernen
   checkout   Branches wechseln oder Dateien im Arbeitsverzeichnis wiederherstellen
   commit     Änderungen in das Repository eintragen
   diff       Änderungen zwischen Commits, Commit und Arbeitsverzeichnis, etc. anzeigen
   merge      zwei oder mehr Entwicklungszweige zusammenführen
   rebase     lokale Commits auf einem aktuellerem Upstream-Branch neu aufbauen
   tag        ein mit GPG signiertes Tag-Objekt erzeugen, auflisten, löschen oder verifizieren.

mit anderen zusammenarbeiten (siehe auch: git help workflows)
   fetch      Objekte und Referenzen von einem anderen Repository herunterladen
   pull       Objekte von einem externen Repository anfordern und sie mit einem anderen Repository oder einem lokalen Branch zusammenführen
   push       Remote-Referenzen mitsamt den verbundenen Objekten aktualisieren

'git help -a' und 'git help -g' listet verfügbare Unterkommandos und
einige Anleitungen zu Git-Konzepten auf. Benutzen Sie 'git help <Kommando>'
oder 'git help <Konzept>', um mehr über ein spezifisches Kommando oder
Konzept zu erfahren.

Einrichtung unter Windows (installiert durch Git Bash)

Hilfreiche Einstellungen

E-Mail und Benutzername festlegen:

> git config --global user.email "Boss@users.noreply.github.com"
> git config --global user.name "Boss"

Zeilenumbruch auf Windows (EOL)

Update: besser autocrlf auf false setzen und eol auf lf, dann mit gitattributes handlen (siehe config oben)

> git config --global core.eol lf

Bei commit Unix-EOL (LF bzw. \n) verwenden und bei checkout auf Windows Windows-EOL (CRLF bzw. \r\n) verwenden (Checkout: CRLF; Commit: LF):

> git config --global core.autocrlf true

Bei commit Unix-EOL (LF bzw. \n) verwenden und bei checkout EOL beibehalten und nicht automatisch ändern (Checkout: original; Commit: LF):

> git config --global core.autocrlf input

Bei commit und checkout EOL beibehalten und nicht automatisch ändern (Checkout: original; Commit: original):

> git config --global core.autocrlf false

siehe https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_formatting_and_whitespace (nach core.autocrlf suchen)

git pull

siehe https://git-scm.com/docs/git-config (nach pull suchen)

> git config --global pull.rebase false   # merge (the default strategy)
> git config --global pull.rebase true    # rebase
> git config --global pull.ff only        # fast-forward only

GPG konfigurieren

siehe auch GPG

  • immer lange version der keyid anzeigen
  • loopback für pinentry benutzen (nur auf windows, wenn man kein popup fenster möchte; auf linux besser pinentry-curses benutzen)
~/.gnupg/gpg.conf
keyid-format long
pinentry-mode loopback
  • cache ttl für passphrase (separat für ssh)
  • loopback pinentry erlauben
~/.gnupg/gpg-agent.conf
default-cache-ttl 7200
default-cache-ttl-ssh 1800
max-cache-ttl 28800
max-cache-ttl-ssh 7200
allow-loopback-pinentry

Unter Windows: Umgebungsvariable anlegen

GNUPGHOME=%userprofile%\.gnupg

evtl. Dateien vom alten ins neue dir verschieben (powershell commands)

> mv $env:userprofile/AppData/Roaming/gnupg/* $env:userprofile/.gnupg/
> rm $env:userprofile/AppData/Roaming/gnupg/

gpg agent restart bzw. start/stop (z.B. bei dem error: gpg: can't connect to the agent: Invalid value passed to IPC)

> gpg-connect-agent /bye
> gpg-connect-agent killagent /bye

das /bye verhindert, dass man in dem prompt von gpg-connect-agent bleibt

gpg testen

> echo "test" | gpg --clearsign

Wenn das Signieren mit GPG nicht geht, liegt es womöglich daran, dass GPG die TTY nicht findet und deshalb nicht nach der Passphrase fragen kann

> echo 'export GPG_TTY=$(tty)' >> ~/.bashrc

gpg key paar generieren:

> gpg --full-generate-key
  • RSA and RSA und 4096 auswählen
  • GitHub Namen und GitHub E-Mail eintragen

gpg key editieren:

> gpg --edit-key XXXXXXXXXXXXXXXX

gpg public key auflisten

> gpg -k --keyid-format=long

gpg private key auflisten

> gpg -K --keyid-format=long

gpg signingkey für git herausfinden (16-stellig, steht hinter sec 4096R/ oder sec rsa4096/):

> gpg -K --keyid-format=long
/home/USER/.gnupg/pubring.kbx
------------------------------
sec   rsa4096/XXXXXXXXXXXXXXXX 2022-01-01 [SC]
      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
uid                 [ultimate] Masterflitzer (Key for GitHub and Development) <USERNAME@users.noreply.github.com>
ssb   rsa4096/0000000000000000 2021-12-02 [E]

gpg key in git hinterlegen und automatisches signieren aktivieren:

> git config --global user.signingkey XXXXXXXXXXXXXXXX
> git config --global commit.gpgsign true

gpg key im armor (ascii) format exportieren (z.b. für github):

> gpg --armor --export XXXXXXXXXXXXXXXX

gpg key exportieren und als anderer user wieder importieren (wenn man ohne –armor exportieren will, sollte man die Standardendung .gpg statt .asc benutzen)

> gpg --armor --export-secret-key XXXXXXXXXXXXXXXX > /tmp/private.asc
> su -
> gpg --import /tmp/private.asc
> rm /tmp/private.asc

gpg key vertrauen (nach import erforderlich)

> gpg --edit-key XXXXXXXXXXXXXXXX
> gpg> trust
> gpg> save

gpg Key lokal exportieren und auf anderer maschine importieren

> gpg --armor --export-secret-key XXXXXXXXXXXXXXXX | ssh other-machine gpg --import

gpg key auf anderer maschine exportieren und lokal importieren

> ssh other-machine gpg --armor --export-secret-key XXXXXXXXXXXXXXXX | gpg --import

gpg programm unter linux:

> git config --global gpg.program gpg

stark empfohlen, da das in git integrierte gpg nicht aktuell ist: gpg programm unter windows (winget install -e --id GnuPG.GnuPG):

> git config --global gpg.program "C:/Program Files (x86)/GnuPG/bin/gpg.exe"

git bare repository

Normale git repos legen im Arbeitsverzeichnis (working dir) ein .git/ Verzeichnis an, welches alle Informationen, die für die Versionskontrolle benötigt werden enthält. Ein git bare repo speichert diese Informationen direkt im Verzeichnis, in dem das repo angelegt wurde, da ein bare repo kein Arbeitsverzeichnis hat. Deshalb eignet es sich für die Verwaltung von unkritischen (man sollte keine Passwörter in der Versionskontrolle haben) dotfiles und anderen configs.

> git init --bare ~/config-linux.git
> echo "alias git-config='git --git-dir=\$HOME/config-linux.git/ --work-tree=/'" >> ~/.bash_aliases
> bash
> git-config config --local status.showUntrackedFiles no

Wenn man nur Dateien innerhalb seines Homeverzeichnisses hinzufügen will, dann kann oben --work-tree=/ zu --work-tree=\$HOME geändert werden

einfache Anwendungsbeispiele:

> git-config add /path/to/config/file
> git-config commit -m "add file"
> git-config push

beim ersten mal statt normalen push:

> git-config remote add origin git@github.com:USERNAME/REPO.git
> git-config push --set-upstream origin main

Quelle/Erklärung: https://youtu.be/tBoLDpTWVOM

git mit proxy

Proxy global verwenden

> git config --global http.proxy http://10.20.30.40:3128

> git config --global --get http.proxy
http://10.20.30.40:3128

> cat ~/.gitconfig
[http]
        proxy = http://10.20.30.40:3128
> git -c http.sslVerify=false clone https://github.com/percona/proxysql-admin-tool
Cloning into 'proxysql-admin-tool'...
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 1559 (delta 14), reused 18 (delta 4), pack-reused 1523
Receiving objects: 100% (1559/1559), 745.10 KiB | 2.18 MiB/s, done.
Resolving deltas: 100% (962/962), done.

Proxy nur mit einer bestimmten URL verwenden

Quelle: https://gist.github.com/evantoli/f8c23a37eb3558ab8765

> vi ~/.gitconfig
[http]
[http "https://github.com"]
        proxy = http://10.20.30.40:3128
> git -c http.sslVerify=false clone https://github.com/percona/proxysql-admin-tool
Cloning into 'proxysql-admin-tool'...
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 1559 (delta 14), reused 18 (delta 4), pack-reused 1523
Receiving objects: 100% (1559/1559), 745.10 KiB | 2.18 MiB/s, done.
Resolving deltas: 100% (962/962), done.
/home/http/wiki/data/attic/git.1657477155.txt · Zuletzt geändert: von david