Skip to content
Snippets Groups Projects
Commit ee9cbddb authored by morit02's avatar morit02
Browse files

Add git branching commands

- List all branches
- Create a new branch
- Delete a branch
- Switch to another branch
parent d3217693
Branches
No related tags found
No related merge requests found
...@@ -13,6 +13,10 @@ Eine Tabelle zur Übersicht der verwendeten Befehle ...@@ -13,6 +13,10 @@ Eine Tabelle zur Übersicht der verwendeten Befehle
| `git restore --staged` | Dokumente/Verzeichnisse aus dem Staging-Bereich entfernen | `git restore --staged` | Dokumente/Verzeichnisse aus dem Staging-Bereich entfernen
| `git status` | Überblick über die Unterschiede zum letzten Commit | `git status` | Überblick über die Unterschiede zum letzten Commit
| `git commit` | Anlegen einer neuen Projektversion mit Dokumenten/Verzeichnissen aus dem Staging-Bereich | `git commit` | Anlegen einer neuen Projektversion mit Dokumenten/Verzeichnissen aus dem Staging-Bereich
| `git branch` | Überblick über alle Branches *(`-r` für remote Branches, `-a` für remote und lokale Branches)*
| `git branch <BRANCH> [<BRANCH/COMMIT>]` | Erstellen einer neuen Branch
| `git branch -d` | Löschen einer lokalen Branch
| `git checkout` | Zu einer anderen Branch wechseln
## Projekterstellung ## Projekterstellung
...@@ -396,6 +400,310 @@ git commit -m "Add files/directories" ...@@ -396,6 +400,310 @@ git commit -m "Add files/directories"
## Branching
In Git gibt es **Branches**. Eine Branch ist ein **Verweis** (Pointer) auf einen bestimmten **Commit**.
<table>
<tr>
<th> Commits und Branches </th>
<th> Info </th>
</tr>
<tr>
<td>
```
b1 main
V V
c1 <- c2 <- c3 <-c4
```
</td>
<td>
- **Commits** (c1, c2, c3 und c4)
- c4 ist abhängig von c3
- c3 ist abhängig von c2
- c2 ist abhängig von c1
- **Branches** (b1, main)
- Verweisen auf unterschiedliche Commits
</td>
</tr>
</table>
Änderungen die auf einer Branch mit einem Commit gespeichert werden, sind **nur** auf dieser Branch (als Commit) vorhanden.
Jede Branch kann eigene Commits mit unterschiedliche Dokumente/Verzeichnisse speichern.
Diese Unterschiede werden als Verzweigungen der Commits sichtbar. (`git log --graph --oneline --all`)
<table>
<tr>
<th> Commits und Branches </th>
<th> Info </th>
</tr>
<tr>
<td>
```
b2
V
b1 c3 <- c4 main
V / V
c1 <- c2 <- c5 <-c6 <- c7
```
</td>
<td>
- **Commits** (c1, c2, c3, c4, c5, c6 und c7)
- c4 ist der einzige Commit abhängig von c3
- c3 und c4 sind abhängig von c2
- c5, c6 und c7 sind abhängig von c2
- **Branches** (b1, b2 und main)
- Verweisen auf unterschiedliche Commits
</td>
</tr>
</table>
Häufig gibt es eine **Hauptbranch** (main/master) von der andere Branches abzweigen.
### Branch Operationen
Um Operationen mit Branches durchzuführen ist es sinnvoll einen Überblick zu allen existierenden Branches zu bekommen.
Die **lokalen** Branches können mit dem Befehl `git branch` angezeigt werden.\
Mit den Optionen `-r` werden die **remote** Branches und mit `-a` **lokal** und **remote** Branches angezeigt.
Zum anzeigen der Branches kann zusätzlich die `--list` Option gesetzt werden, um mit einem Muster zu filtern.
Die aktuelle lokale Branch wird mit einem **\*** markiert.
Zum **erstellen** einer neuen Branch kann `git branch <BRANCH_NAME> [<COMMIT/BRANCH>]` verwendet werden.\
Mit dem **Parameter** (`[<COMMIT/BRANCH>]`) kann ein **Commit** oder eine **Branch** als **Grundlage** für die zu erstellende Branch angegeben werden.
Ohne diesen zusätzlichen Parameter ist die Grundlage der **letzte Commit** der **aktuellen Branch**.\
*Mit `git checkout -b <NEUE_BRANCH> [<COMMIT/BRANCH>]` wird zusätzlich zu der neu erstellten Branch gewechselt.*
Zum **entfernen** einer Branch kann `git branch -d <BRANCH_NAME>` verwendet werden.
Zum **wechseln** zu einer anderen Branch kann `git checkout <BRANCH_NAME>` verwendet werden.
___
*Anstatt eine Branch anzugeben kann auch ein Commit durch den Commit-Hash oder einen relativen Pfad angegeben werden!\
Ein relativer Pfad verweist auf einen Commit zuvor, von dem der aktuelle Commit abhängt.
Ein relativer Pfad kann von einem Commit (`Commit-Hash~1`), einer Branch (`Branch~1`) oder dem HEAD (`HEAD~1`) angegeben werden.*
**Beispiel:**
- Commits werden mit c markiert
- Aktuelle Branch wird mit einem (**\***) markiert
<!--- Branch Creation Table -->
<table>
<tr>
<th> Commits und Branches </th>
<th> Git Befehl - Erstellung einer Branch </th>
</tr>
<tr>
<td>
```
b1 main (*)
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<!--- Examples Table -->
<td>
<table>
<tr>
<td>
- Erstellen einer Branch mit der aktuellen Branch
```
git branch b2
```
</td>
<td>
- Erstellen einer Branch mit einer anderen Branch (*b1*)
```
git branch b2 b1
```
</td>
<td>
- Erstellen einer Branch mit einem Commit-Hash (*c3*)
```
git branch b2 c3
```
</td>
</tr>
<tr>
<td>
- Resultat:
```
b2
b1 main (*)
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<td>
- Resultat:
```
b2
b1 main (*)
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<td>
- Resultat:
```
b1 b2 main (*)
V V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--- Branch deletion Table -->
<table>
<tr>
<th> Commits und Branches </th>
<th> Git Befehl - Löschen einer Branch </th>
</tr>
<td>
```
b1 main (*)
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<td>
<!--- Example Table -->
<table>
<tr>
<td>
- Löschen einer anderen Branch (*b1*)
```
git branch -d b1
```
</td>
</tr>
<tr>
<td>
- Resultat:
```
main (*)
V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
</tr>
</table>
</td>
</table>
<!--- Branch switching Table -->
<table>
<tr>
<th> Commits und Branches </th>
<th> Git Befehl - Wechseln zu anderen Commits/Branches </th>
</tr>
<td>
```
b1 main (*)
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<td>
<!--- Example Table -->
<table>
<tr>
<td>
- Wechseln zu einer anderen Branch (*b1*)
```
git checkout b1
```
</td>
<td>
- Wechseln zu einem Commit-Hash (*c3*)
```
git checkout c3
```
</td>
</tr>
<tr>
<td>
- Resultat:
```
b1 (*) main
V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
<td>
- Resultat:
```
b1 (*) main
V V V
c1 <- c2 <- c3 <- c4 <- c5
```
</td>
</tr>
</table>
</td>
</table>
## Add your files ## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files - [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment