Git-komentojen pikaopas
Git seuraa tiedostojen muutoksia commit-sarjana repositoriossa. Nämä komennot kattavat päivittäisen työnkulun: repon aloitus, työn tallennus, haaraaminen, yhdistäminen ja jakaminen etän kanssa. Pidä tämä opas auki opiskellessasi.
| Komento | Mitä tekee |
|---|---|
| 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. |
Huomautukset
- Useimmat komennot hyväksyvät liput (kuten -m viestille tai -a kaikille); täysi lista on komennossa 'git help <komento>'.
- Haaranimet ovat halpoja ja poisheitettäviä — commitoi usein ominaisuushaarassa ja yhdistä sitten mainiin.
- Ennen tuhoisia komentoja, kuten rebase tai reset, varmista, että työsi on commitoitu tai stashattu.
Usein kysytyt kysymykset
- Mikä on erona merge- ja rebase-komentojen välillä?
- Merge yhdistää historiat uudella commitilla, jolla on kaksi vanhempaa. Rebase toistaa commitisi toisen haaran päällä, mikä tuottaa lineaarisen historian mutta kirjoittaa commit-tunnukset uudelleen.
- Miten peruutan commitin?
- Käytä 'git revert <commit>' lisätäksesi uuden commitin, joka kumoaa muutoksen (turvallinen, jaettu historia säilyy). 'git reset' kirjoittaa historian uudelleen eikä sitä pidä käyttää jaetuilla haaroilla.
- Mikä on erona git pull- ja git fetch-komentojen välillä?
- Fetch lataa commitit ja viitteet etän ilman että koskettaa työpuuta. Pull tekee haun ja yhdistää tai rebaseaa muutokset nykyiseen haaraasi.
- Milloin git stash kannattaa käyttää?
- Stash laittaa commitoimattomat muutokset väliaikaisesti syrjään, jotta voit vaihtaa haaraa tai hakea puhtaasti, ja palauttaa ne myöhemmin komennolla 'git stash pop'.