Dies ist eine alte Version des Dokuments!
Inhaltsverzeichnis
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, --global und --local (siehe pfade)
Git Konfiguration
Pfade
- Wichtig:
- mehr Informationen:
git config –helpoder https://git-scm.com/docs/git-config#FILES - Einstellungen nachvollziehen:
git config –list –show-origin(zeigt zu jedem Eintrag den Speicherort an)
| Scope | Allgemein | Linux (Beispiel) | Windows (Beispiel) |
|---|---|---|---|
| System | $(prefix)/etc/gitconfig | /usr/local/etc/gitconfig | C:/Program Files/Git/etc/gitconfig |
| Global | $HOME/.gitconfig | /home/user/.gitconfig | C:/Users/user/.gitconfig |
| Local | $GIT_DIR/config | my-repo/.git/config | my-repo/.git/config |
| Worktree | $GIT_DIR/config.worktree | my-repo/.git/config.worktree | my-repo/.git/config.worktree |
- System: unter linux gibt diese beiden verbreiteten pfade:
/usr/local/etc/gitconfigund/etc/gitconfig - Global:
$XDG_CONFIG_HOME/git/configund$HOME/.config/git/configwerden auch unterstützt, aber von$HOME/.gitconfigüberschrieben - Local: standard, wenn man keine option angibt (z.B.
git config init.defaultBranch main) - Worktree: sehr selten genutzt, siehe docs
schönes git log
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 fsmonitor = true fscache = true symlinks = false preloadindex = true editor = code --wait attributesfile = gitattributes [init] defaultBranch = main [commit] gpgsign = true [fetch] prune = true [pull] rebase = true [push] autoSetupRemote = true [log] abbrevCommit = true [format] pretty = oneline [credential] helper = manager-core gitHubAuthModes = pat gitLabAuthModes = pat [http] sslbackend = openssl [gpg] program = gpg [diff "astextplain"] textconv = astextplain [difftool] prompt = true [mergetool] prompt = true [diff] tool = vscode [merge] tool = vscode [difftool "vscode"] cmd = code --wait --diff $LOCAL $REMOTE keepBackup = false [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
Achtung: keine vollständige Konfiguration, nur Änderungen gegenüber der Datei für Linux!
Folgende Änderungen an der Linux Konfiguration vornehmen:
Diese Änderungen sind optional:
http.sslbackend(der wertopensslfunktioniert auch unter win, aberschannelermöglicht das nutzen von Windows Certificate Stores)- Um das Diff- bzw. Merge-Tool von Visual Studio zu benutzen:
diff.tool,merge.tool,difftool "vsdiffmerge".cmdundmergetool "vsdiffmerge".cmd
- .gitconfig
[http] sslbackend = schannel [gpg] program = "C:/Program Files (x86)/GnuPG/bin/gpg.exe" [diff] tool = vsdiffmerge [merge] tool = vsdiffmerge [difftool "vsdiffmerge"] cmd = \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$LOCAL\" \"$REMOTE\" //t keepBackup = false [mergetool "vsdiffmerge"] cmd = \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m keepBackup = false trustExitCode = true
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 GPG
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"
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 public key im ascii format (armored) exportieren (z.b. für github):
> gpg --armor --export XXXXXXXXXXXXXXXX
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.
