SlideShare a Scribd company logo
1 of 62
Download to read offline
Łódź, 8.01.2015
Java User Group Łódź Maciej Jesionowski
2
Spis treści
● OCB: Git
● Szybki start
● Offline
● Branching
● Remotes
● Q&A
3
Spis treści
● OCB: Git
● Szybki start
● Offline
● Branching
● Remotes
● Q&A
Staging
Commit
Log
Branch
Diff
4
Spis treści
● OCB: Git
● Szybki start
● Offline
● Branching
● Remotes
● Q&A
Merge
Rebase
Konflikty
5
O co biega? Git
VCS – System Kontroli Wersji
● Offline
● Szybki
● Elastyczny
● Dla power-userów
6
O co biega? Git
A wady?
● Command Line
● Dużo do opanowania na początek
● To nie SVN
● Zróżnicowany poziom trudności
7
Cel
● Nacisk na podstawy
● Solidna baza do pogłębiania wiedzy
● Kontrola nad tym co się dzieje
8
Szybki start
GNU/Linux
Package Manager
● git
● gitk
● git-gui
● meld
Windows
http://git-scm.com/
Mac OS X
Instalowany razem
z Xcode i narzędziami
Inne narzędzia: Sourcetree
9
Konfiguracja
$ git config --global <klucz> <wartość>
user.name
user.email
core.editor
color.ui
merge.tool
“Jan Kowalski”
jkowalski@example.com
vim
auto (domyślne od 1.8.4)
meld
10
Git Offline
11
Git Offline - Repozytorium
1. Projekt bez kontroli wersji
2. Import do Git'a
3. Repozytorium
status
init
12
Git Offline - Workflow
Wprowadzanie zmian Przygotowanie do “zdjęcia”
Staging
Snapshot
Commit
13
Git Offline - Staging i Index
1. Jak zapisuje się zmiany?
2. Dodawanie, usuwanie
i przywracanie plików
3. Commit (snapshot)
status
add
rm
mv
commit
reset
checkout
14
Git Offline - Staging i Index
Working Directory Stage / Index
? ?
Nowo utworzone repozytorium
$ git status
untracked
15
Git Offline - Staging i Index
Working Directory Stage / Index
Dodanie zmian (stage)
$ git add <file>
added
16
Git Offline - Staging i Index
Working Directory Stage / Index
Stan po zrobieniu commita
$ git commit
17
Git Offline - Staging i Index
Working Directory Stage / Index
Kolejne zmiany
$ git status
?
R
to be removed
untracked
18
Git Offline - Staging i Index
Working Directory Stage / Index
$ git rm $ git add
added
removed
19
Git Offline - Staging i Index
Working Directory Stage / Index
Wycofanie pliku (unstage)
$ git reset -- <file>
?
removed untracked
20
Git Offline - Staging i Index
Working Directory Stage / Index
added
modified
removed
M
Modyfikacja po dodaniu zmian
.gitignore
21
Git Offline - Staging i Index
Checkout Reset
Reset --hard
Albo:
git-gui
NB: Git nie jest w stanie zapisać pustego katalogu
22
Git Offline - Commit
1. Commit pod lupą
2. Historia zmian (log)
3. gitk
commit
log
23
Git Offline - Commit
Commit =
zmiany + metadane
● commit hash
● komunikat (message)
● zmiany
● hash poprzedniego
commita(ów) (parent)
● autor, commiter,
data/godzina
Ważne: Commit jest niemodyfikowalny
Raz utworzony commit zostaje w repo na zawsze*
24
Nic
Git Offline - Kilka commitów - log
czas
#1
#2
#3
init
pierwszy commit
drugi
trzeci
25
Git Offline - Log i Branch
$ git log
$ gitk
Branch - to “etykietka” przypięta
do najnowszego commita.
Active branch - to branch, “na którym”
tworzymy nowe commity.
26
Git Offline - Nawigacja
master
master~1
master~2
master~3
#0
#1
#2
#3
HEAD
HEAD~1
HEAD~2
HEAD~3
HEAD~
HEAD~~
HEAD~~~
eb32a79
HEAD^
HEAD^^
HEAD^^^
Poleceniem git diff można zobaczyć zmiany
wprowadzone od wskazanego commita.
27
Git Offline - Szybka korekta
Amend - używamy, gdy chcemy poprawić
ostatni commit.
przed po
a
b
c
a1
b
c
a
master master
master
$ git commit --amend
28
Git Offline - Podsumowanie
● Kopia robocza / Working directory
● Repozytorium (lokalne) / Repository
● Stage / Index
● Commit
● Branch
● Log
● Diff
29
Git Offline - Podsumowanie
● init
● status
● add, rm, mv
● commit
● log
● diff
● checkout
● reset
$ git add <file>
$ git rm <file>
$ git add -A
$ git commit -m “Message”
$ git reset
$ git checkout -- <file>
$ git checkout -f
$ git reset --hard
$ git diff --cached
$ git diff HEAD~1
$ git help <command>
30
Branching
31
Branching - Dlaczego?
● Porządkowanie pracy (feature/topic branch)
● Eksperymentowanie
● Elastyczność (bugfix vs development)
● Bo to szybkie i wygodne
NB: Stash - szybka alternatywa dla branch+commit
32
Branching - Przykład
master
feature/baz
feature/foo
Implemented Foo
Added dependency on LibBaz
[WTF-0027] Bugfix for ...
a
b
d
f
h
c
g
e
33
Branching - Polecenia
1. Tworzenie nowego brancha
2. Zmiana aktywnego brancha
3. Dodanie i usunięcie
commitów
4. Zmiana nazwy i usunięcie
brancha
status
checkout
branch
reset
34
Branching - Log i diff
e
foo bar
c
d
a
b
$ git log foo a, b, e, ...
$ git log bar c, d, e, ...
$ git log foo..bar c, d
$ git log bar..foo a, b
$ git log foo bar
$ git log bar foo
a, b, c, d, e, ...
$ git log foo...bar
$ git log bar...foo
a, b, c, d
$ git diff foo..bar
$ git diff foo bar a -> c
$ git diff bar..foo
$ git diff bar foo
c -> a
$ git diff foo...bar e -> c
$ git diff bar...foo e -> a
35
Branching - Scalanie branchy
Merge Rebase
● Nigdy nie zmienia historii
● Tworzy nowy commit
łączący branche
● Rozgałęzienie w logu jest
widoczne po merge'u
● Zmienia historię
(podmienia oryginalne commity)
● Log ulega “wyprostowaniu”
(commity są “przenoszone” na
docelowy branch)
Uwaga: Scalanie zawsze zaczynamy od usunięcia zmian
w katalogu roboczym (git status)
36
Branching - Konflikty
Modyfikacja tego samego pliku (usunięcie, zmiana
tych samych linii, etc.)
/**
* Add two floating point values.
*/
public double add(double a, double b) {
<<<<<<< HEAD
// TODO cba to implement this atm...
throw new UnsupportedOperationException();
=======
return a + b;
>>>>>>> foobar
}
Unmerged paths:
both modified: math.java
$ git status
37
Branching - Merge
przed po
c
e
a
b
d
master
foobar
master
1) $ git checkout master
2) $ git merge foobar
c
e
a
b
d
master
foobar
f
38
Branching - Fast-forward Merge
przed po
c
d
a
b
master
foobar
master
1) $ git checkout master
2) $ git merge foobar
c
d
a
b
master foobar
39
Branching - Merge - konflikt
c
e
a
b
d
master
foobar
f
math.java:
throw new Exception...
math.java:
return a + b;
Konflikt: math.java
1. Konflikty ze wszystkich commitów rozwiązujemy
w tym samym miejscu - merge commit.
2. Poprawiamy, dodajemy do stage'a i wykonujemy commit.
40
Branching - Rebase
przed po
a
e
b
d
master
foobar foobar
1) $ git checkout foobar
2) $ git rebase master
c
a
e
b
d
master
foobar
c
b1
d1
41
Branching - Rebase - konflikt
a
e
b
d
master
foobar
c
math.java:
throw new Exception...
math.java:
return a + b;
utils.java
Dodano metodę
utils.java
Usunięty - nie używany
1. Rebase przenosi po kolei commity z foobar na master
2. Dla każdego kolejnego commita może wystąpić nowy konflikt
3. Przebieg rebase'a kontrolujemy poleceniem
$ git rebase (--continue | --skip | --abort)
Konflikt
42
Branching - 3-way diff
OURS a
e
b
d
master
foobar
c
THEIRS
BASE
/**
* Add two floating point
*/
public double add(double a,
<<<<<<< HEAD
// TODO cba to impleme
throw new UnsupportedO
=======
return a + b;
>>>>>>> foobar
}
BACKUP - conflicted
/**
* Add two floating point va
*/
public double add(double a,
return 0.0; // TODO
}
/**
* Add two floating point va
*/
public double add(double a,
// TODO cba to implement
throw new UnsupportedOpe
}
/**
* Add two floating point va
*/
public double add(double a,
return a + b;
}
OURS / LOCAL THEIRS / REMOTE
BASE
$ git mergetool
43
Branching - Rebase #2
przed po
e
g
c
d
master
foo
f
a
b
bar
e
g
c
d
master
f
a
b
bar
a1
b1
c1
d1 bar
foo
1) $ git checkout bar
2) $ git rebase master
44
Branching - Rebase #2
przed po
e
g
c
d
master
foo
1) $ git rebase --onto master foo bar
$ git rebase -i master
f
a
b
bar
e
g
c
d
master
foo
f
a
b
bar
a1
b1 bar
45
Branching - Tips & Tricks
● $ git mergetool
● $ git clean (-n | -f | -rf)
● $ git checkout --conflict=diff3 -- <plik>
● $ git reset --hard ORIG_HEAD
● $ git reflog
● konflikty plików binarnych -- unikać :-)
● “merge” bez zachowania historii -- squash
● rebase interaktywny (rebase -i) może też:
odrzucić lub zmienić kolejność commitów, squashować
46
Branching - Podsumowanie
● Master branch
● Feature / topic / etc. branch
● Merge commit
● Rebase
● Konflikt / 3-way diff / mergetool
● Commit jest niezmienny i nie “ginie”
● NB: interactive rebase / squash merge i inne
47
Branching - Podsumowanie
● checkout
● branch
● reset
● merge
● rebase
● status
● add, rm, mv
$ git checkout -f <branch>
$ git checkout -b <nowy>
$ git reset --hard <branch>
$ git branch <nowy> <gdzie>
$ git branch -m <stara> <nowa>
$ git branch (-d|-D) <branch>
$ git merge feature
$ git rebase master [feature]
$ git rebase --continue
$ git log <branch>
$ git diff <od>..<do>
48
Remotes
49
Remotes - Ogólnie
GIT
src/
lib/
README
GIT
GIT
Bob
Repozytorium
lokalne (local)
Working
Directory
Bare Repo
Filesystem Bob'a
Git Hosting
GIT / HTTP(S) / SSH
Inny projekt
GIT
Repozytorium
zdalne (remote)
50
remotes/origin https://gitserver.com/repo.git
Branche
Remotes - Local vs Remote
GIT
src/
lib/
README
GIT
Branche
Lokalne
(Local)
Zdalne
(Remote)
51
Remotes - Klonowanie
1. Pobranie repozytorium
2. Wyświetlanie branchy
3. Informacje o remote
clone
branch
status
remote
52
Remotes - Local vs Remote
Różnice między local i remote branchem:
1. Branch zdalny nie może być aktywny
2. Na branch zdalny nie da się commitować
NB: Branch zdalny nie aktualizuje się “automatycznie”
a
b
c
master remotes/origin/master
53
Remotes - Pobieranie zmian
a
b
master r../origin/master
1) $ git fetch
$ git fetch (<remote> | --all)
2) $ git merge origin/master
a
b
master
r../origin/master
c
d
przed po
r../origin/mas..
54
Remotes - Pobieranie zmian #2
a
b
master r../origin/master
1) $ git checkout master
2) $ git pull origin master
$ git pull
a
b
master r../origin/master
c
d
przed po
r../origin/master
master
NB: Często użycie pull kończy się merge'm,
którego nie chcieliśmy.
55
Remotes - Wysyłanie zmian
1) $ git push origin master
$ git push origin lokalny:zdalny
przed po
c
d
master r../origin/master
a
b
remotes/origin/master
c
d
master
remotes/origin/master
a
b
56
Remotes - Force push
1) $ git push --force origin lokalny:zdalny
przed po
d
c
master
r../origin/master
a
b
d
c
master r../origin/master
a
b
r../origin/master
NB: Wykonując force push, upewnij się, że inne
osoby też pozbędą się odrzuconych commitów
57
Remotes - Workflow
GIT
src/
lib/
README
GIT
Local
Twoje repo
Server
Remote
push origin master
fetch origin
Work
Commit
Merge
Rebase
fetch
push
origin
deploy
GIT
Remote
https://gitserver.com/repo.git
Aktualizuj branche
58
Remotes - Pobieranie / wysyłanie
1. Pobranie zdalnych commitów
2. Aktualizacja lokalnego
brancha
3. Wysłanie lokalnych commitów
4. Usuwanie zdalnych branchy
(lokalnie i “na serwerze”)
fetch
pull
push
branch
59
Remotes - Podsumowanie
● Remote (repozytorium), Bare repository
● Clone
● Local vs Remote branch
● Fetch (a także pull)
● Push
● Upstream / tracking
60
Remotes - Podsumowanie
● clone
● remote
● fetch
● push
● pull
● branch
$ git clone (<url> | path)
$ git branch (-a | -r)
$ git remote show origin
$ git checkout -t origin/foo
$ git fetch [-p | --prune]
$ git fetch origin
$ git push origin lokalny:zdalny
$ git push origin :zdalny
$ git branch -rd origin/foo
61
Dziękuję!
62
Linki / Dokumentacja
● Oficjalna dokumentacja: Reference, Git Book
http://git-scm.com/doc
● Różne tutoriale i poradniki
https://help.github.com/articles/good-resources-for-learning-git-a
nd-github/

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

