Zum Hauptinhalt springen
hinweis

Dieses Tutorial stellt einen Auszug aus meiner persönlichen Weiterbildung dar, daher sind die Informationen in kompakter Stichpunktform präsentiert.

Einführung in Git mithilfe von PowerShell

Git Allgemein​

Was ist git?​

  • Open Source Versionskontrolle
  • sehr schnell und effizient
    • kein Netzwerk erforderlich
  • große Verbreitung
    • große Community
    • GitHub sehr bekannt
  • Multi-platform
    • Linux
    • Mac
    • Windows
  • command-line basiert
    • gut für Automatisierung
    • scripting
  • integriert in vielen GUIs

Woher kommt git?​

  • kommt aus dem Linux Umfeld
  • Entwickler: Linus Torvalds

Git Verzeichnis Struktur​

  • Konfigurationen bleiben erhalten, wenn zu anderer Umgebung gewechselt wird

Grundlegendes Konzept​

COMMIT

Snapshot der Repository

BRANCH

Ein Favorit der auf ein commit zeigt

TAG

Beschreibung für eine Sammlung von commits

Konzept für Zusammenarbeit​

REMOTE

origin -> Ursprung

Lokale Kopie, Remote Host, Netzlaufwerk

CLONE

Kopie Repository von remote auf dem lokalen PC mit der History von git und alle commits

test

Philosophie​

  • master / main branch
  • alles was im master branch ist deployable (einsetzbar)
    • getestet
  • Neue features, fixes werden in eigenem branch entwickelt
    • branch nach feature, fix benennen
  • häufig und früh commiten
    • so bleibt es nachvollziehbar
  • der lokale branch und remote branch sollten die selben Namen haben

Praxis​

Voraussetzung PowerShell​

Das Module posh-git kann installiert werden, um git Support direkt im Powershell Terminal zu erhalten.

# Powershell Gallery einbinden
$webclient = New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = "tls12"
Register-PSRepository -Default -Verbose

# Module installieren
Install-Module posh-git
# Module muss jedes mal importiert werden
Import-Module posh-git
# Es kann aber auch im Profile hinterlegt werden
Add-PoshGitToProfile

Git Config initialisieren​

# config initialisieren
git config --global user.email "hoell@hl-dev.de"
git config --global user.name "David Höll"
# config prüfen

Vorbereitung, repository erstellen​

# Ordner erstellen
New-Item -Path testDirectory -ItemType Directory
# zu Ordner navigieren
Set-Location -Path .\testDirectory
# Ordner zeigen
Start-Process .
# repository initialisieren
git init
# Ordner zeigen
Start-Process .
# .git Ordner -> config etc.

Zur Repository hinzufügen​

Add-Content -Value "Hello this is my file" -Path "./myfile.txt"
# show files
Get-ChildItem
# Status zeigen
git status
# myfile.txt -> ist wird noch nicht getrackt von git
# my file tracken
git add .\myfile.txt
# oder
git add .
# Status zeigen
git status
# myfile.txt wurde von workspace -> zur staging area verschoben
git commit -m "feat: create a new file for testing"
# Status zeigen
git status

Hilfe​

# weitere Befehle anzeigen oder google benutzen
git help add
# myfile.txt zur repository hinzufügen

Veränderte Dateien​

# Änderungen hinzufügen
Add-Content -Value "This is the second line in my testfile" -Path "./myfile.txt"
# Status zeigen
git status
# Änderungen in repository schreiben
git add .
git commit -m "feat: second test line in myfile"
# oder -am -> add, commit
git commit -am "feat: second test line in myfile"
# Status zeigen
git status

git history​

# Historie anzeigen
git log
# Hilfe anzeigen
git help log
# z.B.:
git log --pretty=oneline

git checkout, reset​

# Änderungen hinzufügen
Add-Content -Value "This is a bad line of code" -Path "./myfile.txt"
# Inhalt zeigen
Get-Content -Path "./myfile.txt"
# Status zeigen
git status
# Workspace -> Staging
git add myfile.txt
git status
# Workspace <- STaging
git reset myfile.txt
git status
# file auf head setzen
git checkout myfile.txt
git status
# Inhalt zeigen
Get-Content -Path "./myfile.txt"

