GIT w praktyce
Wojciech Urbański
Słowem wstępu
- System kontroli wersji
- porządkuje proces współpracy
- wspomaga automatyzację
- praktyka, praktyka, praktyka...
Podstawowe operacje
- Wstępna konfiguracja
- Tworzenie/klonowanie repo
- Operacje na zmianach
- Grupowanie zmian
- Przeglądanie historii
- Synchronizacja zmian
Wstępna konfiguracja
- git config
$ git config [params]
git config --global user.name "[name]"
git config --global user.email "[email address]"
git config --global color.ui auto
git config --global core.autocrlf true
Konfiguracja gita
Tworzenie repozytorium
- git clone
- git init
$ git init [project-name]
$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /home/urban/repos/test/.git/
### magiczny katalog .git!
Tworzenie repozytorium
$ git clone [url]
$ git clone <adres_repozytorium>
Pobranie repozytorium wraz z historią
Operacje na zmianach
- git status
- git diff
- git add [file]
- git reset [file]
- git commit -m "[wiadomość]"
$ git status
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Wyświetla wszystkie zmiany w plikach śledzonych
$ git diff
$ git diff
diff --git a/test.txt b/test.txt
index e69de29..4fc43a6 100644
--- a/test.txt
+++ b/test.txt
@@ -0,0 +1 @@
+test
Wyświetlanie różnic w plikach
$ git add [file]
$ touch test2
$ git add test2
Dodanie pliku do wersjonowania
$ git restore --staged [file]
$ git add test.txt
$ git reset test.txt
Unstaged changes after reset:
M test.txt
Przeciwieństwo do git add, zachowuje zawartość pliku
$ git restore [file]
$ git add test.txt
$ git reset test.txt
Unstaged changes after reset:
M test.txt
Przywraca zawartość pliku do stanu z repozytorium
$ git commit -m "[message]"
$ git add test.txt
$ git commit -m "Dodana istotna zawartość pliku test.txt"
[master 03bbc98] Dodana istotna zawartość pliku test.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test2
Stworzenie snapshot'u z dodanych plików
$ git commit --amend
$ git add test.txt
$ git commit --amend
Modyfikacja ostatniego commita
Przeglądanie historii
- git log
- git diff
- git show
$ git log
$ git log
commit 9dfaa28762f41efa8323a4efdd5465d5ee9ad581
Author: Wojciech Urbanski <urban@ds.pg.gda.pl>
Date: Tue Mar 31 10:48:55 2015 +0200
Zmiany na branchu szkolenie
commit 03bbc9803a060925ab2e63b00e0cd92d96e456a7
Author: Wojciech Urbanski <urban@ds.pg.gda.pl>
Date: Tue Mar 31 10:23:28 2015 +0200
Dodana istotna zawartość pliku test.txt
commit 55b6db8f046d168165d6eab10b7f5fd2ea4d8001
Author: Wojciech Urbanski <urban@ds.pg.gda.pl>
Date: Tue Mar 31 10:15:43 2015 +0200
Pierwszy commit
commit b528b6b5672bef2965df0b7f27fe49afff8cf6d1
Author: Wojciech Urbański <teh.urb@gmail.com>
Date: Sun Mar 29 22:40:59 2015 +0200
Initial commit
Wyświetla historię zmian obecnego branch'a
$ git log "a dog"
git log --all --decorate --oneline --graph
$ git diff [r1]..[r2]
$ git diff master..test2
diff --git a/test.txt b/test.txt
index 5407457..4fc43a6 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1 @@
test
-test2
Wyświetlenie zmian między rewizjami
$ git show [branch]
$ git show master
commit 9dfaa28762f41efa8323a4efdd5465d5ee9ad581
Author: Wojciech Urbanski <urban@ds.pg.gda.pl>
Date: Tue Mar 31 10:48:55 2015 +0200
Zmiany na branchu szkolenie
diff --git a/test.txt b/test.txt
index 4fc43a6..5407457 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
test
+test2
Wyświetla informacje o danym branch'u
Grupowanie zmian
- git branch
- git switch
- git merge
- git tag
$ git branch
$ git branch
* master
test1
test2
test3
Lista branch'y repozytorium
$ git branch [name]
$ git branch szkolenie
$ git branch
* master
szkolenie
test1
test2
test3
Tworzenie nowego branch'a
$ git switch [-c] [name]
$ git switch szkolenie
Switched to branch 'szkolenie'
$ git branch
master
* szkolenie
test1
test2
test3
Przejście na innego branch'a
-b tworzy branch "w locie"
$ git merge [branch]
$ git checkout master
Switched to branch 'master'
$ git merge szkolenie
Updating 03bbc98..9dfaa28
Fast-forward
test.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ git log --oneline
9dfaa28 Zmiany na branchu szkolenie
03bbc98 Dodana istotna zawartość pliku test.txt
55b6db8 Pierwszy commit
b528b6b Initial commit
Łączenie historii branch'a z obecnym
$ git tag
$ git tag
v1.0
v2.0
$ git tag -l "v1.8.5*"
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
v1.8.5-rc2
v1.8.5-rc3
v1.8.5.1
v1.8.5.2
v1.8.5.3
v1.8.5.4
v1.8.5.5
tagi są znacznikami na konkretne miejsca w repozytorium
$ git tag -a <tag> -m <msg>
$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4
$ git show v1.4
tag v1.4
Tagger: Ben Straub <ben@straub.cc>
Date: Sat May 3 20:19:12 2014 -0700
my version 1.4
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date: Mon Mar 17 21:52:11 2008 -0700
Change version number
Tagi z adnotacją zawierają dodatkowe informacje o danym miejscu w repo
Czym różni się tag od brancha?
Przepisujemy historię
- squash merge
- git rebase
squash merge
- połączenie listy commitów w jeden, zawierający wszystkie zmiany
- upraszcza historię, tracąc część informacji
git rebase
- odtworzenie listy zmian z jednego brancha na innym
- tworzy nowe commity, zachowując zmiany
- pozwala na edycję każdego commita z osobna
- mogą występować konflikty
tryb interaktywny!
$ git switch experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command
Synchronizacja zmian
- git remote
- git fetch
- git push
- git pull
$ git remote
$ git remote add repo2 git@github.com:some/repo
$ git remote
origin
repo2
Zarządzanie odnośnikami do repozytoriów
$ git push [repo] [branch]
$ git push repo2 master
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 798 bytes, done.
Total 9 (delta 0), reused 3 (delta 0)
To git@github.com:some/repo
* [new branch] master -> master
Wysłanie zmian do repozytorium
$ git fetch [repo] [branch]
$ git fetch repo2 master
From git@github.com:some/repo
* branch master -> FETCH_HEAD
Pobranie historii z danego repozytorium
$ git pull [repo] [branch]
$ git pull repo2 master
From git@github.com:some/repo
* branch master -> FETCH_HEAD
Already up-to-date.
Pobranie zmian z repozytorium i włączenie ich do odpowiedniego branch'a
<remote>/<branch>
$ git pull repo2 master
From git@github.com:some/repo
* branch master -> FETCH_HEAD
Already up-to-date.
lokalny branch odpowiada zdalnemu stanowi <remote>/<branch>
Gitlab Flow
- Ustandaryzowany sposób współpracy nad repozytorium
- wykorzystuje branche i merge requesty do wprowadzania zmian
- może korzystać z forków
Przydatne linki
- Strona domowa gita
- Git probook
- Cheat sheet
- Szkolenie na codeacademy.com
- git immersion
Szkolenie - git
By Wojciech Urbański
Szkolenie - git
- 84