GIT

  • 1. Łódź, 8.01.2015 Java User Group Łódź Maciej Jesionowski
  • 2. 2 Spis treści ● OCB: Git ● Szybki start ● Offline ● Branching ● Remotes ● Q&A
  • 3. 3 Spis treści ● OCB: Git ● Szybki start ● Offline ● Branching ● Remotes ● Q&A Staging Commit Log Branch Diff
  • 4. 4 Spis treści ● OCB: Git ● Szybki start ● Offline ● Branching ● Remotes ● Q&A Merge Rebase Konflikty
  • 5. 5 O co biega? Git VCS – System Kontroli Wersji ● Offline ● Szybki ● Elastyczny ● Dla power-userów
  • 6. 6 O co biega? Git A wady? ● Command Line ● Dużo do opanowania na początek ● To nie SVN ● Zróżnicowany poziom trudności
  • 7. 7 Cel ● Nacisk na podstawy ● Solidna baza do pogłębiania wiedzy ● Kontrola nad tym co się dzieje
  • 8. 8 Szybki start GNU/Linux Package Manager ● git ● gitk ● git-gui ● meld Windows http://git-scm.com/ Mac OS X Instalowany razem z Xcode i narzędziami Inne narzędzia: Sourcetree
  • 9. 9 Konfiguracja $ git config --global <klucz> <wartość> user.name user.email core.editor color.ui merge.tool “Jan Kowalski” jkowalski@example.com vim auto (domyślne od 1.8.4) meld
  • 11. 11 Git Offline - Repozytorium 1. Projekt bez kontroli wersji 2. Import do Git'a 3. Repozytorium status init
  • 12. 12 Git Offline - Workflow Wprowadzanie zmian Przygotowanie do “zdjęcia” Staging Snapshot Commit
  • 13. 13 Git Offline - Staging i Index 1. Jak zapisuje się zmiany? 2. Dodawanie, usuwanie i przywracanie plików 3. Commit (snapshot) status add rm mv commit reset checkout
  • 14. 14 Git Offline - Staging i Index Working Directory Stage / Index ? ? Nowo utworzone repozytorium $ git status untracked
  • 15. 15 Git Offline - Staging i Index Working Directory Stage / Index Dodanie zmian (stage) $ git add <file> added
  • 16. 16 Git Offline - Staging i Index Working Directory Stage / Index Stan po zrobieniu commita $ git commit
  • 17. 17 Git Offline - Staging i Index Working Directory Stage / Index Kolejne zmiany $ git status ? R to be removed untracked
  • 18. 18 Git Offline - Staging i Index Working Directory Stage / Index $ git rm $ git add added removed
  • 19. 19 Git Offline - Staging i Index Working Directory Stage / Index Wycofanie pliku (unstage) $ git reset -- <file> ? removed untracked
  • 20. 20 Git Offline - Staging i Index Working Directory Stage / Index added modified removed M Modyfikacja po dodaniu zmian .gitignore
  • 21. 21 Git Offline - Staging i Index Checkout Reset Reset --hard Albo: git-gui NB: Git nie jest w stanie zapisać pustego katalogu
  • 22. 22 Git Offline - Commit 1. Commit pod lupą 2. Historia zmian (log) 3. gitk commit log
  • 23. 23 Git Offline - Commit Commit = zmiany + metadane ● commit hash ● komunikat (message) ● zmiany ● hash poprzedniego commita(ów) (parent) ● autor, commiter, data/godzina Ważne: Commit jest niemodyfikowalny Raz utworzony commit zostaje w repo na zawsze*
  • 24. 24 Nic Git Offline - Kilka commitów - log czas #1 #2 #3 init pierwszy commit drugi trzeci
  • 25. 25 Git Offline - Log i Branch $ git log $ gitk Branch - to “etykietka” przypięta do najnowszego commita. Active branch - to branch, “na którym” tworzymy nowe commity.
  • 26. 26 Git Offline - Nawigacja master master~1 master~2 master~3 #0 #1 #2 #3 HEAD HEAD~1 HEAD~2 HEAD~3 HEAD~ HEAD~~ HEAD~~~ eb32a79 HEAD^ HEAD^^ HEAD^^^ Poleceniem git diff można zobaczyć zmiany wprowadzone od wskazanego commita.
  • 27. 27 Git Offline - Szybka korekta Amend - używamy, gdy chcemy poprawić ostatni commit. przed po a b c a1 b c a master master master $ git commit --amend
  • 28. 28 Git Offline - Podsumowanie ● Kopia robocza / Working directory ● Repozytorium (lokalne) / Repository ● Stage / Index ● Commit ● Branch ● Log ● Diff
  • 29. 29 Git Offline - Podsumowanie ● init ● status ● add, rm, mv ● commit ● log ● diff ● checkout ● reset $ git add <file> $ git rm <file> $ git add -A $ git commit -m “Message” $ git reset $ git checkout -- <file> $ git checkout -f $ git reset --hard $ git diff --cached $ git diff HEAD~1 $ git help <command>
  • 31. 31 Branching - Dlaczego? ● Porządkowanie pracy (feature/topic branch) ● Eksperymentowanie ● Elastyczność (bugfix vs development) ● Bo to szybkie i wygodne NB: Stash - szybka alternatywa dla branch+commit
  • 32. 32 Branching - Przykład master feature/baz feature/foo Implemented Foo Added dependency on LibBaz [WTF-0027] Bugfix for ... a b d f h c g e
  • 33. 33 Branching - Polecenia 1. Tworzenie nowego brancha 2. Zmiana aktywnego brancha 3. Dodanie i usunięcie commitów 4. Zmiana nazwy i usunięcie brancha status checkout branch reset
  • 34. 34 Branching - Log i diff e foo bar c d a b $ git log foo a, b, e, ... $ git log bar c, d, e, ... $ git log foo..bar c, d $ git log bar..foo a, b $ git log foo bar $ git log bar foo a, b, c, d, e, ... $ git log foo...bar $ git log bar...foo a, b, c, d $ git diff foo..bar $ git diff foo bar a -> c $ git diff bar..foo $ git diff bar foo c -> a $ git diff foo...bar e -> c $ git diff bar...foo e -> a
  • 35. 35 Branching - Scalanie branchy Merge Rebase ● Nigdy nie zmienia historii ● Tworzy nowy commit łączący branche ● Rozgałęzienie w logu jest widoczne po merge'u ● Zmienia historię (podmienia oryginalne commity) ● Log ulega “wyprostowaniu” (commity są “przenoszone” na docelowy branch) Uwaga: Scalanie zawsze zaczynamy od usunięcia zmian w katalogu roboczym (git status)
  • 36. 36 Branching - Konflikty Modyfikacja tego samego pliku (usunięcie, zmiana tych samych linii, etc.) /** * Add two floating point values. */ public double add(double a, double b) { <<<<<<< HEAD // TODO cba to implement this atm... throw new UnsupportedOperationException(); ======= return a + b; >>>>>>> foobar } Unmerged paths: both modified: math.java $ git status
  • 37. 37 Branching - Merge przed po c e a b d master foobar master 1) $ git checkout master 2) $ git merge foobar c e a b d master foobar f
  • 38. 38 Branching - Fast-forward Merge przed po c d a b master foobar master 1) $ git checkout master 2) $ git merge foobar c d a b master foobar
  • 39. 39 Branching - Merge - konflikt c e a b d master foobar f math.java: throw new Exception... math.java: return a + b; Konflikt: math.java 1. Konflikty ze wszystkich commitów rozwiązujemy w tym samym miejscu - merge commit. 2. Poprawiamy, dodajemy do stage'a i wykonujemy commit.
  • 40. 40 Branching - Rebase przed po a e b d master foobar foobar 1) $ git checkout foobar 2) $ git rebase master c a e b d master foobar c b1 d1
  • 41. 41 Branching - Rebase - konflikt a e b d master foobar c math.java: throw new Exception... math.java: return a + b; utils.java Dodano metodę utils.java Usunięty - nie używany 1. Rebase przenosi po kolei commity z foobar na master 2. Dla każdego kolejnego commita może wystąpić nowy konflikt 3. Przebieg rebase'a kontrolujemy poleceniem $ git rebase (--continue | --skip | --abort) Konflikt
  • 42. 42 Branching - 3-way diff OURS a e b d master foobar c THEIRS BASE /** * Add two floating point */ public double add(double a, <<<<<<< HEAD // TODO cba to impleme throw new UnsupportedO ======= return a + b; >>>>>>> foobar } BACKUP - conflicted /** * Add two floating point va */ public double add(double a, return 0.0; // TODO } /** * Add two floating point va */ public double add(double a, // TODO cba to implement throw new UnsupportedOpe } /** * Add two floating point va */ public double add(double a, return a + b; } OURS / LOCAL THEIRS / REMOTE BASE $ git mergetool
  • 43. 43 Branching - Rebase #2 przed po e g c d master foo f a b bar e g c d master f a b bar a1 b1 c1 d1 bar foo 1) $ git checkout bar 2) $ git rebase master
  • 44. 44 Branching - Rebase #2 przed po e g c d master foo 1) $ git rebase --onto master foo bar $ git rebase -i master f a b bar e g c d master foo f a b bar a1 b1 bar
  • 45. 45 Branching - Tips & Tricks ● $ git mergetool ● $ git clean (-n | -f | -rf) ● $ git checkout --conflict=diff3 -- <plik> ● $ git reset --hard ORIG_HEAD ● $ git reflog ● konflikty plików binarnych -- unikać :-) ● “merge” bez zachowania historii -- squash ● rebase interaktywny (rebase -i) może też: odrzucić lub zmienić kolejność commitów, squashować
  • 46. 46 Branching - Podsumowanie ● Master branch ● Feature / topic / etc. branch ● Merge commit ● Rebase ● Konflikt / 3-way diff / mergetool ● Commit jest niezmienny i nie “ginie” ● NB: interactive rebase / squash merge i inne
  • 47. 47 Branching - Podsumowanie ● checkout ● branch ● reset ● merge ● rebase ● status ● add, rm, mv $ git checkout -f <branch> $ git checkout -b <nowy> $ git reset --hard <branch> $ git branch <nowy> <gdzie> $ git branch -m <stara> <nowa> $ git branch (-d|-D) <branch> $ git merge feature $ git rebase master [feature] $ git rebase --continue $ git log <branch> $ git diff <od>..<do>
  • 49. 49 Remotes - Ogólnie GIT src/ lib/ README GIT GIT Bob Repozytorium lokalne (local) Working Directory Bare Repo Filesystem Bob'a Git Hosting GIT / HTTP(S) / SSH Inny projekt GIT Repozytorium zdalne (remote)
  • 50. 50 remotes/origin https://gitserver.com/repo.git Branche Remotes - Local vs Remote GIT src/ lib/ README GIT Branche Lokalne (Local) Zdalne (Remote)
  • 51. 51 Remotes - Klonowanie 1. Pobranie repozytorium 2. Wyświetlanie branchy 3. Informacje o remote clone branch status remote
  • 52. 52 Remotes - Local vs Remote Różnice między local i remote branchem: 1. Branch zdalny nie może być aktywny 2. Na branch zdalny nie da się commitować NB: Branch zdalny nie aktualizuje się “automatycznie” a b c master remotes/origin/master
  • 53. 53 Remotes - Pobieranie zmian a b master r../origin/master 1) $ git fetch $ git fetch (<remote> | --all) 2) $ git merge origin/master a b master r../origin/master c d przed po r../origin/mas..
  • 54. 54 Remotes - Pobieranie zmian #2 a b master r../origin/master 1) $ git checkout master 2) $ git pull origin master $ git pull a b master r../origin/master c d przed po r../origin/master master NB: Często użycie pull kończy się merge'm, którego nie chcieliśmy.
  • 55. 55 Remotes - Wysyłanie zmian 1) $ git push origin master $ git push origin lokalny:zdalny przed po c d master r../origin/master a b remotes/origin/master c d master remotes/origin/master a b
  • 56. 56 Remotes - Force push 1) $ git push --force origin lokalny:zdalny przed po d c master r../origin/master a b d c master r../origin/master a b r../origin/master NB: Wykonując force push, upewnij się, że inne osoby też pozbędą się odrzuconych commitów
  • 57. 57 Remotes - Workflow GIT src/ lib/ README GIT Local Twoje repo Server Remote push origin master fetch origin Work Commit Merge Rebase fetch push origin deploy GIT Remote https://gitserver.com/repo.git Aktualizuj branche
  • 58. 58 Remotes - Pobieranie / wysyłanie 1. Pobranie zdalnych commitów 2. Aktualizacja lokalnego brancha 3. Wysłanie lokalnych commitów 4. Usuwanie zdalnych branchy (lokalnie i “na serwerze”) fetch pull push branch
  • 59. 59 Remotes - Podsumowanie ● Remote (repozytorium), Bare repository ● Clone ● Local vs Remote branch ● Fetch (a także pull) ● Push ● Upstream / tracking
  • 60. 60 Remotes - Podsumowanie ● clone ● remote ● fetch ● push ● pull ● branch $ git clone (<url> | path) $ git branch (-a | -r) $ git remote show origin $ git checkout -t origin/foo $ git fetch [-p | --prune] $ git fetch origin $ git push origin lokalny:zdalny $ git push origin :zdalny $ git branch -rd origin/foo
  • 62. 62 Linki / Dokumentacja ● Oficjalna dokumentacja: Reference, Git Book http://git-scm.com/doc ● Różne tutoriale i poradniki https://help.github.com/articles/good-resources-for-learning-git-a nd-github/