git reset --hard​

# Änderungen hinzufügen
Add-Content -Value "This is a bad line of code" -Path "./myfile.txt"
# neue Dateien anlegen
New-Item test1.txt
New-Item test2.txt
git status
# Workspace (Alle Dateien) -> Staging
git add .
# Head <- Staging (Alle Dateien)
git reset --hard
git status

git revert​

  • Setzt den commit zurück
  • und erstellt einen neuen Commit
# Änderungen hinzufügen
Add-Content -Value "This is a bad line of code" -Path "./myfile.txt"
Get-Content -path "./myfile.txt"
# Workspache --> Repository
git commit -am "feat: This my new nice feature"
# history anzeigen
git log
# commit zurücknehmen über die commit id (kann in der history nachgeschaut werden)
git revert 21a509d48349d84e03d5779017adf9541c1cffec
Get-Content -path "./myfile.txt"
# history anzeigen
git log

git branch​

branch basics​

# branches zeigen
git branch
# neuen branch aus dem letzten commit des aktuellen branches erstellen
git branch mybranch
git branch
# branch wechseln
git checkout mybranch
git branch
git checkout master
# delete branch
git branch mybranch123
git branch
git branch -D mybranch123
git branch
# branch erstellen und direkt in branch wechseln
git checkout -b mybranch12345679
git branch

branch implement a feature​

git checkout master
# file anlegen und commiten
Add-Content -Value "This is a bad line of code" -Path "./myfile.txt"
Get-Content -path "./myfile.txt"
git add .
git commit -m "feat: This my new nice feature"
git status
# branch wechseln
git checkout -b feature/delete_file
Get-ChildItem
Remove-Item .\myfile.txt
Get-ChildItem
git commit -am "feat: delete my file."
# in master branch wechseln
git checkout master
Get-ChildItem
git checkout feature/delete_file
Get-ChildItem

merge a file​

git checkout master
# neuen branch restellen
git checkout -b feature/add_files
# Dateien erstellen und commiten
New-Item myfile4.txt
New-Item myfile5.txt
New-Item myfile6.txt
Get-ChildItem
git add .
git status
git commit -m "feat: add 3 files"
# code review etc und dann in master mergen
git branch
git checkout master
git merge feature/add_files
# History zeigen
git log

merge Konflikt​

New-Item "./myfile9.txt"
git add .
git commit -m "feat: add myfile9.txt to content"
# neunen branch erstellen
git branch feature/add_lines_of_code
# Änderungen im master branch durchführen
Add-Content -Value "This is the first line from master branch" -Path "./myfile9.txt"
git commit -am "feat: first line from master branch"
Get-Content .\myfile9.txt
# branch wechseln
git checkout feature/add_lines_of_code
Get-Content .\myfile9.txt
Add-Content -Value "This is the first line from feature/add_lines_of_code branch" -Path "./myfile9.txt"
git commit -am "feat: first line from feature/add_lines_of_code branch"
Get-Content .\myfile9.txt
# in master branch wechseln
git checkout master
# Änderungen mergen und es kommt zum Konflikt
git merge feature/add_lines_of_code
git status
# VSCode verwenden um den Konflikt anzusehen
# z.B. Accept current changes
# commit via VSCode

git repository​

leere git Repro anlegen​

# Verzeichnis ertsellen
Set-Location \\ws0906\logfiles
New-Item GitRepro -Type Directory
Set-Location \\ws0906\logfiles\GitRepro
# git Repro anlegen
git init –-bare
# Zum git Pfad wechseln
Set-Location c:\Temp\Test

pull, push, Repro hinzufügen​

# git remote hinzufügen
git remote add origin //ws0906/logfiles/GitRepro
# zur Repro push
git push --set-upstream origin master
git push
# Auf Aktualisierungen prüfen
git fetch
# Aktualisierungen holen
git pull

repro klonen​

Set-Location ..
New-Item test2 -Type Directory
Set-Location .\test2
git clone //ws0906/logfiles/GitRepro
Set-Location .\GitRepro
git status

Wie man einen guten commit schreiben

https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/amp


Kommentare