Nástroje pro vývoj a monitorování software
GIT
Matematicko-fyzikální fakulta, Univerzita Karlova v Praze
Daniel Remiš
Overview
• Staging changes
– Commiting only selected changes
– C ommitting changes to one file in several phases – Describe output for `git diff` for a complex scenario
• Undo changes
• Branches and merging
– Basic concept
– Creating branches
– Switching working directory to another branch – Merging
– Resolving conflicts
File status lifecycle
Local operations
Track new file and check the status
$ git init
$ git add snake.pas
$ git status
# On branch master
# Initial commit
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: snake.pas
#
$ git commit
git init Create an empty git repository.
git add Add file contents to the index.
git status Show the working tree status.
git commit Record changes to the repository.
Staged and unstaged file
If you modify a file after you run git add , you have to run git add again to stage the latest version of the file.
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: snake.pas
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: snake.pas
#
View staged and unstaged changes
git diff Compares what is in your working directory with what is in your staging area.
git diff --cached Compares your staged changes to your last commit.
diff --git a/snake.pas b/snake.pas index 0e3b00a..d04c2fc 100644
--- a/snake.pas +++ b/snake.pas
@@ -1,3 +1,4 @@
+{my first game}
uses crt;
const a: byte = 2;
b: byte = 4;
Ignoring files and commit otions
.gitignore File, which disable tracking changes for files/folders.
git commit -v Puts the diff of your change in the editor.
git commit –m Type your commit message inline.
git commit –a Automatically stage every file
that is already tracked before
doing the commit.
Removing and moving files
git rm Remove files from the
working tree and from the index.
git rm –f Remove modified and indexed files.
git rm --cached Remove files from the index.
git mv Move or rename a file, a directory, or a symlink.
$ git mv default.html index.html
$ mv default.html index.html
$ git rm default.html
$ git add index.html
Undoing things
git commit --amend The commit you create replaces the current tip.
git reset HEAD <file> Unstaging a staged file.
git checkout -- <file> Unmodifying a modified file.
$ git commit -m 'initial commit'
$ git add index.html
$ git commit --amend
Branching and merging
git branch Create branch.
git branch -d Delete branch.
git checkout Checkout a branch or paths to the working tree.
git merge Join two or more development histories together.
$ git commit -m 'initial commit'
$ git add index.html
$ git commit --amend
Git vs. Subversion
Distributed version control diagram.
Centralized version control diagram.
Snapshots vs.Differences
data as snapshots
data as changes to a base
Comit
654fd.. 5dae1..
Snapshot A Snapshot B
commit
tree 4fde4f
author Dan..
commiter Dan..
commit
tree 43ea2f
author Dan..
commiter Dan..
parent 654fd
Branching
master
654ef5 56a3fe
Snapshot A Snapshot B
Branching 2
master
654ef5 56a3fe
pozdrav
$ git branch pozdrav
master HEAD
Branching 3
master
654ef5 56a3fe
pozdrav
$ git checkout pozdrav master
HEAD
Branching 4
master
654ef5 56a3fe
$ git commit –a –m „hello update“
master
HEAD 14a3fe
pozdrav
Branching 5
master
654ef5 56a3fe
$ git checkout master
master HEAD
14a3fe
pozdrav
Branching 6
master56a3fe
$ git checkout –b rozlouceni
rozlouceni HEAD
14a3fe
pozdrav 654ef5
master master
Branching 7
master56a3fe
$ git commit –a –m „added goodbye“
rozlouceni HEAD
14a3fe
pozdrav 654ef5
36fae master
master
Branching 8
master
56a3fe
$ git checkout master
rozlouceni HEAD
14a3fe
pozdrav 654ef5
36fae master
master
Branching 9
master56a3fe
$ git merge rozlouceni
rozlouceni HEAD
14a3fe
pozdrav 654ef5
36fae master
master
Branching 10
master56a3fe
$ git branch –d rozlouceni
HEAD
14a3fe
pozdrav 654ef5
36fae master
master
Merge conflict
master56a3fe
$ git merge pozdrav
HEAD
14a3fe
pozdrav 654ef5
36fae master
master
Merge conflict 2
$ git merge pozdrav Auto-merging snake.pas
CONFLICT (content): Merge conflict in snake.pas
Automatic merge failed; fix conflicts and then commit the result.
$ git status
# On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: snake.pas
#
Merge conflict 3
master56a3fe
$ git add snake.pas
$ git commit
HEAD
14a3fe
pozdrav 654ef5
36fae master
master
26afe1
Zdroje
Otázky
http://git-scm.com/book http://git-scm.com/docs
???