<
From version < 22.1 >
edited by cds
on 2012/10/15 11:19
To version < 30.1 >
edited by msp
on 2012/10/16 10:56
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.cds
1 +XWiki.msp
Content
... ... @@ -1,19 +1,325 @@
1 -This tutorial will address the source code management (SCM) tool named [[Git>>url:http://git-scm.com/||shape="rect"]].
1 +This tutorial will address the source code management (SCM) tool named [[Git>>url:http://git-scm.com/||shape="rect"]]. By following these steps you should learn about the basic usage of Git, which is required for the whole practical course. Furthermore, Git is a great SCM tool, and it's good to know how to use it. During this tutorial, we will follow Alan Turing's thoughts towards developing the [[Turing Machine>>url:http://en.wikipedia.org/wiki/Turing_machine||shape="rect"]].
2 2  
3 -= Repositories and Commits =
3 +More in-depth documentation can be found on the [[official home page>>url:http://git-scm.com/documentation||shape="rect"]], which mentions books, videos, and links to other tutorials and references.
4 4  
5 -{{task-list}}
6 -{{task id="1" status="incomplete"}}
7 -test1
8 -{{/task}}
5 += Creating Commits =
9 9  
10 -{{task id="2" status="incomplete"}}
11 -test2
12 -{{/task}}
7 +1. Read the [[Git for Computer Scientists>>url:http://eagain.net/articles/git-for-computer-scientists/||shape="rect"]] introduction (skip this if you are already familiar with Git).
8 +1. For Linux, Git is available in its own package. Windows users can install [[msysGit>>url:http://msysgit.github.com/||shape="rect"]]. For MacOS, Git is available as part of [[Xcode>>url:https://developer.apple.com/xcode/||shape="rect"]]; if you cannot install that, use [[Git for OSX>>url:http://code.google.com/p/git-osx-installer/||shape="rect"]].
9 +1. (((
10 +Configure your name and email address (will be included in all commits you create):
13 13  
14 -{{task id="3" status="incomplete"}}
15 -test 3
16 -{{/task}}
17 -{{/task-list}}
12 +{{noformat}}
13 +$ git config --global --add user.name "Your Name"
14 +$ git config --global --add user.email "<login>@informatik.uni-kiel.de"
15 +{{/noformat}}
16 +)))
17 +1. (((
18 +Create a local repository for the "//Turing Project//":
18 18  
19 -\\
20 +{{noformat}}
21 +$ mkdir turing
22 +$ cd turing
23 +$ git init
24 +Initialized empty Git repository in ~/turing/.git/
25 +{{/noformat}}
26 +
27 +The {{code language="none"}}.git{{/code}} subdirectory contains all history and metadata of the repository. You should not modify it.
28 +)))
29 +1. (((
30 +Add and commit some content: copy [[attach:notes.txt]]{{code language="none"}}{{/code}} to your {{code language="none"}}turing{{/code}} directory.
31 +
32 +{{noformat}}
33 +$ git add notes.txt
34 +$ git commit -m "wrote some first notes"
35 +[master (root-commit) 2e73b34] wrote some first notes
36 + 1 files changed, 5 insertions(+), 0 deletions(-)
37 + create mode 100644 notes.txt
38 +{{/noformat}}
39 +
40 +The file is now stored in the local history of your repository.
41 +)))
42 +1. Edit {{code language="none"}}notes.txt{{/code}}:\\
43 +11. Replace "fixed" with "infinite" in line 1.
44 +11. Replace "... (TODO)" with "a finite state machine" in line 4.
45 +1. (((
46 +View the status of your current working copy:
47 +
48 +{{noformat}}
49 +$ git status
50 +# On branch master
51 +# Changed but not updated:
52 +# (use "git add <file>..." to update what will be committed)
53 +# (use "git checkout -- <file>..." to discard changes in working directory)
54 +#
55 +# modified: notes.txt
56 +#
57 +no changes added to commit (use "git add" and/or "git commit -a")
58 +{{/noformat}}
59 +)))
60 +1. (((
61 +Mark the modified file to include it in the next commit, then view the status again and compare with the previous output:
62 +
63 +{{noformat}}
64 +$ git add notes.txt
65 +$ git status
66 +# On branch master
67 +# Changes to be committed:
68 +# (use "git reset HEAD <file>..." to unstage)
69 +#
70 +# modified: notes.txt
71 +#
72 +{{/noformat}}
73 +)))
74 +1. (((
75 +Commit the modified content to your local repository and view the status:
76 +
77 +{{noformat}}
78 +$ git commit -m "modified tape length, found a controller for tape head"
79 +[master 52e2d49] modified tape length, found a controller for tape head
80 + 1 files changed, 2 insertions(+), 2 deletions(-)
81 +$ git status
82 +# On branch master
83 +nothing to commit (working directory clean)
84 +{{/noformat}}
85 +)))
86 +
87 +After the preceding steps you have two commits in your local repository, each with one file in the index. You have different commands for viewing these commits:
88 +
89 +{{noformat}}
90 +$ git log
91 +commit 52e2d4946791c2725015853e5e261ce143c6fe8a
92 +Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
93 +Date: Mon Oct 15 15:00:14 2012 +0200
94 +
95 + modified tape length, found a controller for tape head
96 +
97 +commit 2e73b34ac44480773fc0e52875b7353a087d8c6d
98 +Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
99 +Date: Mon Oct 15 12:14:06 2012 +0200
100 +
101 + wrote some first notes
102
103 +$ git show 52e2d49
104 +commit 52e2d4946791c2725015853e5e261ce143c6fe8a
105 +Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
106 +Date: Mon Oct 15 15:00:14 2012 +0200
107 +
108 + modified tape length, found a controller for tape head
109 +
110 +diff --git a/notes.txt b/notes.txt
111 +index 4ded2b3..bd422b3 100644
112 +--- a/notes.txt
113 ++++ b/notes.txt
114 +@@ -1,5 +1,5 @@
115 +- * A tape with fixed length
116 ++ * A tape with infinite length
117 + * Tape head can read or write data
118 + * Tape head can move left or right
119 +- * The head is controlled by ... (TODO)
120 ++ * The head is controlled by a finite state machine
121 +{{/noformat}}
122 +
123 +Note that each commit is identified by a looong hash value, but it is possible to use only a prefix when referencing them (if the prefix is not ambiguous): the example above uses {{code language="none"}}52e2d49{{/code}} to identify the second commit. The commit hashes in your repository will be different from those seen in this tutorial, because the name of the author and the exact time of committing is also considered in the hash calculation. Also try the command {{code language="none"}}gitk{{/code}} to get an overview of your commits (a better alternative available for MacOS is [[GitX>>url:http://gitx.frim.nl/||shape="rect"]]).
124 +
125 += Branching and Merging =
126 +
127 +In the previous section you have created two commits on the default branch, which is named {{code language="none"}}master{{/code}}. Now you will create a new branch and commit there, thus adding complexity to the commit graph.
128 +
129 +1. (((
130 +Create a branch with name //sketches//:
131 +
132 +{{noformat}}
133 +$ git branch sketches
134 +{{/noformat}}
135 +)))
136 +1. (((
137 +View the list of branches:
138 +
139 +{{noformat}}
140 +$ git branch
141 +* master
142 + sketches
143 +{{/noformat}}
144 +
145 +The star reveals that you are still on the old {{code language="none"}}master{{/code}} branch.
146 +)))
147 +1. (((
148 +Switch to the new branch:
149 +
150 +{{noformat}}
151 +$ git checkout sketches
152 +Switched to branch 'sketches'
153 +$ git branch
154 + master
155 +* sketches
156 +{{/noformat}}
157 +
158 +It is also possible to create a branch and switch immediately to it using the option {{code language="none"}}-b{{/code}} of {{code language="none"}}git checkout{{/code}}.
159 +)))
160 +1. (((
161 +Download and add the new file [[attach:examples.txt]]{{code language="none"}}{{/code}}:
162 +
163 +{{noformat}}
164 +$ git add examples.txt
165 +$ git commit -m "wrote first examples"
166 +[sketches cd63135] wrote first examples
167 + 1 files changed, 20 insertions(+), 0 deletions(-)
168 + create mode 100644 examples.txt
169 +{{/noformat}}
170 +
171 +Inspecting the commit graph with {{code language="none"}}gitk{{/code}} (or another graphical viewer) you see that the {{code language="none"}}sketches{{/code}} branch now has three commits, while {{code language="none"}}master{{/code}} is still at the second commit.
172 +)))
173 +1. (((
174 +Merging the {{code language="none"}}sketches{{/code}} branch into {{code language="none"}}master{{/code}} means that all changes that have been made in {{code language="none"}}sketches{{/code}} are also applied to {{code language="none"}}master{{/code}}. In order to perform this merge, we have to check out the {{code language="none"}}master{{/code}} branch first:
175 +
176 +{{noformat}}
177 +$ git checkout master
178 +Switched to branch 'master'
179 +$ git merge sketches
180 +Updating 52e2d49..cd63135
181 +Fast-forward
182 + examples.txt | 20 ++++++++++++++++++++
183 + 1 files changed, 20 insertions(+), 0 deletions(-)
184 + create mode 100644 examples.txt
185 +{{/noformat}}
186 +
187 +This was a //fast-forward// merge: since the {{code language="none"}}master{{/code}} branch was completely contained in the {{code language="none"}}sketches{{/code}} branch, the merge could be done by simply changing the head pointer of {{code language="none"}}master{{/code}} to be the same as the head of {{code language="none"}}sketches{{/code}}.
188 +)))
189 +1. (((
190 +Now add the line "{{code language="none"}}see some examples in 'examples.txt'{{/code}}" to the file {{code language="none"}}notes.txt{{/code}} and commit this change in the current branch:
191 +
192 +{{noformat}}
193 +$ git add notes.txt
194 +$ git commit -m "added reference to the new examples"
195 +[master a5e244f] added reference to the new examples
196 + 1 files changed, 2 insertions(+), 1 deletions(-)
197 +{{/noformat}}
198 +)))
199 +1. (((
200 +Switch back to the {{code language="none"}}sketches{{/code}} branch and commit something there. Note that the {{code language="none"}}checkout{{/code}} command modifies your working copy, hence you have to update your text editor's content if you opened one of the files.
201 +
202 +{{noformat}}
203 +$ git checkout sketches
204 +Switched to branch 'sketches'
205 +{{/noformat}}
206 +
207 +Add the line "{{code language="none"}}Move one step left:{{/code}}" and write an updated version of the tape with tape head in the file {{code language="none"}}examples.txt{{/code}}, then commit.
208 +
209 +{{noformat}}
210 +$ git add examples.txt
211 +$ git commit -m "added another example"
212 +[sketches 55a9cb1] added another example
213 + 1 files changed, 5 insertions(+), 0 deletions(-)
214 +{{/noformat}}
215 +
216 +Now our two branches have //diverged//, which means that they cannot be fast-forwarded anymore.
217 +)))
218 +1. (((
219 +Merge the {{code language="none"}}master{{/code}} branch into {{code language="none"}}sketches{{/code}}:
220 +
221 +{{noformat}}
222 +$ git merge master
223 +Merge made by recursive.
224 + notes.txt | 3 ++-
225 + 1 files changed, 2 insertions(+), 1 deletions(-)
226 +{{/noformat}}
227 +
228 +Using {{code language="none"}}gitk{{/code}} you can see that a new commit was created that has two parent commits. Such a commit is called //merge// commit and is done automatically when a non-fast-forward merge is applied.
229 +)))
230 +1. (((
231 +Add a commit in each of the two branches using the commands you have already learned.
232 +1. Check out {{code language="none"}}master{{/code}}.
233 +1. (((
234 +Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
235 +
236 +{{noformat nopanel="true"}}
237 + * The finite state machine has an initial state and one or more final states
238 +{{/noformat}}
239 +)))
240 +1. Commit the change to {{code language="none"}}notes.txt{{/code}}.
241 +1. Check out {{code language="none"}}sketches{{/code}} (make sure to refresh your text editor so {{code language="none"}}notes.txt{{/code}} is reset to its previous state, without the change made above).
242 +1. (((
243 +Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
244 +
245 +{{noformat nopanel="true"}}
246 + * Each state transition can trigger head movement and data read/write
247 +{{/noformat}}
248 +)))
249 +1. Commit the change to {{code language="none"}}notes.txt{{/code}}.
250 +)))
251 +1. (((
252 +Merge the {{code language="none"}}master{{/code}} branch into the current branch ({{code language="none"}}sketches{{/code}}):
253 +
254 +{{noformat}}
255 +$ git merge master
256 +Auto-merging notes.txt
257 +CONFLICT (content): Merge conflict in notes.txt
258 +Automatic merge failed; fix conflicts and then commit the result.
259 +{{/noformat}}
260 +
261 +As expected, the branches could not be merged automatically, since both branches modified the same line in the same file.
262 +)))
263 +1. (((
264 +Use the {{code language="none"}}status{{/code}} command to see the list of affected files:
265 +
266 +{{noformat}}
267 +$ git status
268 +# On branch sketches
269 +# Unmerged paths:
270 +# (use "git add/rm <file>..." as appropriate to mark resolution)
271 +#
272 +# both modified: notes.txt
273 +#
274 +no changes added to commit (use "git add" and/or "git commit -a")
275 +{{/noformat}}
276 +)))
277 +1. (((
278 +The modified {{code language="none"}}notes.txt{{/code}} should now contain the following text:
279 +
280 +{{noformat nopanel="true"}}
281 +<<<<<<< HEAD
282 + * Each state transition can trigger head movement and data read/write
283 +=======
284 + * The finite state machine has an initial state and one or more final states
285 +>>>>>>> master
286 +{{/noformat}}
287 +
288 +The upper line is the one committed to {{code language="none"}}sketches{{/code}}, while the lower line was committed to {{code language="none"}}master{{/code}}. You have to resolve the conflict by editing the file. In this case the conflict is resolved by keeping both lines in arbitrary order, that means you should just remove the conflict markers (lines 5, 7, and 9 in {{code language="none"}}notes.txt{{/code}}).
289 +)))
290 +1. (((
291 +Use the {{code language="none"}}add{{/code}} command to mark {{code language="none"}}notes.txt{{/code}} as resolved. Entering {{code language="none"}}git commit{{/code}} without a message will open a text editor with an automatically created commit message. Just close the editor, and the merge commit is completed:
292 +
293 +{{noformat}}
294 +$ git commit
295 +[sketches 21d5ddb] Merge branch 'master' into sketches
296 +$ git show 21d5ddb
297 +commit 21d5ddbbcba4e36464653a2a550dbf595ead921f
298 +Merge: 17f75c7 8af2d50
299 +Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
300 +Date: Tue Oct 16 10:44:09 2012 +0200
301 +
302 + Merge branch 'master' into sketches
303 +
304 + Conflicts:
305 + notes.txt
306 +
307 +diff --cc notes.txt
308 +index 8f72873,bb81298..ba94a08
309 +--- a/notes.txt
310 ++++ b/notes.txt
311 +@@@ -2,6 -2,6 +2,7 @@@
312 + * Tape head can read or write data
313 + * Tape head can move left or right
314 + * The head is controlled by a finite state machine
315 + + * Each state transition can trigger head movement and data read/write
316 ++ * The finite state machine has an initial state and one or more final states
317 + see some examples in 'examples.txt'
318 +{{/noformat}}
319 +)))
320 +
321 +The {{code language="none"}}gitk{{/code}} tool should now display this graph:
322 +
323 +[[image:attach:turing-graph-01.png]]
324 +
325 +
Confluence.Code.ConfluencePageClass[0]
Id
... ... @@ -1,1 +1,1 @@
1 -2982107
1 +2982134
URL
... ... @@ -1,1 +1,1 @@
1 -https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982107/Git
1 +https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982134/Git