Child pages
  • Git
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 31 Next »

This tutorial will address the source code management (SCM) tool named Git. 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.

More in-depth documentation can be found on the official home page, which mentions books, videos, and links to other tutorials and references.

Contents

Creating Commits

  1. Read the Git for Computer Scientists introduction (skip this if you are already familiar with Git). 
  2. For Linux, Git is available in its own package. Windows users can install msysGit. For MacOS, Git is available as part of Xcode; if you cannot install that, use Git for OSX.
  3. Configure your name and email address (will be included in all commits you create):

    $ git config --global --add user.name "Your Name"
    $ git config --global --add user.email "<login>@informatik.uni-kiel.de"
  4. Create a local repository for the "Turing Project":

    $ 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.

  5. Add and commit some content: copy notes.txt to your turing directory.

    $ 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.

  6. Edit notes.txt:
    1. Replace "fixed" with "infinite" in line 1.
    2. Replace "... (TODO)" with "a finite state machine" in line 4.
  7. View the status of your current working copy:

    $ 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")
  8. Mark the modified file to include it in the next commit, then view the status again and compare with the previous output:

    $ git add notes.txt
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       modified:   notes.txt
    #
  9. Commit the modified content to your local repository and view the status:

    $ 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)

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:

$ git log
commit 52e2d4946791c2725015853e5e261ce143c6fe8a
Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
Date:   Mon Oct 15 15:00:14 2012 +0200

    modified tape length, found a controller for tape head

commit 2e73b34ac44480773fc0e52875b7353a087d8c6d
Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
Date:   Mon Oct 15 12:14:06 2012 +0200

    wrote some first notes
 
$ git show 52e2d49
commit 52e2d4946791c2725015853e5e261ce143c6fe8a
Author: Miro Spoenemann <msp@informatik.uni-kiel.de>
Date:   Mon Oct 15 15:00:14 2012 +0200

    modified tape length, found a controller for tape head

diff --git a/notes.txt b/notes.txt
index 4ded2b3..bd422b3 100644
--- a/notes.txt
+++ b/notes.txt
@@ -1,5 +1,5 @@
- * A tape with fixed length
+ * A tape with infinite length
  * Tape head can read or write data
  * Tape head can move left or right
- * The head is controlled by ... (TODO)
+ * The head is controlled by a finite state machine

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 52e2d49 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 gitk to get an overview of your commits (a better alternative available for MacOS is GitX).

Branching and Merging

In the previous section you have created two commits on the default branch, which is named master. Now you will create a new branch and commit there, thus adding complexity to the commit graph.

  1. Create a branch with name sketches:

    $ git branch sketches
  2. View the list of branches:

    $ git branch
    * master
      sketches

    The star reveals that you are still on the old master branch.

  3. Switch to the new branch:

    $ 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 of git checkout.

  4. Download and add the new file examples.txt:

    $ 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 the sketches branch now has three commits, while master is still at the second commit.

  5. Merging the sketches branch into master means that all changes that have been made in sketches are also applied to master. In order to perform this merge, we have to check out the master branch first:

    $ 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 the sketches branch, the merge could be done by simply changing the head pointer of master to be the same as the head of sketches.

  6. Now add the line "see some examples in 'examples.txt'" to the file notes.txt and commit this change in the current branch:

    $ 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(-)
  7. Switch back to the sketches branch and commit something there. Note that the checkout command modifies your working copy, hence you have to update your text editor's content if you opened one of the files.

    $ 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 file examples.txt, then commit.

    $ git add examples.txt
    $ git commit -m "added another example"
    [sketches 55a9cb1] added another example
     1 files changed, 5 insertions(+), 0 deletions(-)

    Now our two branches have diverged, which means that they cannot be fast-forwarded anymore.

  8. Merge the master branch into sketches:

    $ 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.

  9. Add a commit in each of the two branches using the commands you have already learned.
    1. Check out master.
    2. Insert the following line after line 4 of notes.txt:

       * The finite state machine has an initial state and one or more final states
    3. Commit the change to notes.txt.
    4. Check out sketches (make sure to refresh your text editor so notes.txt is reset to its previous state, without the change made above).
    5. Insert the following line after line 4 of notes.txt:

       * Each state transition can trigger head movement and data read/write
    6. Commit the change to notes.txt.
  10. Merge the master branch into the current branch (sketches):

    $ 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.

  11. Use the status command to see the list of affected files:

    $ 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")
  12. The modified notes.txt should now contain the following text:

    <<<<<<< 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 to master. 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 notes.txt).

  13. Use the add command to mark notes.txt as resolved. Entering git 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:

    $ 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'

The gitk tool should now display this graph:

 

  • No labels