Шпаргалка з команд Git

Git записує зміни файлів як серію комітів у репозиторії. Ці команди охоплюють щоденний цикл: створення репозиторію, збереження роботи, гілкування, злиття та обмін із віддаленим. Тримайте шпаргалку відкритою під час навчання.

Оновлено:

КомандаПризначення
git initCreate a new, empty Git repository in the current directory.
git cloneDownload a remote repository and its full history to your machine.
git statusShow changed, staged and untracked files in the working tree.
git addStage changes (files) to be included in the next commit.
git commitRecord staged changes as a new commit with a message.
git logDisplay the commit history of the repository.
git diffShow unstaged or staged differences between states.
git branchList, create or delete branches in the repository.
git checkout / switchSwitch branches or restore files (switch is the modern command).
git mergeJoin another branch's history into the current branch.
git rebaseReapply commits on top of another base commit, rewriting history.
git pullFetch from a remote and merge the changes into the current branch.
git pushUpload local commits to a remote repository.
git stashTemporarily shelve uncommitted changes to reapply later.
git remoteManage the set of remote repositories you track.

Примітки

Часті запитання

У чому різниця між merge і rebase?
Merge об'єднує історії новим комітом із двома батьками. Rebase відтворює ваші коміти поверх іншої гілки, даючи лінійну історію, але переписує ID комітів.
Як скасувати коміт?
Скористайтеся 'git revert <коміт>', щоб додати новий коміт, що скасовує зміну (безпечно, спільна історія). 'git reset' переписує історію і не варто застосовувати на спільних гілках.
У чому різниця між git pull і git fetch?
Fetch завантажує коміти та посилання з віддаленого, не торкаючись робочого дерева. Pull робить fetch, а потім зливає або робить rebase цих змін у поточну гілку.
Коли використовувати git stash?
Stash тимчасово відкладає незакомічені зміни, щоб ви могли чисто перемкнути гілку або зробити pull, а потім відновити їх командою 'git stash pop'.