Files
Git-Schulung/03-branches/README.md
T

131 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🌿 Modul 03 Branches
Branches sind das Herzstück von Git. Sie ermöglichen es dir, an neuen Features zu arbeiten, ohne den stabilen Hauptcode zu verändern.
---
## Was ist ein Branch?
Ein Branch ist ein **beweglicher Zeiger** auf einen Commit. Wenn du auf einem Branch einen neuen Commit machst, rückt der Zeiger automatisch vorwärts.
Das klingt abstrakt stell dir vor:
- `main` ist dein **veröffentlichter Stand** (funktioniert immer)
- Du erstellst einen Branch `feature/begruessung` für neue Arbeit
- Dort probierst du aus, änderst, brichst ab `main` bleibt unberührt
- Wenn du fertig bist, **mergst** du den Feature-Branch zurück in `main`
---
## Branch-Workflow visualisiert
```mermaid
gitGraph
commit id: "Projekt start"
commit id: "README fertig"
branch feature/begruessung
checkout feature/begruessung
commit id: "hallo.txt angelegt"
commit id: "Begrüßung verbessert"
checkout main
merge feature/begruessung id: "Feature gemergt"
```
Auf `main` hat sich nichts verändert, solange du auf `feature/begruessung` gearbeitet hast. Erst beim Merge kommen die Änderungen zusammen.
---
## Befehle im Überblick
### Branch anlegen und wechseln
```bash
git branch feature/begruessung
git switch feature/begruessung
```
Oder in einem Schritt:
```bash
git switch -c feature/begruessung
```
### Aktuellen Branch anzeigen
```bash
git branch
```
```text
* feature/begruessung
main
```
Das Sternchen `*` zeigt, auf welchem Branch du gerade bist.
### Alle Branches anzeigen (auch remote)
```bash
git branch -a
```
### Zurück zu main wechseln
```bash
git switch main
```
### Branch mergen
```bash
git switch main
git merge feature/begruessung
```
```text
Updating a3f9c12..d4e5f67
Fast-forward
hallo.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 hallo.txt
```
### Branch löschen (nach dem Merge)
```bash
git branch -d feature/begruessung
```
---
## Unterschied sehen
Bevor du mergst, kannst du sehen, was sich auf dem Feature-Branch geändert hat:
```bash
git diff main..feature/begruessung
```
Oder alle Branches in der Übersicht:
```bash
git log --all --oneline --graph
```
```text
* d4e5f67 (feature/begruessung) Begrüßung verbessert
* c3d4e56 hallo.txt angelegt
* b7e2f45 (HEAD -> main) README fertig
* a3f9c12 Projekt start
```
---
## 📝 Jetzt bist du dran!
Weiter zur [Aufgabe](aufgabe.md).
---
**Weiter geht's mit:** [04 Merge & Konflikte](../04-merge-und-konflikte/README.md)