Basic Git Workflow

The git model

The main components to consider when dealing with a simple git workflow are:

  • the working directory
  • the staging area
  • the repository
  • the .git directory

We use the various git commmands to move data between these areas. In the image below (taken from the Software Carpentry's Version Control with Git lesson, CC-BY-4.0) we see a file in the Working Directory that has some exisiting lines in black and a new addition in green. git add is used to move the change, the additional green bit, in to the staging area. Once all the changes that make up the logical change have been assembled they are stored in the repositrory with git commit.

staging and repo

Setting up git

In order for git to record who made changes we need to configure our name and email address.

$ git config --global user.name "Alice Researcher"
$ git config --global user.email "a.researcher@gw4.ac.uk"

There are other configuration options, but these are the only ones we need to set for now.

git status

git status is a very handy command for figuring our where you are and what is going on.

If you aren't in a working directory you'll get a message like

train31@myVM:~$ git status
fatal: Not a git repository (or any of the parent directories): .git

Otherwise you'll see something like

train31@myVM1:~/git1/example-basic$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

We'll cover branches in few minutes. The last line is what we are looking for.

git diff

If we make a change to one of the tracked files in the working directory git will notice

train31@myVM1:~/git1/example-basic$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

We can examine exactly what the modification was with git diff

train31@myVM1:~/git1/example-basic$ git diff 
diff --git a/README.md b/README.md
index cfebfd9..9708379 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,9 @@ ex-basic

 This repository has just 5 basic commits on master by three different coders, providing a basic commit structure for learning exploring Git commands.

+Adding a new line in the README will be a new commit.
+
 ## Usage

 * Using `git log` to review simple history
-* Filtering `git log` with `--author` option
\ No newline at end of file
+* Filtering `git log` with `--author` option

git add

The command git add takes the changes and moves them to the staging area

train31@myVM1:~/git1/example-basic$ git add README.md

Now the status is

train31@myVM1:~/git1/example-basic$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README.md

git commit

Git already automatically records

  • who
  • when
  • what

The only thing it can't determine is why. It is up to you do supply the thinking behind the change.

xkcd CC-BY-NC 2.5 Randall Munroe

If it is a particularly simple change and the reasoning is so obvious that you can use a single line to describe it you can use the -m flag

git commit -m "Short Message explaining the commit"

Otherwise run

git commit

which will open up your editor (in the case of this workshop nano, but this can be set with git config --global core.editor )

Here's a screenshot of a commit message in the process of being edited

  GNU nano 2.5.3 File: .../train31/git1/example-basic/.git/COMMIT_EDITMSG Modified  

Add a line as an example

Here we have a longer description of the commit explaining the 
rationale.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#       modified:   README.md

^G Get Help   ^O Write Out  ^W Where Is   ^K Cut Text   ^J Justify    ^C Cur Pos
^X Exit       ^R Read File  ^\ Replace    ^U Uncut Text ^T To Spell   ^_ Go To Line

Commit Messsage Best Practice

https://chris.beams.io/posts/git-commit/

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters
  3. Capitalize the subject line
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how

git log

train31@myVM1:~/git1/example-basic$ git log -p -1
commit f7a92c7db602425409a5bbb01c6f6d7c3cff9063
Author: Alice Researcher <a.researcher@gw4.ac.uk>
Date:   Wed Dec 6 14:39:29 2017 +0000

    Add a line as an example

    Here we have a longer description of the commit explaining the
    rationale.

diff --git a/README.md b/README.md
index cfebfd9..9708379 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,9 @@ ex-basic

 This repository has just 5 basic commits on master by three different coders, providing a basic commit structure for learning exploring Git commands.

+Adding a new line in the README will be a new commit.
+
 ## Usage

 * Using `git log` to review simple history
-* Filtering `git log` with `--author` option
\ No newline at end of file
+* Filtering `git log` with `--author` option

Exercises

Exercise 1

  • Configure your git with your name and email address

Exercise 2

  • Make a change to a file
  • see what effect this has had on the working directory

Exercise 3

  • examine the difference between the state of the repository and the working directory. Is the exactly and only what you wanted to change?

Exercise 4

  • Stage the change so that it is ready to be commited
  • Examine the state now

Exercise 5

  • Commit your change

Exercise 6

  • Look at the repository history and see how your commit is displayed