...
Most steps of this tutorial are done by typing shell commands. The grey boxes contain the commands you should enter, preceded by a $
symbol, together with 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.
- Read the Git for Computer Scientists introduction (skip this if you are already familiar with Git).
- For Linux, Git is available in its own package. Windows users can install msysGit. For Mac OSX, Git is available as part of Xcode; if you cannot install that, use Git for OSX.
Configure your name and email address (will be included in all commits you create):
No Format $ git config --global --add user.name "Your Name" $ git config --global --add user.email "<login>@informatik.uni-kiel.de"
Create a local repository for the "Turing Project":
No Format $ mkdir turing $ cd turing $ git init Initialized empty Git repository in ~/turing/.git/
The
.git
subdirectory contains all history and metadata of the repository. You should not modify it. Theturing
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.git
).Add and commit some content: copy
notes.txt
to yourturing
directory.No Format $ git add notes.txt $ git commit -m "wrote some first notes" [master (root-commit) 2e73b34] wrote some first notes 1 files changed, 5 insertions(+), 0 deletions(-) create mode 100644 notes.txt
The file is now stored in the local history of your repository.
- Edit
notes.txt
:- Replace "fixed" with "infinite" in line 1.
- Replace "... (TODO)" with "a finite state machine" in line 4.
View the status of your current working copy:
No Format $ git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: notes.txt # no changes added to commit (use "git add" and/or "git commit -a")
Mark the modified file to include it in the next commit, then view the status again and compare with the previous output:
No Format $ git add notes.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: notes.txt #
Commit the modified content to your local repository and view the status:
No Format $ git commit -m "modified tape length, found a controller for tape head" [master 52e2d49] modified tape length, found a controller for tape head 1 files changed, 2 insertions(+), 2 deletions(-) $ git status # On branch master nothing to commit (working directory clean)
...
Create a branch with name sketches:
No Format $ git branch sketches
View the list of branches:
No Format $ git branch * master sketches
The star reveals that you are still on the old
master
branch.Switch to the new branch:
No Format $ git checkout sketches Switched to branch 'sketches' $ git branch master * sketches
It is also possible to create a branch and switch immediately to it using the option
-b
ofgit checkout
.Download and add the new file
examples.txt
:No Format $ git add examples.txt $ git commit -m "wrote first examples" [sketches cd63135] wrote first examples 1 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 examples.txt
Inspecting the commit graph with
gitk
(or another graphical viewer) you see that thesketches
branch now has three commits, whilemaster
is still at the second commit.Merging the
sketches
branch intomaster
means that all changes that have been made insketches
are also applied tomaster
. In order to perform this merge, we have to check out themaster
branch first:No Format $ git checkout master Switched to branch 'master' $ git merge sketches Updating 52e2d49..cd63135 Fast-forward examples.txt | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 examples.txt
This was a fast-forward merge: since the
master
branch was completely contained in thesketches
branch, the merge could be done by simply changing the head pointer ofmaster
to be the same as the head ofsketches
.Now add the line "
see some examples in 'examples.txt'
" to the filenotes.txt
and commit this change in the current branch:No Format $ git add notes.txt $ git commit -m "added reference to the new examples" [master a5e244f] added reference to the new examples 1 files changed, 2 insertions(+), 1 deletions(-)
Switch back to the
sketches
branch and commit something theremodify it as shown below. Note that thecheckout
command modifies your working copy, hence you have to update your text editor's content if you opened one of the files.No Format $ git checkout sketches Switched to branch 'sketches'
Add the line "
Move one step left:
" and write an updated version of the tape with tape head in the fileexamples.txt
, then commit.No Format $ git add examples.txt $ git commit -m "added another example" [sketches 55a9cb1] added another example 1 files changed, 5 insertions(+), 0 deletions(-)
Now our your two branches have diverged, which means that they cannot be fast-forwarded anymore.
Merge the
master
branch intosketches
:No Format $ git merge master Merge made by recursive. notes.txt | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
Using
gitk
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.- Add a commit in each of the two branches using the commands you have already learned.
- Check out
master
. Insert the following line after line 4 of
notes.txt
:No Format nopanel true * The finite state machine has an initial state and one or more final states
- Commit the change to of
notes.txt
. - Check out
sketches
(make sure to refresh your text editor so thatnotes.txt
is reset to its previous state, without the change made above). Insert the following line after line 4 of
notes.txt
:No Format nopanel true * Each state transition can trigger head movement and data read/write
- Commit the change to of
notes.txt
.
- Check out
Merge the
master
branch into the current branch (sketches
):No Format $ git merge master Auto-merging notes.txt CONFLICT (content): Merge conflict in notes.txt Automatic merge failed; fix conflicts and then commit the result.
As expected, the branches could not be merged automatically, since both branches modified the same line in the same file.
Use the
status
command to see the list of affected files:No Format $ git status # On branch sketches # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: notes.txt # no changes added to commit (use "git add" and/or "git commit -a")
The modified
notes.txt
should now contain the following text:No Format nopanel true <<<<<<< HEAD * Each state transition can trigger head movement and data read/write ======= * The finite state machine has an initial state and one or more final states >>>>>>> master
The upper line is the one committed to
sketches
, while the lower line was committed tomaster
. 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 innotes.txt
).Use the
add
command to marknotes.txt
as resolved. Enteringgit commit
without a message will open a text editor with an automatically created commit message. Just close the editor, and the merge commit is completed:No Format $ git commit [sketches 21d5ddb] Merge branch 'master' into sketches $ git show 21d5ddb commit 21d5ddbbcba4e36464653a2a550dbf595ead921f Merge: 17f75c7 8af2d50 Author: Miro Spoenemann <msp@informatik.uni-kiel.de> Date: Tue Oct 16 10:44:09 2012 +0200 Merge branch 'master' into sketches Conflicts: notes.txt diff --cc notes.txt index 8f72873,bb81298..ba94a08 --- a/notes.txt +++ b/notes.txt @@@ -2,6 -2,6 +2,7 @@@ * Tape head can read or write data * Tape head can move left or right * The head is controlled by a finite state machine + * Each state transition can trigger head movement and data read/write + * The finite state machine has an initial state and one or more final states see some examples in 'examples.txt'
...
In the previous sections you have worked only with a local repository. The next step is to share this content with a remote repository. Later we will use Stash for repository management, but we need to create group accounts for you first, thus you will use another system called Gitorious for now.
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 turing
and turing2
are each managed by a different user. You will simulate the resulting interference by switching your working directory between these two.
- Register to the Gitorious system: https://git.rtsys.informatik.uni-kiel.de/ (use your Institut für Informatik login name and email address)
- Go to your Dashboard → Manage SSH keys → Add SSH key
- Copy & paste the content of your public SSH key.
- If you don't have an SSH key: use the shell command
ssh-keygen
, confirm the default destination file~/.ssh/id_rsa
, 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~/.ssh/id_rsa
, which has to be kept secret, and a public key~/.ssh/id_rsa.pub
.
- If you don't have an SSH key: use the shell command
- Go to Projects → Create a new project and call it "personal-<login>", replacing <login> with your own login name.
- On the next page, create a repository named "turing" (or select Add repository on your project page).
- Once you are on the repository page, copy the URL shown in Clone & push urls.
- Email the copied URL to msp@informatik.uni-kiel.de. This will serve as proof for your work on this tutorial.
Transfer your
master
branch to the new server-side repository. Replace the URL in the following command by the one copied from Gitorious:No Format $ git remote add gitorious git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git $ git push gitorious master Counting objects: 15, done. Delta compression using up to 16 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (15/15), 1.54 KiB, done. Total 15 (delta 3), reused 0 (delta 0) remote: => Syncing Gitorious... [OK] To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git * [new branch] master -> master
The first command adds a remote named "gitorious" to your local repository, which is just a bookmark for the long URL. The second command transfers the
master
branch to the server, which is called pushing. After that is done, reload the Gitorious page in your browser, and you see all changes that are transferred to the server-side repository.Create a local clone of your remote repository (replace the URL accordingly):
No Format $ cd .. $ git clone git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git turing2 Initialized empty Git repository in /home/msp/tmp/turing2/.git/ remote: Counting objects: 15, done. remote: Compressing objects: 100% (13/13), done. remote: Total 15 (delta 3), reused 0 (delta 0) Receiving objects: 100% (15/15), done. Resolving deltas: 100% (3/3), done. $ cd turing2
The
clone
command automatically creates a remote namedorigin
in the new local repository, which is set to the given URL.- Edit the file
examples.txt
in the new clone (turing2
): replace"a"
in line 6 by"c"
and correct the tape representations in lines 9, 14, and 19 accordingly. Commit the change. Push the new commit to the server:
No Format $ git push Counting objects: 5, done. Delta compression using up to 16 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 362 bytes, done. Total 3 (delta 1), reused 0 (delta 0) remote: => Syncing Gitorious... [OK] To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git 8af2d50..1d1577f master -> master
In this case the push command can be used without arguments, which means that it pushes all branches as configured in
.git/config
:No Format $ more .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git [branch "master"] remote = origin merge = refs/heads/master
Here the branch
master
is linked with the remoteorigin
, hencegit push
does the same asgit push origin master
.Go back to the original local repository and check out the
master
branch:No Format $ cd ../turing $ git checkout master Switched to branch 'master'
Merge the
sketches
branch intomaster
:No Format $ git merge sketches Updating 8af2d50..21d5ddb Fast-forward examples.txt | 5 +++++ notes.txt | 1 + 2 files changed, 6 insertions(+), 0 deletions(-)
Now your local
master
branch and the one on the server-side repository have divergedFetch the server-side changes:
No Format $ git fetch gitorious remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git.rtsys.informatik.uni-kiel.de:personal-msp/turing 8af2d50..1d1577f master -> gitorious/master
Now the change to
examples.txt
that was previously committed in theturing2
repository is stored in a remote tracking branch namedgitorious/master
:No Format $ git branch -a * master sketches remotes/gitorious/master
You can analyze the remote tracking branch using the
log
andshow
commands. However, you should never directly modify a remote tracking branch.You can merge the remote changes into your local
master
branch with the following command:No Format $ git merge gitorious/master Auto-merging examples.txt Merge made by recursive. examples.txt | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
Since this combination of
fetch
andmerge
is used very often, Git offers a shortcut for it, namely thepull
command. In this case the according command would have beengit pull gitorious master
.Push the merged branch to the server, and then push the
sketches
branch, which is not on the server yet:No Format $ git push gitorious master Counting objects: 23, done. Delta compression using up to 16 threads. Compressing objects: 100% (14/14), done. Writing objects: 100% (14/14), 1.65 KiB, done. Total 14 (delta 4), reused 0 (delta 0) remote: => Syncing Gitorious... [OK] To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git 1d1577f..957f686 master -> master $ git push gitorious sketches Total 0 (delta 0), reused 0 (delta 0) remote: => Syncing Gitorious... [OK] To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git * [new branch] sketches -> sketches
As next step change your working directory to the second local repository
turing2
, add the following line to the end ofnotes.txt
in theturing2
directory, and commit the change:No Format nopanel true TODO: formal definition
Trying to push this commit to the server results in the following error message:
No Format $ git push To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.
This is because you have modified the branch while working in the original
turing
repository, and these changes have to be merged with the new commit you have just made fornotes.txt
.The solution is to apply the
pull
command followed by thepush
command:No Format $ git pull remote: Counting objects: 23, done. remote: Compressing objects: 100% (14/14), done. remote: Total 14 (delta 4), reused 0 (delta 0) Unpacking objects: 100% (14/14), done. From git.rtsys.informatik.uni-kiel.de:personal-msp/turing 1d1577f..957f686 master -> origin/master * [new branch] sketches -> origin/sketches Auto-merging notes.txt Merge made by recursive. examples.txt | 5 +++++ notes.txt | 1 + 2 files changed, 6 insertions(+), 0 deletions(-) $ git push Counting objects: 10, done. Delta compression using up to 16 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 673 bytes, done. Total 6 (delta 2), reused 0 (delta 0) remote: => Syncing Gitorious... [OK] To git@git.rtsys.informatik.uni-kiel.de:personal-msp/turing.git 957f686..b58ded7 master -> master
While
pull
performs afetch
and amerge
,push
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.In order to check out the
sketches
branch, which was previously pushed to the server, simply type the following command:No Format $ git checkout sketches Branch sketches set up to track remote branch sketches from origin. Switched to a new branch 'sketches'
This branch can be pushed and pulled with the server in the same way as you did for the
master
branch.
...