# Wiki source code of Git

Last modified by Richard Kreissig on 2025/01/30 12:04
Export
Office Formats
Export as PDF Export as ODT Export as RTF
Other Formats

Select the pages to export:

select all/ none

Legend:
  Created Page
  Modified Extension Page
  Clean Extension Page

Export as HTML

Show last authors

author | version | line-number | content
--- | --- | --- | ---
|  | 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 |
|  | 3 | More in-depth Git 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. Furthermore, the shell command {{code language="none"}}git help{{/code}} lists the most commonly used Git commands, and {{code language="none"}}git help <command>{{/code}} gives very detailed documentation for the specified Git command.
|  | 4 |
|  | 5 | === Contents ===
|  | 6 |
|  | 7 |
|  | 8 |
|  | 9 | {{toc maxLevel="2" style="circle"/}}
|  | 10 |
|  | 11 | = Creating Commits =
|  | 12 |
|  | 13 | Most steps of this tutorial are done by typing shell commands. The grey boxes contain the commands you should enter, preceded by a {{code language="none"}}${{/code}} symbol, and followed by their output. While you may copy & paste these commands, some of them may require modifications to adapt them to your own projects. The output will be slightly different for many commands when you enter them, since it also depends on parameters such as the user name and time of execution.
|  | 14 |
|  | 15 | 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).
|  | 16 | 1. For Linux, Git is available in its own package. Windows users can install [[msysGit>>url:http://msysgit.github.com/||shape="rect"]]. For Mac OSX, 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"]].
|  | 17 | 1. (((
|  | 18 | Configure your name and email address (will be included in all commits you create):
|  | 19 |
|  | 20 | {{code}}
|  | 21 | $ git config --global --add user.name "Your Name"
|  | 22 | $ git config --global --add user.email "<login>@informatik.uni-kiel.de"
|  | 23 | {{/code}}
|  | 24 | )))
|  | 25 | 1. (((
|  | 26 | Create a local repository for the "//Turing Project//":
|  | 27 |
|  | 28 | {{code}}
|  | 29 | $ mkdir turing
|  | 30 | $ cd turing
|  | 31 | $ git init
|  | 32 | Initialized empty Git repository in ~/turing/.git/
|  | 33 | {{/code}}
|  | 34 |
|  | 35 | The {{code language="none"}}.git{{/code}} subdirectory contains all history and metadata of the repository. You should not modify it. The {{code language="none"}}turing{{/code}} directory contains the //working copy//, that is the currently checked-out snapshot. You work by modifying your working copy and committing the modifications to the repository (contained in {{code language="none"}}.git{{/code}}).
|  | 36 | )))
|  | 37 | 1. (((
|  | 38 | Add and commit some content: copy [[attach:notes.txt]]{{code language="none"}}{{/code}} to your {{code language="none"}}turing{{/code}} directory.
|  | 39 |
|  | 40 | {{code}}
|  | 41 | $ git add notes.txt
|  | 42 | $ git commit -m "wrote some first notes"
|  | 43 | [master (root-commit) 2e73b34] wrote some first notes
|  | 44 | 1 files changed, 5 insertions(+), 0 deletions(-)
|  | 45 | create mode 100644 notes.txt
|  | 46 | {{/code}}
|  | 47 |
|  | 48 | The file is now stored in the local history of your repository.
|  | 49 | )))
|  | 50 | 1. Edit {{code language="none"}}notes.txt{{/code}}:
|  | 51 | 11. Replace "fixed" with "infinite" in line 1.
|  | 52 | 11. Replace "... (TODO)" with "a finite state machine" in line 4.
|  | 53 | 1. (((
|  | 54 | View the status of your current working copy:
|  | 55 |
|  | 56 | {{code}}
|  | 57 | $ git status
|  | 58 | # On branch master
|  | 59 | # Changed but not updated:
|  | 60 | # (use "git add <file>..." to update what will be committed)
|  | 61 | # (use "git checkout -- <file>..." to discard changes in working directory)
|  | 62 | #
|  | 63 | # modified: notes.txt
|  | 64 | #
|  | 65 | no changes added to commit (use "git add" and/or "git commit -a")
|  | 66 | {{/code}}
|  | 67 | )))
|  | 68 | 1. (((
|  | 69 | Mark the modified file to include it in the next commit, then view the status again and compare with the previous output:
|  | 70 |
|  | 71 | {{code}}
|  | 72 | $ git add notes.txt
|  | 73 | $ git status
|  | 74 | # On branch master
|  | 75 | # Changes to be committed:
|  | 76 | # (use "git reset HEAD <file>..." to unstage)
|  | 77 | #
|  | 78 | # modified: notes.txt
|  | 79 | #
|  | 80 | {{/code}}
|  | 81 | )))
|  | 82 | 1. (((
|  | 83 | Commit the modified content to your local repository and view the status:
|  | 84 |
|  | 85 | {{code}}
|  | 86 | $ git commit -m "modified tape length, found a controller for tape head"
|  | 87 | [master 52e2d49] modified tape length, found a controller for tape head
|  | 88 | 1 files changed, 2 insertions(+), 2 deletions(-)
|  | 89 | $ git status
|  | 90 | # On branch master
|  | 91 | nothing to commit (working directory clean)
|  | 92 | {{/code}}
|  | 93 | )))
|  | 94 |
|  | 95 | 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:
|  | 96 |
|  | 97 | {{code}}
|  | 98 | $ git log
|  | 99 | commit 52e2d4946791c2725015853e5e261ce143c6fe8a
|  | 100 | Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
|  | 101 | Date: Mon Oct 15 15:00:14 2012 +0200
|  | 102 |
|  | 103 | modified tape length, found a controller for tape head
|  | 104 |
|  | 105 | commit 2e73b34ac44480773fc0e52875b7353a087d8c6d
|  | 106 | Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
|  | 107 | Date: Mon Oct 15 12:14:06 2012 +0200
|  | 108 |
|  | 109 | wrote some first notes
|  | 110 |
|  | 111 | $ git show 52e2d49
|  | 112 | commit 52e2d4946791c2725015853e5e261ce143c6fe8a
|  | 113 | Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
|  | 114 | Date: Mon Oct 15 15:00:14 2012 +0200
|  | 115 |
|  | 116 | modified tape length, found a controller for tape head
|  | 117 |
|  | 118 | diff --git a/notes.txt b/notes.txt
|  | 119 | index 4ded2b3..bd422b3 100644
|  | 120 | --- a/notes.txt
|  | 121 | +++ b/notes.txt
|  | 122 | @@ -1,5 +1,5 @@
|  | 123 | - * A tape with fixed length
|  | 124 | + * A tape with infinite length
|  | 125 | * Tape head can read or write data
|  | 126 | * Tape head can move left or right
|  | 127 | - * The head is controlled by ... (TODO)
|  | 128 | + * The head is controlled by a finite state machine
|  | 129 | {{/code}}
|  | 130 |
|  | 131 | 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 Mac OSX is [[GitX>>url:http://gitx.frim.nl/||shape="rect"]]).
|  | 132 |
|  | 133 | = Branching and Merging =
|  | 134 |
|  | 135 | 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. In general, you may create as many local branches as you like, since they are simple to use and can be a great tool to structure your work.
|  | 136 |
|  | 137 | 1. (((
|  | 138 | Create a branch with name //sketches//:
|  | 139 |
|  | 140 | {{code}}
|  | 141 | $ git branch sketches
|  | 142 | {{/code}}
|  | 143 | )))
|  | 144 | 1. (((
|  | 145 | View the list of branches:
|  | 146 |
|  | 147 | {{code}}
|  | 148 | $ git branch
|  | 149 | * master
|  | 150 | sketches
|  | 151 | {{/code}}
|  | 152 |
|  | 153 | The star reveals that you are still on the old {{code language="none"}}master{{/code}} branch.
|  | 154 | )))
|  | 155 | 1. (((
|  | 156 | Switch to the new branch:
|  | 157 |
|  | 158 | {{code}}
|  | 159 | $ git checkout sketches
|  | 160 | Switched to branch 'sketches'
|  | 161 | $ git branch
|  | 162 | master
|  | 163 | * sketches
|  | 164 | {{/code}}
|  | 165 |
|  | 166 | 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}}.
|  | 167 | )))
|  | 168 | 1. (((
|  | 169 | Download and add the new file [[attach:examples.txt]]{{code language="none"}}{{/code}}:
|  | 170 |
|  | 171 | {{code}}
|  | 172 | $ git add examples.txt
|  | 173 | $ git commit -m "wrote first examples"
|  | 174 | [sketches cd63135] wrote first examples
|  | 175 | 1 files changed, 20 insertions(+), 0 deletions(-)
|  | 176 | create mode 100644 examples.txt
|  | 177 | {{/code}}
|  | 178 |
|  | 179 | 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.
|  | 180 | )))
|  | 181 | 1. (((
|  | 182 | 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:
|  | 183 |
|  | 184 | {{code}}
|  | 185 | $ git checkout master
|  | 186 | Switched to branch 'master'
|  | 187 | $ git merge sketches
|  | 188 | Updating 52e2d49..cd63135
|  | 189 | Fast-forward
|  | 190 | examples.txt | 20 ++++++++++++++++++++
|  | 191 | 1 files changed, 20 insertions(+), 0 deletions(-)
|  | 192 | create mode 100644 examples.txt
|  | 193 | {{/code}}
|  | 194 |
|  | 195 | 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}}.
|  | 196 | )))
|  | 197 | 1. (((
|  | 198 | 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:
|  | 199 |
|  | 200 | {{code}}
|  | 201 | $ git add notes.txt
|  | 202 | $ git commit -m "added reference to the new examples"
|  | 203 | [master a5e244f] added reference to the new examples
|  | 204 | 1 files changed, 2 insertions(+), 1 deletions(-)
|  | 205 | {{/code}}
|  | 206 | )))
|  | 207 | 1. (((
|  | 208 | Switch back to the {{code language="none"}}sketches{{/code}} branch and modify it as shown below. 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.
|  | 209 |
|  | 210 | {{code}}
|  | 211 | $ git checkout sketches
|  | 212 | Switched to branch 'sketches'
|  | 213 | {{/code}}
|  | 214 |
|  | 215 | Add the line "{{code language="none"}}Move one step left:{{/code}}" followed by an accordingly updated version of the tape with tape head at the end of the file {{code language="none"}}examples.txt{{/code}}, then commit.
|  | 216 |
|  | 217 | {{code}}
|  | 218 | $ git add examples.txt
|  | 219 | $ git commit -m "added another example"
|  | 220 | [sketches 55a9cb1] added another example
|  | 221 | 1 files changed, 5 insertions(+), 0 deletions(-)
|  | 222 | {{/code}}
|  | 223 |
|  | 224 | Now your two branches have //diverged//, which means that they cannot be fast-forwarded anymore.
|  | 225 | )))
|  | 226 | 1. (((
|  | 227 | Merge the {{code language="none"}}master{{/code}} branch into {{code language="none"}}sketches{{/code}}:
|  | 228 |
|  | 229 | {{code}}
|  | 230 | $ git merge master
|  | 231 | Merge made by recursive.
|  | 232 | notes.txt | 3 ++-
|  | 233 | 1 files changed, 2 insertions(+), 1 deletions(-)
|  | 234 | {{/code}}
|  | 235 |
|  | 236 | 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. See how both the change to {{code language="none"}}notes.txt{{/code}} done in the {{code language="none"}}master{{/code}} branch and the change to {{code language="none"}}examples.txt{{/code}} done in the {{code language="none"}}sketches{{/code}} branch are now contained in the repository state that results from the merge.
|  | 237 | )))
|  | 238 | 1. (((
|  | 239 | Add a commit in each of the two branches using the commands you have already learned.
|  | 240 |
|  | 241 | 1. Check out {{code language="none"}}master{{/code}}.
|  | 242 | 1. (((
|  | 243 | Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
|  | 244 |
|  | 245 | {{code nopanel="true"}}
|  | 246 | * The finite state machine has an initial state and one or more final states
|  | 247 | {{/code}}
|  | 248 | )))
|  | 249 | 1. Commit the change of {{code language="none"}}notes.txt{{/code}}.
|  | 250 | 1. Check out {{code language="none"}}sketches{{/code}} (make sure to refresh your text editor so that {{code language="none"}}notes.txt{{/code}} is reset to its previous state, without the change made above).
|  | 251 | 1. (((
|  | 252 | Insert the following line after line 4 of {{code language="none"}}notes.txt{{/code}}:
|  | 253 |
|  | 254 | {{code nopanel="true"}}
|  | 255 | * Each state transition can trigger head movement and data read/write
|  | 256 | {{/code}}
|  | 257 | )))
|  | 258 | 1. Commit the change of {{code language="none"}}notes.txt{{/code}}.
|  | 259 | )))
|  | 260 | 1. (((
|  | 261 | Merge the {{code language="none"}}master{{/code}} branch into the current branch ({{code language="none"}}sketches{{/code}}):
|  | 262 |
|  | 263 | {{code}}
|  | 264 | $ git merge master
|  | 265 | Auto-merging notes.txt
|  | 266 | CONFLICT (content): Merge conflict in notes.txt
|  | 267 | Automatic merge failed; fix conflicts and then commit the result.
|  | 268 | {{/code}}
|  | 269 |
|  | 270 | As expected, the branches could not be merged automatically, since both branches modified the same line in the same file.
|  | 271 | )))
|  | 272 | 1. (((
|  | 273 | Use the {{code language="none"}}status{{/code}} command to see the list of affected files:
|  | 274 |
|  | 275 | {{code}}
|  | 276 | $ git status
|  | 277 | # On branch sketches
|  | 278 | # Unmerged paths:
|  | 279 | # (use "git add/rm <file>..." as appropriate to mark resolution)
|  | 280 | #
|  | 281 | # both modified: notes.txt
|  | 282 | #
|  | 283 | no changes added to commit (use "git add" and/or "git commit -a")
|  | 284 | {{/code}}
|  | 285 | )))
|  | 286 | 1. (((
|  | 287 | The modified {{code language="none"}}notes.txt{{/code}} should now contain the following text:
|  | 288 |
|  | 289 | {{code nopanel="true"}}
|  | 290 | <<<<<<< HEAD
|  | 291 | * Each state transition can trigger head movement and data read/write
|  | 292 | =======
|  | 293 | * The finite state machine has an initial state and one or more final states
|  | 294 | >>>>>>> master
|  | 295 | {{/code}}
|  | 296 |
|  | 297 | 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}}).
|  | 298 | )))
|  | 299 | 1. (((
|  | 300 | 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:
|  | 301 |
|  | 302 | {{code}}
|  | 303 | $ git commit
|  | 304 | [sketches 21d5ddb] Merge branch 'master' into sketches
|  | 305 | $ git show 21d5ddb
|  | 306 | commit 21d5ddbbcba4e36464653a2a550dbf595ead921f
|  | 307 | Merge: 17f75c7 8af2d50
|  | 308 | Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
|  | 309 | Date: Tue Oct 16 10:44:09 2012 +0200
|  | 310 |
|  | 311 | Merge branch 'master' into sketches
|  | 312 |
|  | 313 | Conflicts:
|  | 314 | notes.txt
|  | 315 |
|  | 316 | diff --cc notes.txt
|  | 317 | index 8f72873,bb81298..ba94a08
|  | 318 | --- a/notes.txt
|  | 319 | +++ b/notes.txt
|  | 320 | @@@ -2,6 -2,6 +2,7 @@@
|  | 321 | * Tape head can read or write data
|  | 322 | * Tape head can move left or right
|  | 323 | * The head is controlled by a finite state machine
|  | 324 | + * Each state transition can trigger head movement and data read/write
|  | 325 | + * The finite state machine has an initial state and one or more final states
|  | 326 | see some examples in 'examples.txt'
|  | 327 | {{/code}}
|  | 328 | )))
|  | 329 |
|  | 330 | The {{code language="none"}}gitk{{/code}} tool should now display this graph:
|  | 331 |
|  | 332 | [[image:attach:turing-graph-01.png]]
|  | 333 |
|  | 334 | = Remote Repositories =
|  | 335 |
|  | 336 | In the previous sections you have worked only with a local repository. The next step is to share this content with a remote repository, which we manage with [[Stash>>url:https://www.atlassian.com/software/stash/overview||shape="rect"]]. (% style="font-size:10.0pt; line-height:13.0pt" %)You will first have to configure your Stash account:
|  | 337 |
|  | 338 | 1. Login to [[our Stash server>>url:http://git.rtsys.informatik.uni-kiel.de/||shape="rect"]] with your Rtsys account information. If you haven't received your password yet, either wait until you have that password or register yourself in Stash (but don't use your IfI login name – that one will be used later when we create your account).
|  | 339 | 1. Through the button in the top right corner, access your profile.
|  | 340 | 1. Switch to the //SSH keys// tab.
|  | 341 | 1. Click //Add Key// and upload a public SSH key that you want to use to access the repository.
|  | 342 | 1*. (% style="font-size:10.0pt; line-height:13.0pt" %)If you don't have an SSH key: use the shell command {{code language="none"}}ssh-keygen{{/code}}, confirm the default destination file {{code language="none"}}~/.ssh/id_rsa{{/code}}, and choose whether to give a passphrase. If you have a passphrase, you need to enter it whenever you use your SSH key for the first time in a session. You can omit the passphrase, but that makes the key less secure. As result, the tool generates a private key {{code language="none"}}~/.ssh/id_rsa{{/code}}, which has to be kept secret, and a public key {{code language="none"}}~/.ssh/id_rsa.pub{{/code}}.
|  | 343 |
|  | 344 | Usually it is sufficient to have only one local copy of a Git repository. However, in this tutorial you will create a second copy in order to "simulate" what can happen if two users access the same remote repository: imagine the directories {{code language="none"}}turing{{/code}} and {{code language="none"}}turing2{{/code}} are each managed by a different user. You will simulate the resulting interference by switching your working directory between these two.
|  | 345 |
|  | 346 | 1. (% style="font-size:10.0pt; line-height:13.0pt" %)Go to [[Stash>>url:http://git.rtsys.informatik.uni-kiel.de/||shape="rect"]] → //Create Project// and call it "personal-<login>", replacing <login> with your own login name. Use your uppercase login name as project key, e.g. "MSP".
|  | 347 | 1. Go to the //Permissions// tab of the project page and add the user "msp" as observer.
|  | 348 | 1. On the project page, select //Create Repository// and name it "turing".
|  | 349 | 1. (% style="font-size:10.0pt; line-height:13.0pt" %)Copy the SSH URL shown in the top right and email it to (%%)[[msp@informatik.uni-kiel.de>>mailto:msp@informatik.uni-kiel.de||shape="rect" style="font-size: 10.0pt;line-height: 13.0pt;"]](% style="font-size:10.0pt; line-height:13.0pt" %). This will serve as proof for your work on this tutorial.
|  | 350 | 1. (((
|  | 351 | Transfer your {{code language="none"}}master{{/code}} branch to the new server-side repository. Replace the URL in the following command by the one copied from Stash:
|  | 352 |
|  | 353 | {{code}}
|  | 354 | $ git remote add stash ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 355 | $ git push stash master
|  | 356 | Counting objects: 15, done.
|  | 357 | Delta compression using up to 16 threads.
|  | 358 | Compressing objects: 100% (13/13), done.
|  | 359 | Writing objects: 100% (15/15), 1.54 KiB, done.
|  | 360 | Total 15 (delta 3), reused 0 (delta 0)
|  | 361 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 362 | * [new branch] master -> master
|  | 363 | {{/code}}
|  | 364 |
|  | 365 | The first command adds a //remote// named "stash" to your local repository, which is just a bookmark for the long URL. The second command transfers the {{code language="none"}}master{{/code}} branch to the server, which is called //pushing//. After that is done, reload the Stash page in your browser, and you see all changes that are transferred to the server-side repository.
|  | 366 | )))
|  | 367 | 1. (((
|  | 368 | Create a local clone of your remote repository (replace the URL accordingly):
|  | 369 |
|  | 370 | {{code}}
|  | 371 | $ cd ..
|  | 372 | $ git clone ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git turing2
|  | 373 | Initialized empty Git repository in /home/msp/tmp/turing2/.git/
|  | 374 | remote: Counting objects: 15, done.
|  | 375 | remote: Compressing objects: 100% (13/13), done.
|  | 376 | remote: Total 15 (delta 3), reused 0 (delta 0)
|  | 377 | Receiving objects: 100% (15/15), done.
|  | 378 | Resolving deltas: 100% (3/3), done.
|  | 379 | $ cd turing2
|  | 380 | {{/code}}
|  | 381 |
|  | 382 | The {{code language="none"}}clone{{/code}} command automatically creates a remote named {{code language="none"}}origin{{/code}} in the new local repository, which is set to the given URL. You will use this second clone to simulate another user with access to the repository.
|  | 383 | )))
|  | 384 | 1. Edit the file {{code language="none"}}examples.txt{{/code}} in the new clone ({{code language="none"}}turing2{{/code}}): replace {{code language="none"}}"a"{{/code}} in line 6 by {{code language="none"}}"c"{{/code}} and correct the tape representations in lines 9, 14, and 19 accordingly. Commit the change.
|  | 385 | 1. (((
|  | 386 | Push the new commit to the server:
|  | 387 |
|  | 388 | {{code}}
|  | 389 | $ git push
|  | 390 | Counting objects: 5, done.
|  | 391 | Delta compression using up to 16 threads.
|  | 392 | Compressing objects: 100% (3/3), done.
|  | 393 | Writing objects: 100% (3/3), 362 bytes, done.
|  | 394 | Total 3 (delta 1), reused 0 (delta 0)
|  | 395 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 396 | 8af2d50..1d1577f master -> master
|  | 397 | {{/code}}
|  | 398 |
|  | 399 | In this case the push command can be used without arguments, which means that it pushes all branches as configured in {{code language="none"}}.git/config{{/code}}:
|  | 400 |
|  | 401 | {{code}}
|  | 402 | $ more .git/config
|  | 403 | [core]
|  | 404 | repositoryformatversion = 0
|  | 405 | filemode = true
|  | 406 | bare = false
|  | 407 | logallrefupdates = true
|  | 408 | [remote "origin"]
|  | 409 | fetch = +refs/heads/*:refs/remotes/origin/*
|  | 410 | url = ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 411 | [branch "master"]
|  | 412 | remote = origin
|  | 413 | merge = refs/heads/master
|  | 414 | {{/code}}
|  | 415 |
|  | 416 | Here the branch {{code language="none"}}master{{/code}} is linked with the remote {{code language="none"}}origin{{/code}}, hence {{code language="none"}}git push{{/code}} does the same as {{code language="none"}}git push origin master{{/code}}.
|  | 417 | )))
|  | 418 | 1. (((
|  | 419 | Go back to the original local repository and check out the {{code language="none"}}master{{/code}} branch:
|  | 420 |
|  | 421 | {{code}}
|  | 422 | $ cd ../turing
|  | 423 | $ git checkout master
|  | 424 | Switched to branch 'master'
|  | 425 | {{/code}}
|  | 426 | )))
|  | 427 | 1. (((
|  | 428 | Merge the {{code language="none"}}sketches{{/code}} branch into {{code language="none"}}master{{/code}}:
|  | 429 |
|  | 430 | {{code}}
|  | 431 | $ git merge sketches
|  | 432 | Updating 8af2d50..21d5ddb
|  | 433 | Fast-forward
|  | 434 | examples.txt | 5 +++++
|  | 435 | notes.txt | 1 +
|  | 436 | 2 files changed, 6 insertions(+), 0 deletions(-)
|  | 437 | {{/code}}
|  | 438 |
|  | 439 | Now your local {{code language="none"}}master{{/code}} branch and the one on the server-side repository have diverged
|  | 440 | )))
|  | 441 | 1. (((
|  | 442 | Fetch the server-side changes:
|  | 443 |
|  | 444 | {{code}}
|  | 445 | $ git fetch stash
|  | 446 | remote: Counting objects: 5, done.
|  | 447 | remote: Compressing objects: 100% (3/3), done.
|  | 448 | remote: Total 3 (delta 1), reused 0 (delta 0)
|  | 449 | Unpacking objects: 100% (3/3), done.
|  | 450 | From ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 451 | 8af2d50..1d1577f master -> stash/master
|  | 452 | {{/code}}
|  | 453 |
|  | 454 | Now the change to {{code language="none"}}examples.txt{{/code}} that was previously committed in the {{code language="none"}}turing2{{/code}} repository is stored in a //remote tracking branch// named {{code language="none"}}stash/master{{/code}}:
|  | 455 |
|  | 456 | {{code}}
|  | 457 | $ git branch -a
|  | 458 | * master
|  | 459 | sketches
|  | 460 | remotes/stash/master
|  | 461 | {{/code}}
|  | 462 |
|  | 463 | You can analyze the remote tracking branch using the {{code language="none"}}log{{/code}} and {{code language="none"}}show{{/code}} commands. However, you should never directly modify a remote tracking branch.
|  | 464 | )))
|  | 465 | 1. (((
|  | 466 | You can merge the remote changes into your local {{code language="none"}}master{{/code}} branch with the following command:
|  | 467 |
|  | 468 | {{code}}
|  | 469 | $ git merge stash/master
|  | 470 | Auto-merging examples.txt
|  | 471 | Merge made by recursive.
|  | 472 | examples.txt | 8 ++++----
|  | 473 | 1 files changed, 4 insertions(+), 4 deletions(-)
|  | 474 | {{/code}}
|  | 475 |
|  | 476 | Since this combination of {{code language="none"}}fetch{{/code}} and {{code language="none"}}merge{{/code}} is used very often, Git offers a shortcut for it, namely the {{code language="none"}}pull{{/code}} command. In this case the according command would have been {{code language="none"}}git pull stash master{{/code}}.
|  | 477 | )))
|  | 478 | 1. (((
|  | 479 | Push the merged branch to the server, and then push the {{code language="none"}}sketches{{/code}} branch, which is not on the server yet:
|  | 480 |
|  | 481 | {{code}}
|  | 482 | $ git push stash master
|  | 483 | Counting objects: 23, done.
|  | 484 | Delta compression using up to 16 threads.
|  | 485 | Compressing objects: 100% (14/14), done.
|  | 486 | Writing objects: 100% (14/14), 1.65 KiB, done.
|  | 487 | Total 14 (delta 4), reused 0 (delta 0)
|  | 488 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 489 | 1d1577f..957f686 master -> master
|  | 490 | $ git push stash sketches
|  | 491 | Total 0 (delta 0), reused 0 (delta 0)
|  | 492 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 493 | * [new branch] sketches -> sketches
|  | 494 | {{/code}}
|  | 495 | )))
|  | 496 | 1. (((
|  | 497 | As next step change your working directory to the second local repository {{code language="none"}}turing2{{/code}}, add the following line to the end of {{code language="none"}}notes.txt{{/code}} in the {{code language="none"}}turing2{{/code}} directory, and commit the change:
|  | 498 |
|  | 499 | {{code nopanel="true"}}
|  | 500 | TODO: formal definition
|  | 501 | {{/code}}
|  | 502 | )))
|  | 503 | 1. (((
|  | 504 | Trying to push this commit to the server results in the following error message:
|  | 505 |
|  | 506 | {{code}}
|  | 507 | $ git push
|  | 508 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 509 | ! [rejected] master -> master (non-fast-forward)
|  | 510 | error: failed to push some refs to 'ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git'
|  | 511 | To prevent you from losing history, non-fast-forward updates were rejected
|  | 512 | Merge the remote changes before pushing again. See the 'Note about
|  | 513 | fast-forwards' section of 'git push --help' for details.
|  | 514 | {{/code}}
|  | 515 |
|  | 516 | This is because you have modified the branch while working in the original {{code language="none"}}turing{{/code}} repository, and these changes have to be merged with the new commit you have just made for {{code language="none"}}notes.txt{{/code}}.
|  | 517 | )))
|  | 518 | 1. (((
|  | 519 | The solution is to apply the {{code language="none"}}pull{{/code}} command followed by the {{code language="none"}}push{{/code}} command:
|  | 520 |
|  | 521 | {{code}}
|  | 522 | $ git pull
|  | 523 | remote: Counting objects: 23, done.
|  | 524 | remote: Compressing objects: 100% (14/14), done.
|  | 525 | remote: Total 14 (delta 4), reused 0 (delta 0)
|  | 526 | Unpacking objects: 100% (14/14), done.
|  | 527 | From ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 528 | 1d1577f..957f686 master -> origin/master
|  | 529 | * [new branch] sketches -> origin/sketches
|  | 530 | Auto-merging notes.txt
|  | 531 | Merge made by recursive.
|  | 532 | examples.txt | 5 +++++
|  | 533 | notes.txt | 1 +
|  | 534 | 2 files changed, 6 insertions(+), 0 deletions(-)
|  | 535 | $ git push
|  | 536 | Counting objects: 10, done.
|  | 537 | Delta compression using up to 16 threads.
|  | 538 | Compressing objects: 100% (6/6), done.
|  | 539 | Writing objects: 100% (6/6), 673 bytes, done.
|  | 540 | Total 6 (delta 2), reused 0 (delta 0)
|  | 541 | To ssh://git@git.rtsys.informatik.uni-kiel.de:7999/MSP/turing.git
|  | 542 | 957f686..b58ded7 master -> master
|  | 543 | {{/code}}
|  | 544 |
|  | 545 | While {{code language="none"}}pull{{/code}} performs a {{code language="none"}}fetch{{/code}} and a {{code language="none"}}merge{{/code}}, {{code language="none"}}push{{/code}} transfers the new merged branch to the server. Note that during the merge operation conflicts can occur. In that case you have to resolve them and commit the changes before you can push. When used without parameters like shown above, {{code language="none"}}pull{{/code}} lookes in {{code language="none"}}.git/config{{/code}} to determine which branches to pull from which remotes.
|  | 546 | )))
|  | 547 | 1. (((
|  | 548 | In order to check out the {{code language="none"}}sketches{{/code}} branch locally, which was previously pushed to the server, simply type the following command:
|  | 549 |
|  | 550 | {{code}}
|  | 551 | $ git checkout sketches
|  | 552 | Branch sketches set up to track remote branch sketches from origin.
|  | 553 | Switched to a new branch 'sketches'
|  | 554 | {{/code}}
|  | 555 |
|  | 556 | This branch can be pushed and pulled with the server in the same way as you did for the {{code language="none"}}master{{/code}} branch. Never check out {{code language="none"}}origin/sketches{{/code}}, since that is a remote tracking branch!
|  | 557 | )))
|  | 558 |
|  | 559 | The {{code language="none"}}master{{/code}} branch should look like this:
|  | 560 |
|  | 561 | [[image:attach:turing-graph-02.png]]
|  | 562 |
|  | 563 | = Other Useful Commands =
|  | 564 |
|  | 565 | This section contains optional steps that you don't need to push online, but can be useful for you to learn.
|  | 566 |
|  | 567 | === Ignoring Files ===
|  | 568 |
|  | 569 | While working on his Machine, Alan Turing has produced a temporary file {{code language="none"}}experiments.tmp{{/code}}, which he does not want to commit in the repository:
|  | 570 |
|  | 571 | {{code}}
|  | 572 | $ git status
|  | 573 | # On branch master
|  | 574 | # Untracked files:
|  | 575 | # (use "git add <file>..." to include in what will be committed)
|  | 576 | #
|  | 577 | # experiments.tmp
|  | 578 | nothing added to commit but untracked files present (use "git add" to track)
|  | 579 | {{/code}}
|  | 580 |
|  | 581 | Since the extra mention of that file can make Git's status reports unnecessarily cluttered, Alan wants to ignore it permanently. Help him by adding a {{code language="none"}}.gitignore{{/code}} file to the repository:
|  | 582 |
|  | 583 | {{code}}
|  | 584 | $ echo "*.tmp" > .gitignore
|  | 585 | $ git add .gitignore
|  | 586 | $ git commit -m "added ignore file"
|  | 587 | [master 738ce4c] added ignore file
|  | 588 | 1 files changed, 1 insertions(+), 0 deletions(-)
|  | 589 | create mode 100644 .gitignore
|  | 590 | $ git status
|  | 591 | # On branch master
|  | 592 | # Your branch is ahead of 'origin/master' by 1 commit.
|  | 593 | #
|  | 594 | nothing to commit (working directory clean)
|  | 595 | {{/code}}
|  | 596 |
|  | 597 | Now the experiments.tmp{{code language="none"}}{{/code}} file is not considered when viewing the status. You can add arbitrary file name patterns to the {{code language="none"}}.gitignore{{/code}} file; for example it is a good idea to ignore {{code language="none"}}*.class{{/code}}, which are binary files generated for Java projects.
|  | 598 |
|  | 599 | === Discarding Changes ===
|  | 600 |
|  | 601 | While working on his Machine, Alan Turing has made some changes to notes.txt that he later found out to be nonsense:
|  | 602 |
|  | 603 | {{code}}
|  | 604 | $ git status
|  | 605 | # On branch master
|  | 606 | # Changed but not updated:
|  | 607 | # (use "git add <file>..." to update what will be committed)
|  | 608 | # (use "git checkout -- <file>..." to discard changes in working directory)
|  | 609 | #
|  | 610 | # modified: notes.txt
|  | 611 | #
|  | 612 | no changes added to commit (use "git add" and/or "git commit -a")
|  | 613 | {{/code}}
|  | 614 |
|  | 615 | Help Alan by restoring the last committed state of that file:
|  | 616 |
|  | 617 | {{code}}
|  | 618 | $ git checkout HEAD notes.txt
|  | 619 | $ git status
|  | 620 | # On branch master
|  | 621 | nothing to commit (working directory clean)
|  | 622 | {{/code}}
|  | 623 |
|  | 624 | Instead of HEAD, which is the last commit on the current branch, you can also name any other branch or commit hash. In that case you would have to commit the change to make it permanent. While resolving conflicts it is possible to use {{code language="none"}}--theirs{{/code}} or {{code language="none"}}--ours{{/code}} instead of HEAD, which replaces the whole content of the respective file by their version (the one on the remote branch) or our version (the one on the current branch).
|  | 625 |
|  | 626 | A more brute-force option is using the {{code language="none"}}reset{{/code}} command:
|  | 627 |
|  | 628 | {{code}}
|  | 629 | $ git reset --hard
|  | 630 | HEAD is now at b58ded7 Merge branch 'master' of git.rtsys.informatik.uni-kiel.de:7999/MSP/turing
|  | 631 | {{/code}}
|  | 632 |
|  | 633 | This resets //all// changes to the working copy to the head of the current branch, so use it with caution! However, {{code language="none"}}reset{{/code}} does not remove unstaged files. In order to do that in one command, use {{code language="none"}}clean{{/code}}:
|  | 634 |
|  | 635 | {{code}}
|  | 636 | $ git status
|  | 637 | # On branch master
|  | 638 | # Untracked files:
|  | 639 | # (use "git add <file>..." to include in what will be committed)
|  | 640 | #
|  | 641 | # test1.tmp
|  | 642 | # test2.tmp
|  | 643 | nothing added to commit but untracked files present (use "git add" to track)
|  | 644 | $ git clean -f
|  | 645 | Removing test1.tmp
|  | 646 | Removing test2.tmp
|  | 647 | {{/code}}
|  | 648 |
|  | 649 | === Rebasing ===
|  | 650 |
|  | 651 | Consider the following situation:
|  | 652 |
|  | 653 | [[image:attach:turing-graph-03.png]]
|  | 654 |
|  | 655 | If you want to merge the changes made on the {{code language="none"}}master{{/code}} branch into the {{code language="none"}}sketches{{/code}} branch, the normal way is to use the {{code language="none"}}merge{{/code}} command and create a merge commit. However, the {{code language="none"}}rebase{{/code}} command gives an interesting alternative to that: it reapplies all commits done in the current branch starting from a given reference.
|  | 656 |
|  | 657 | {{code}}
|  | 658 | $ git rebase master
|  | 659 | First, rewinding head to replay your work on top of it...
|  | 660 | Applying: added another example
|  | 661 | Applying: state transitions
|  | 662 | Using index info to reconstruct a base tree...
|  | 663 | Falling back to patching base and 3-way merge...
|  | 664 | Auto-merging notes.txt
|  | 665 | {{/code}}
|  | 666 |
|  | 667 | Afterwards the commit graph looks like this:
|  | 668 |
|  | 669 | [[image:attach:turing-graph-04.png]]
|  | 670 |
|  | 671 | The two commits made in {{code language="none"}}sketches{{/code}} are reapplied starting from the head of the {{code language="none"}}master{{/code}} branch. The resulting structure of commits is much cleaner than before. rebase{{code language="none"}}{{/code}} even allows to squeeze multiple commits into one. Note that in this example a merge conflict had to be resolved in the same way as it was done in Section "Branching and Merging"; instead of committing the resolved file, the rebase command is resumed with {{code language="none"}}git rebase --continue{{/code}}.
|  | 672 |
|  | 673 | {{warning}}
|  | 674 | Never rebase a branch that is already pushed online! Due to the structural change the rebased branch is no longer compatible with the previous one, and pushing it will fail, since fast-forward merge is not possible.
|  | 675 | {{/warning}}
|  | 676 |
|  | 677 | === Tagging ===
|  | 678 |
|  | 679 | Finally Alan Turing has made a great success in the development of his Machine, and he would like to fix that stage as "Milestone 1". Help him by tagging the current state of the project:
|  | 680 |
|  | 681 | {{code}}
|  | 682 | $ git tag milestone1
|  | 683 | {{/code}}
|  | 684 |
|  | 685 | Then the head of the current branch is stored under the name {{code language="none"}}milestone1{{/code}}, so it can be found very easily at later stages of the project:
|  | 686 |
|  | 687 | {{code}}
|  | 688 | $ git tag
|  | 689 | milestone1
|  | 690 | $ git checkout milestone1
|  | 691 | Note: checking out 'milestone1'.
|  | 692 |
|  | 693 | You are in 'detached HEAD' state. You can look around, make experimental
|  | 694 | changes and commit them, and you can discard any commits you make in this
|  | 695 | state without impacting any branches by performing another checkout.
|  | 696 |
|  | 697 | If you want to create a new branch to retain commits you create, you may
|  | 698 | do so (now or later) by using -b with the checkout command again. Example:
|  | 699 |
|  | 700 | git checkout -b new_branch_name
|  | 701 |
|  | 702 | HEAD is now at 957f686... Merge remote branch 'stash/master'
|  | 703 | {{/code}}
|  | 704 |
|  | 705 | Tags can also be loaded to the server using the {{code language="none"}}push{{/code}} command.

# Quick Links

KIELER on GitHub
RTSYS Homepage

XWiki 14.4.7