5.1. Command line workflowΒΆ

The purpose of this section is not to teach you the git command line (there are already many good git tutorials available on the web). Instead, this section illustrates the specific workflow that we use.

A training repository called, appropriately, training/workflow.small, is available for you to practice with, following the script below.

Confirm that you have access to the training repositories

$ ssh dascgitolite@dasc-git.diamond.ac.uk info      # issue command exactly as it appears, don't change the userid
                                                    # if you are prompted for a password, there is an error in the setup
  @C @R   W      training/${USER}/workflow\..*      # you can create and update the training repositories for your userid
     @R_         training/template/workflow\..*     # you can read the training repository templates

Check out the training/workflow.small repository

Start off by cloning a remote repository that you can practice with.

You only have read access to the remote repository, so having cloned it, change your clone to point to your own remote repository (which you can change).

$ rm -rf /scratch/training.small/                                                         # remove unwanted copies
$ mkdir /scratch/training.small/
$ cd /scratch/training.small/
$ git clone dascgitolite@dasc-git.diamond.ac.uk:training/template/workflow.small.git      # clone the training repository
Initialized empty Git repository in /scratch/training.small/workflow.small/.git/
$ cd workflow.small                                                                       # cd into the training repository
$ git remote set-url origin dascgitolite@dasc-git.diamond.ac.uk:training/${USER}/workflow.small.git
$ git push --mirror --verbose                                                             # (re-)create our private remote
Pushing to dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
Initialized empty Git repository in /var/www/dascgitolite_repositories/training/bmn54829/workflow.small.git/
To dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
 * [new branch]      master -> master
 * [new branch]      origin/8.14 -> origin/8.14
 * [new branch]      origin/8.16 -> origin/8.16
 * [new branch]      origin/HEAD -> origin/HEAD
 * [new branch]      origin/master -> origin/master
$ gitk --all &                                                                            # take a look at the repository

Make a simple change (no feature branch required)

Make a simple change to the repository (add a new file). Since the change is small, make it directly on the master branch.

  ### make sure we are on the master branch
$ git branch -a                                                                          # display all branches
* master
  remotes/origin/8.14
  remotes/origin/8.16
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git checkout master                                                                    # switch to the master branch
Already on 'master'

  ### create a new file
$ echo "first line" > simple-file-add.txt

 ### commit the change to our local repository
$ git status                                                                             # display all pending changes
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   simple-file-add.txt
nothing added to commit but untracked files present (use "git add" to track)

$ git add --verbose -A                                                                   # tell git to stage all changes
add 'simple-file-add.txt'

$ git status                                                                             # the new file is now staged
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   simple-file-add.txt
#

$ git commit -m "TRAINING: add new test file"                                            # this commits all staged changes
[master d80b5ac] TRAINING: add new test file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 simple-file-add.txt

$ git status                                                                             # no pending commits in local repository
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

  ### push current branch to the remote repository
$ git push --verbose                                                                     # update the central repository
Pushing to dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
To dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
   8d4269f..d80b5ac  master -> master

Make a complex change (new feature branch used)

Make a more complex change to the repository. Since the change is large, create a new feature branch, and develop the feature there. Once coding (and testing!) is complete, merge the feature branch into the master branch. Branches for features under development tend to live in developer repositories only, not on origin.

New feature branches should have a meaningful name that starts with feature-.

  ### create a new branch and switch to it
$ git branch -a                                                                          # display all branches
* master
  remotes/origin/8.14
  remotes/origin/8.16
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git checkout master                                                                    # switch to the master branch
Already on 'master'
$ git pull                                                                               # do a fetch and merge to make sure master is up-to-date
Already up-to-date.

$ git checkout -b feature-test1 master                                                   # create a new branch off master and switch to it
Switched to a new branch 'feature-test1'

  ### create a new directory and file, and commit
$ mkdir newdir                                                                           # create a new directory
$ echo "first line" > newdir/file1.txt                                                   # and a new file
$ git status                                                                             # display all pending changes
# On branch feature-test1
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   newdir/
nothing added to commit but untracked files present (use "git add" to track)
$ git add --verbose newdir/*                                                             # tell git to stage the new directory and file
add 'newdir/file1.txt'
$ git status                                                                             # changes now staged
# On branch feature-test1
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   newdir/file1.txt
#
$ git commit -m "TRAINING: add new feature dir and file"                                 # this commits all staged changes
[feature-test1 e5a0112] TRAINING: add new feature dir and file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 newdir/file1.txt
$ git status                                                                             # no pending commits
# On branch feature-test1
nothing to commit (working directory clean)

  ### now modify an existing file, create a new file, and commit
$ echo "some text" > newdir/file2.txt                                                    # create a new file
$ sed --in-place 's/line/text/' newdir/file1.txt                                         # modify existing file
$ git status                                                                             # display all pending changes
# On branch feature-test1
# 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:   newdir/file1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   newdir/file2.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add --verbose newdir/*                                                             # tell git to stage the new file and changed file
add 'newdir/file1.txt'
add 'newdir/file2.txt'
$ git status                                                                             # changes now staged
# On branch feature-test1
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   newdir/file1.txt
#   new file:   newdir/file2.txt
#
$ git commit -m "TRAINING: add new feature dir and file"                                 # this commits all staged changes
[feature-test1 767f33e] TRAINING: add new feature dir and file
 2 files changed, 2 insertions(+), 1 deletions(-)
 create mode 100644 newdir/file2.txt
$ git status                                                                             # no pending commits
# On branch feature-test1
nothing to commit (working directory clean)

  ### optionally push the new feature branch to the remote repository
# you might want to do this as a way of backing up the new feature from your local repository
# you might want to do this so another user can see your "in-progress" code (alternatively, they could pull directly from your repository)
$ git push --verbose -u origin feature-test1
Pushing to dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
To dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
 * [new branch]      feature-test1 -> feature-test1
Branch feature-test1 set up to track remote branch feature-test1 from origin.

  ### merge your new feature from the feature branch into the master branch
$ git checkout master                                                                   # switch to the branch we want to merge into
Switched to branch 'master'
$ git pull --verbose                                                                    # we should probably make sure it's up-to-date
From dasc-git.diamond.ac.uk:training/bmn54829/workflow.small
 = [up to date]      feature-test1 -> origin/feature-test1
 = [up to date]      master     -> origin/master
Already up-to-date.
$ git merge --no-ff feature-test1                                                       # merge our feature branch into master
Merge made by recursive.
 newdir/file1.txt |    1 +
 newdir/file2.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 newdir/file1.txt
 create mode 100644 newdir/file2.txt
$ git push origin master                                                                # update the central repository
To dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
   d80b5ac..e133d92  master -> master

  ### delete your feature branch
$ git branch -a
  feature-test1
* master
  remotes/origin/8.14
  remotes/origin/8.16
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-test1
  remotes/origin/master
$ git branch -d feature-test1                                                           # delete the working branch in the local repository
Deleted branch feature-test1 (was 767f33e).
$ git branch -a
* master
  remotes/origin/8.14
  remotes/origin/8.16
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-test1
  remotes/origin/master
$ git push origin :feature-test1                                                        # delete the working branch in the remote repository
To dascgitolite@dasc-git.diamond.ac.uk:training/bmn54829/workflow.small.git
 - [deleted]         feature-test1
$ git branch -a
* master
  remotes/origin/8.14
  remotes/origin/8.16
  remotes/origin/HEAD -> origin/master
  remotes/origin/master