<
From version < 31.1 >
edited by msp
on 2012/10/16 11:00
To version < 25.1 >
edited by msp
on 2012/10/15 14:48
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,24 +1,12 @@
1 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 -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.
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 -
6 -
7 -{{toc style="circle" maxLevel="4"/}}
8 -
9 9  = Creating Commits =
10 10  
11 11  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).
12 12  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"]].
13 13  1. (((
14 -Configure your name and email address (will be included in all commits you create):
15 -
16 -{{noformat}}
17 -$ git config --global --add user.name "Your Name"
18 -$ git config --global --add user.email "<login>@informatik.uni-kiel.de"
19 -{{/noformat}}
20 -)))
21 -1. (((
22 22  Create a local repository for the "//Turing Project//":
23 23  
24 24  {{noformat}}
... ... @@ -28,10 +28,25 @@
28 28  Initialized empty Git repository in ~/turing/.git/
29 29  {{/noformat}}
30 30  
31 -The {{code language="none"}}.git{{/code}} subdirectory contains all history and metadata of the repository. You should not modify it.
19 +{{panel}}
20 +**$ mkdir turing**{{code language="none"}}{{/code}}
21 +
22 +**$ cd turing**{{code language="none"}}{{/code}}
23 +
24 +**$ git init**{{code language="none"}}{{/code}}
25 +
26 +{{code language="none"}}Initialized empty Git repository in ~/turing/.git/{{/code}}
27 +{{/panel}}
28 +
29 +{{code language="bash"}}
30 +$ mkdir turing
31 +$ cd turing
32 +$ git init
33 +Initialized empty Git repository in ~/turing/.git/
34 +{{/code}}
32 32  )))
33 33  1. (((
34 -Add and commit some content: copy [[attach:notes.txt]]{{code language="none"}}{{/code}} to your {{code language="none"}}turing{{/code}} directory.
37 +Add some content: copy [[attach:notes.txt]]{{code language="none"}}{{/code}} to your {{code language="none"}}turing{{/code}} directory.
35 35  
36 36  {{noformat}}
37 37  $ git add notes.txt
... ... @@ -40,61 +40,28 @@
40 40   1 files changed, 5 insertions(+), 0 deletions(-)
41 41   create mode 100644 notes.txt
42 42  {{/noformat}}
43 -
44 -The file is now stored in the local history of your repository.
45 45  )))
46 46  1. Edit {{code language="none"}}notes.txt{{/code}}:\\
47 47  11. Replace "fixed" with "infinite" in line 1.
48 48  11. Replace "... (TODO)" with "a finite state machine" in line 4.
49 49  1. (((
50 -View the status of your current working copy:
51 +Commit the modified content to your local repository:
51 51  
52 52  {{noformat}}
53 -$ git status
54 -# On branch master
55 -# Changed but not updated:
56 -# (use "git add <file>..." to update what will be committed)
57 -# (use "git checkout -- <file>..." to discard changes in working directory)
58 -#
59 -# modified: notes.txt
60 -#
61 -no changes added to commit (use "git add" and/or "git commit -a")
62 -{{/noformat}}
63 -)))
64 -1. (((
65 -Mark the modified file to include it in the next commit, then view the status again and compare with the previous output:
66 -
67 -{{noformat}}
68 68  $ git add notes.txt
69 -$ git status
70 -# On branch master
71 -# Changes to be committed:
72 -# (use "git reset HEAD <file>..." to unstage)
73 -#
74 -# modified: notes.txt
75 -#
76 -{{/noformat}}
77 -)))
78 -1. (((
79 -Commit the modified content to your local repository and view the status:
80 -
81 -{{noformat}}
82 82  $ git commit -m "modified tape length, found a controller for tape head"
83 -[master 52e2d49] modified tape length, found a controller for tape head
56 +[master 3f28a0e] modified tape length, found a controller for tape head
84 84   1 files changed, 2 insertions(+), 2 deletions(-)
85 -$ git status
86 -# On branch master
87 -nothing to commit (working directory clean)
88 88  {{/noformat}}
89 89  )))
90 90  
91 -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:
61 +After the preceding steps you have two commits in your local repository, each with one file in the index. You have different options for viewing these commits:
92 92  
93 93  {{noformat}}
94 94  $ git log
95 -commit 52e2d4946791c2725015853e5e261ce143c6fe8a
65 +commit 3f28a0e473bf3da4aff34a09fed838fe033f3bb5
96 96  Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
97 -Date: Mon Oct 15 15:00:14 2012 +0200
67 +Date: Mon Oct 15 14:30:24 2012 +0200
98 98  
99 99   modified tape length, found a controller for tape head
100 100  
... ... @@ -104,10 +104,10 @@
104 104  
105 105   wrote some first notes
106 106   
107 -$ git show 52e2d49
108 -commit 52e2d4946791c2725015853e5e261ce143c6fe8a
77 +$ git show 3f28a0e
78 +commit 3f28a0e473bf3da4aff34a09fed838fe033f3bb5
109 109  Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
110 -Date: Mon Oct 15 15:00:14 2012 +0200
80 +Date: Mon Oct 15 14:30:24 2012 +0200
111 111  
112 112   modified tape length, found a controller for tape head
113 113  
... ... @@ -124,206 +124,4 @@
124 124  + * The head is controlled by a finite state machine
125 125  {{/noformat}}
126 126  
127 -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"]]).
128 -
129 -= Branching and Merging =
130 -
131 -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.
132 -
133 -1. (((
134 -Create a branch with name //sketches//:
135 -
136 -{{noformat}}
137 -$ git branch sketches
138 -{{/noformat}}
139 -)))
140 -1. (((
141 -View the list of branches:
142 -
143 -{{noformat}}
144 -$ git branch
145 -* master
146 - sketches
147 -{{/noformat}}
148 -
149 -The star reveals that you are still on the old {{code language="none"}}master{{/code}} branch.
150 -)))
151 -1. (((
152 -Switch to the new branch:
153 -
154 -{{noformat}}
155 -$ git checkout sketches
156 -Switched to branch 'sketches'
157 -$ git branch
158 - master
159 -* sketches
160 -{{/noformat}}
161 -
162 -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}}.
163 -)))
164 -1. (((
165 -Download and add the new file [[attach:examples.txt]]{{code language="none"}}{{/code}}:
166 -
167 -{{noformat}}
168 -$ git add examples.txt
169 -$ git commit -m "wrote first examples"
170 -[sketches cd63135] wrote first examples
171 - 1 files changed, 20 insertions(+), 0 deletions(-)
172 - create mode 100644 examples.txt
173 -{{/noformat}}
174 -
175 -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.
176 -)))
177 -1. (((
178 -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:
179 -
180 -{{noformat}}
181 -$ git checkout master
182 -Switched to branch 'master'
183 -$ git merge sketches
184 -Updating 52e2d49..cd63135
185 -Fast-forward
186 - examples.txt | 20 ++++++++++++++++++++
187 - 1 files changed, 20 insertions(+), 0 deletions(-)
188 - create mode 100644 examples.txt
189 -{{/noformat}}
190 -
191 -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}}.
192 -)))
193 -1. (((
194 -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:
195 -
196 -{{noformat}}
197 -$ git add notes.txt
198 -$ git commit -m "added reference to the new examples"
199 -[master a5e244f] added reference to the new examples
200 - 1 files changed, 2 insertions(+), 1 deletions(-)
201 -{{/noformat}}
202 -)))
203 -1. (((
204 -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.
205 -
206 -{{noformat}}
207 -$ git checkout sketches
208 -Switched to branch 'sketches'
209 -{{/noformat}}
210 -
211 -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.
212 -
213 -{{noformat}}
214 -$ git add examples.txt
215 -$ git commit -m "added another example"
216 -[sketches 55a9cb1] added another example
217 - 1 files changed, 5 insertions(+), 0 deletions(-)
218 -{{/noformat}}
219 -
220 -Now our two branches have //diverged//, which means that they cannot be fast-forwarded anymore.
221 -)))
222 -1. (((
223 -Merge the {{code language="none"}}master{{/code}} branch into {{code language="none"}}sketches{{/code}}:
224 -
225 -{{noformat}}
226 -$ git merge master
227 -Merge made by recursive.
228 - notes.txt | 3 ++-
229 - 1 files changed, 2 insertions(+), 1 deletions(-)
230 -{{/noformat}}
231 -
232 -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.
233 -)))
234 -1. (((
235 -Add a commit in each of the two branches using the commands you have already learned.
236 -1. Check out {{code language="none"}}master{{/code}}.
237 -1. (((
238 -Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
239 -
240 -{{noformat nopanel="true"}}
241 - * The finite state machine has an initial state and one or more final states
242 -{{/noformat}}
243 -)))
244 -1. Commit the change to {{code language="none"}}notes.txt{{/code}}.
245 -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).
246 -1. (((
247 -Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
248 -
249 -{{noformat nopanel="true"}}
250 - * Each state transition can trigger head movement and data read/write
251 -{{/noformat}}
252 -)))
253 -1. Commit the change to {{code language="none"}}notes.txt{{/code}}.
254 -)))
255 -1. (((
256 -Merge the {{code language="none"}}master{{/code}} branch into the current branch ({{code language="none"}}sketches{{/code}}):
257 -
258 -{{noformat}}
259 -$ git merge master
260 -Auto-merging notes.txt
261 -CONFLICT (content): Merge conflict in notes.txt
262 -Automatic merge failed; fix conflicts and then commit the result.
263 -{{/noformat}}
264 -
265 -As expected, the branches could not be merged automatically, since both branches modified the same line in the same file.
266 -)))
267 -1. (((
268 -Use the {{code language="none"}}status{{/code}} command to see the list of affected files:
269 -
270 -{{noformat}}
271 -$ git status
272 -# On branch sketches
273 -# Unmerged paths:
274 -# (use "git add/rm <file>..." as appropriate to mark resolution)
275 -#
276 -# both modified: notes.txt
277 -#
278 -no changes added to commit (use "git add" and/or "git commit -a")
279 -{{/noformat}}
280 -)))
281 -1. (((
282 -The modified {{code language="none"}}notes.txt{{/code}} should now contain the following text:
283 -
284 -{{noformat nopanel="true"}}
285 -<<<<<<< HEAD
286 - * Each state transition can trigger head movement and data read/write
287 -=======
288 - * The finite state machine has an initial state and one or more final states
289 ->>>>>>> master
290 -{{/noformat}}
291 -
292 -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}}).
293 -)))
294 -1. (((
295 -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:
296 -
297 -{{noformat}}
298 -$ git commit
299 -[sketches 21d5ddb] Merge branch 'master' into sketches
300 -$ git show 21d5ddb
301 -commit 21d5ddbbcba4e36464653a2a550dbf595ead921f
302 -Merge: 17f75c7 8af2d50
303 -Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
304 -Date: Tue Oct 16 10:44:09 2012 +0200
305 -
306 - Merge branch 'master' into sketches
307 -
308 - Conflicts:
309 - notes.txt
310 -
311 -diff --cc notes.txt
312 -index 8f72873,bb81298..ba94a08
313 ---- a/notes.txt
314 -+++ b/notes.txt
315 -@@@ -2,6 -2,6 +2,7 @@@
316 - * Tape head can read or write data
317 - * Tape head can move left or right
318 - * The head is controlled by a finite state machine
319 - + * Each state transition can trigger head movement and data read/write
320 -+ * The finite state machine has an initial state and one or more final states
321 - see some examples in 'examples.txt'
322 -{{/noformat}}
323 -)))
324 -
325 -The {{code language="none"}}gitk{{/code}} tool should now display this graph:
326 -
327 -[[image:attach:turing-graph-01.png]]
328 -
329 -
97 +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"}}3f28a0e{{/code}} to identify the second commit.
Confluence.Code.ConfluencePageClass[0]
Id
... ... @@ -1,1 +1,1 @@
1 -2982135
1 +2982118
URL
... ... @@ -1,1 +1,1 @@
1 -https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982135/Git
1 +https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982118/Git