Git-Befehle-Spickzettel
Git zeichnet Änderungen an Dateien als eine Reihe von Commits in einem Repository auf. Diese Befehle decken den Tagesablauf ab: Repo anlegen, Arbeit speichern, verzweigen, mergen und mit einem Remote teilen. Halte diese Übersicht beim Lernen offen.
| Befehl | Funktion |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
Hinweise
- Die meisten Befehle akzeptieren Flags (wie -m für Nachricht oder -a für alle); rufe 'git help <befehl>' für die volle Liste auf.
- Branch-Namen sind günstig und wegwerfbar — committe oft auf einem Feature-Branch und merge dann in main.
- Vor destruktiven Befehlen wie rebase oder reset sicherstellen, dass die Arbeit committet oder gestasht ist.
Häufige Fragen
- Was ist der Unterschied zwischen merge und rebase?
- Merge vereint Historien mit einem neuen Commit, der zwei Eltern hat. Rebase spielt deine Commits auf einem anderen Branch erneut ab und erzeugt so einen linearen Verlauf, schreibt aber die Commit-IDs um.
- Wie mache ich einen Commit rückgängig?
- Nutze 'git revert <commit>', um einen neuen Commit hinzuzufügen, der die Änderung rückgängig macht (sicher, geteilte Historie). 'git reset' schreibt die Historie um und sollte nicht auf geteilten Branches genutzt werden.
- Was ist der Unterschied zwischen git pull und git fetch?
- Fetch lädt Commits und Referenzen von einem Remote herunter, ohne deinen Arbeitsbaum zu verändern. Pull macht fetch und führt die Änderungen dann in deinen aktuellen Branch zusammen oder rebased sie.
- Wann sollte ich git stash verwenden?
- Stash legt nicht committete Änderungen vorübergehend ab, damit du sauber Branch wechseln oder pullen kannst, und stellt sie später mit 'git stash pop' wieder her.