Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lesson 02 #43

Merged
merged 5 commits into from Jun 20, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,76 +1,101 @@
---
title: "Getting started"
title: "Getting started with git"
teaching: 20
exercises: 0
questions:
- "What are repositories and how are they created?"
- "What do `add` and `commit` mean?"
objectives:
- "initiate a git repository on the command line"
- "track and commit files to a git repository"
- "initiate a git repository"
- "track changes to project files"
- "persist changes to a git repository"
keypoints:
- "`git init` can initialize a directory on your computer as a Git repository"
- "Your Git configuration on your computer is important to attributing your work to you"
- "Git uses a two-stage commit process. Changes must be added to the staging area and then committed from there"
---
### Using Git
One of the main barriers to getting started with Git is the language. Although some of the language used in Git is fairly self-explanatory, other terms are not so clear. The best way to learn Git language - which consists of a number of verbs, or commands, e.g., add, commit, always preceded by the word Git - is by using it, but having an overview of the language and the way Git is used will provide a good starting point.

<!demonstrate Git commands whilst outlining what they mean>
- "git repositories are data-structures used to track changes to files over time"
- "Git uses a two-stage commit process. Changes must be added to the staging area then committed from there"

### Exercise for our first git repository

We will try to do this session as a group, but those who prefer to go at a slower pace can follow the instructions on the [github page](https://github.com/data-lessons/library-git).
### Using Git

### Some Git commands/language - what they mean and how to use them
One of the main barriers to getting started with git is the language. Although some of the language used in git is
fairly self-explanatory, other terms are not so clear. The best way to get to learn the language - which consists of a
number of verbs such as `add`, `commit` and `push` (precdeded by the 'git') - is by using it, which is what we will be doing during this
lesson. These commands will be explained as we proceed from setting up a new version controlled project to publishing
our own website.

**Repository** A repository is the place where projects and associated changes are stored. Repositories can contain one single readme file or hundreds of different folders making up the source code for extensive projects. We can create repositories in a number of different ways; we can make our own from scratch, we can fork (copy) an existing repository or we can create a Git repository from an existing folder we have been working on.
We will try to do this session as a group, but those who prefer to go at a slower pace can follow the instructions on
the [GitHub page](https://github.com/data-lessons/library-git).

**init** Create a repository

Whenever we use Git on the command line, we need to preface our command with `git`. This is so the computer knows we are trying to get Git to do something rather than another program.
### Creating a repository

To try out some of the Git commands, we can make a repository for today's session. We can either delete this directory later or keep it as a place to test out new Git commands.
A git **repository** is a data structure used to track changes to a set of project files over time. Repositories are
stored within the same directory as these project files, in a hidden subdirectory called `.git`. We can create a new git
repository by either using the GitHub interface, or via the command line. Let's use the command line to create a git
repository for the experiments that we're going to do today.

We covered the command line last week but we will go over them again. <!explain commands as we go along>
First, we will create a new directory for our project and enter that directory.
<!explain commands as we go along>

~~~
$ mkdir git_test
$ cd git_test
$ git init
$ mkdir hello-world
$ cd hello-world
~~~
{: .bash}

This initiates `git_test` as a git repository.
We will now create an empty git repository to track changes to our project. To do this we will use the **init** command,
which is simply short for *initialise*.

If you do an `ls` now, the repository might seem empty. However, an `ls -a` will show the hidden files, which includes the new file `.git`.
~~~
$ git init
~~~
{: .bash}

This signifies that the directory is now a Git repository. Were the `.git` file ever to be deleted after you have begun committing files, all versioning of the data would be lost.
The `hello-world` directory is now a git repository.

### Tracking files in Git
If you run the `ls` command now the repository might seem empty, however, `ls -a` will show all hidden files, which
in this case includes the new subdirectory `.git`.

Git is able to track changes to files and new files that get added into a Git repository. To track files in Git we need to `add` them and then `commit` them. This is a two-stage process, `add` places the file in the staging area, or "On the stage", `commit` cleans off the stage and adds the changes to the reposiotory. This is a two-stage process that gives you control over what should and should not be included in a particular commit.
Note that whenever we use git via the command line we need to preface our command with `git` so that the computer knows
we are trying to get git to do something, rather than some other program.

We can use `git status` at any time to let us know what Git is up to.
### Displaying the current project's status

If we try it now, we should get something like this
We can run the `git status` command to display the current state of a project. Let's do that now.

~~~
$ git status
~~~
{: .bash}
~~~
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
~~~
{: .output}

This is telling us that we are on the master branch (more on this later) and that we have nothing to commit (nothing to save changes from).
If we use `ls` we can see currently we don't have any files to track. Let's change that by adding a txt file. `touch` is a command which allows us to quickly create an empty file.
The output tells us that we are on the master branch (more on this later) and that we have nothing to commit (no
unsaved changes).


### Adding and committing

We'll now create and persist our first project file. This is a two-stage process. First, we **add** any files for which
we want to save the changes to a staging area, then we **commit** those changes to the repository. This two-stage
process gives us fine-grained control over what should and should not be included in a particular commit.

Let's create a new file using the `touch` command, which is a quick way to create an empty file.

~~~
$ ls
$ touch git_test.txt
$ touch index.md
~~~
{: .bash}

We now have an empty file. If we try `git status` again, we will get the following
When sharing projects with others, it is good practice to include a text document that gives a brief overview of the
project, usually in the form of a README file. The `.md` extension above signifies that we have chosen to format that we have
chosen to use Markdown, a lightweight markup language with plain text formatting syntax. We will explore Markdown a
bit later.

Let's check the status of our project again.

~~~
$ git status
@@ -82,22 +107,23 @@ Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)

git_test.txt
index.md

nothing added to commit but untracked files present (use "git add" to track)
~~~
{: .output}

This status is Git telling us that it has noticed a new file in our directory that we are not yet tracking. With colourised output, the filename will appear in red.

To change this, and to tell Git we want to track any changes we make to git_test.txt, we use `git add`
This status is telling us that git has noticed a new file in our directory that we are not yet tracking. With colourised
output, the filename will appear in red. To change this, and to tell Git we want to track any changes we make to
index.md, we use `git add`.

~~~
$ git add git_test.txt
$ git add index.md
~~~
{: .bash}

This adds our txt file to the **staging area** (the area where Git checks for file changes). Because we are not alerted that this has happened, we might want to use `git status` again.
This adds our Markdown file to the **staging area** (the area where git checks for file changes). To confirm this we
might want to use `git status` again.

~~~
$ git status
@@ -111,50 +137,68 @@ Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: git_test.txt
new file: index.md
~~~
{: .output}

If we opted for colourised output, we can see that the filename has changed colour (from red to green) and Git also tells us that it has recognised a new file.
If we are using colourised output, we will see that the filename has changed colour (from red to green). Git also tells us that there
is a new file to be committed but, before we do that, let's make some changes.


Let's make some changes to this file before we commit it:
Open index.md with any text editor (e.g. Notepad on Windows or TextEdit on Mac OSX) and enter "# Hello, world!". The
hash character is one way of writing a header with Markdown. Now, save the file within your text editor and check if git
has spotted the changes.

~~~
$ nano git_test.txt
$ git status
~~~
{: .bash}

Open the file in nano or another text editor.

We should now be able to add some text to our text file. For now, let's just write 'hello world'. If we try `git status` again. We should get the following message
We should now be able to add some text to our text file.
For now, let's just write 'hello world'. If we try `git status` again. We should get the following message

~~~
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: git_test.txt
modified: index.md
~~~
{: .output}

This lets us know that Git has spotted changes to our txt file but that it hasn't yet 'staged' them. This means Git won't currently record the changes we made. We can add the file to the staging area again.
This lets us know that git has indeed spotted the changes to our file but that it hasn't yet staged them, so let's add
the file to the staging area again.

~~~
$ git add git_test.txt
$ git add index.md
~~~
{: .bash}

We can now **commit** our first changes. Commit is similar to 'saving' a file to Git. However compared to saving, a lot more information about the changes we have made is recorded and thus visible to us later.
We can now **commit** our first changes.
Commit is similar to 'saving' a file to Git.
However, compared to saving, a commit provides a lot more information about the changes we have made,
and this information will remain visible to us later.


~~~
$ git commit -m 'hello world'
$ git commit -m 'Add index.md'
~~~
{: .bash}
~~~
[master (root-commit) 27c3f19] hello world
[master (root-commit) e9e8fd3] Add index.md
1 file changed, 1 insertion(+)
create mode 100644 git_test.txt
create mode 100644 index.md
~~~
{: .output}

We can see that one file changed and we made one insertion which was a line with the text 'hello world'. We have now recorded our changes and we can go back at any time to see when we made changes to our file and decided to add 'hello world'. We do have a problem now though. At the moment our changes are only recorded locally on our computer. Therefore, if we wanted to work with someone else, they would have no way of seeing what we've done. Let's fix that. Let's jump to the GitHub website where we will host some of our work. Hosting on GitHub will allow us to share our work with friends and colleagues but will also allow other people to use or build on our work.
We can see that one file has changed and that we made one insertion, which was a line with the text '#Hello, world!'.
We can
also see the commit message 'Add index.md', which we added by adding the `-m` flag to `git commit`.

We now have a permanent record of what was changed, along with additional details such as when that change was made.

However, we now have a problem. At the moment our changes are only recorded locally, on our computer. If we wanted to
work collaboratively with someone else they would have no way of seeing what we've done. Let's fix that by using GitHub
to share our work.