The main components to consider when dealing with a simple git
workflow are:
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
.
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
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.
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
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 already automatically records
The only thing it can't determine is why. It is up to you do supply the thinking behind the change.
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"
If you forget the -m "Commit message"
the git will automatically open the default editor to force you to provide a commit message:
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
https://chris.beams.io/posts/git-commit/
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
git config --global user.name "Alice Researcher"
git config --global user.email "a.researcher@gw4.ac.uk"
nano README.md
git status
git diff
git add README.md
git status
git commit -m "Add more context to the readme"
Or, you could have used
git commit
And then used nano
to write a full commit message.
git log
try the options:
git log -p -1
what is being displayed?