Ściąga z poleceń Git
Git śledzi zmiany w plikach jako serię commitów w repozytorium. Te polecenia obejmują codzienny przepływ: tworzenie repo, zapisywanie pracy, rozgałęzianie, scalanie i udostępnianie zdalnemu. Trzymaj tę ściągę otwartą podczas nauki.
| Polecenie | Co robi |
|---|---|
| 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. |
Uwagi
- Większość poleceń przyjmuje flagi (jak -m dla wiadomości czy -a dla wszystkich); uruchom 'git help <polecenie>' dla pełnej listy.
- Nazwy gałęzi są tanie i można je porzucić — często commituj na gałęzi funkcji, a potem scalaj do main.
- Przed niszczącymi poleceniami, takimi jak rebase czy reset, upewnij się, że praca jest zcommitowana lub w stashu.
Częste pytania
- Jaka jest różnica między merge a rebase?
- Merge łączy historie nowym commitem mającym dwóch rodziców. Rebase odtwarza twoje commity na innym branchu, dając liniową historię, ale przepisuje ID commitów.
- Jak cofnąć commit?
- Użyj 'git revert <commit>', aby dodać nowy commit cofający zmianę (bezpieczne, wspólna historia). 'git reset' przepisuje historię i nie powinno się go używać na współdzielonych branchach.
- Jaka jest różnica między git pull a git fetch?
- Fetch pobiera commity i referencje z remote bez dotykania drzewa roboczego. Pull robi fetch, a potem scala lub robi rebase tych zmian do bieżącego brancha.
- Kiedy używać git stash?
- Stash tymczasowo odkłada niezcommitowane zmiany, by móc czysto przełączyć branch lub pobrać, a potem przywraca je przez 'git stash pop'.