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

Cleanup and general fixes, expanded GitHub Pages episode #56

Merged
merged 10 commits into from Sep 14, 2017
@@ -29,7 +29,7 @@ the [GitHub page](https://github.com/data-lessons/library-git).
### Creating a repository

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 file called `.git`. We can create a new git
stored within the same directory as these project files, in a hidden directory called `.git`. We can create a new git
repository either by using GitHub's web 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.

@@ -53,7 +53,7 @@ $ git init
The `hello-world` directory is now a git repository.

If we 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 hidden file `.git`.
in this case includes the new hidden directory `.git`.

Note that whenever we use git via the command line, we need to preface each command (or verb) with `git`, so that the computer knows
we are trying to get git to do something, rather than some other program.
@@ -90,8 +90,7 @@ $ touch index.md
~~~
{: .bash}

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

@@ -142,41 +141,43 @@ Changes to be committed:
{: .output}

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.
is a new file to be committed but, before we do that, let's add some text to the file.


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
We will open the file `index.md` with any text editor we have at hand (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, let's save the file within the text editor and check if Git
has spotted the changes.

~~~
$ git status
~~~
{: .bash}
~~~
On branch master
Initial commit
Open the file in nano or another text editor.
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
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
new file: index.md
~~~
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: index.md
modified: index.md
~~~
{: .output}

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.
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 new version of the file to the staging area.

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

We can now **commit** our first changes.
Now we are ready to **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.
@@ -196,9 +197,33 @@ $ git commit -m 'Add index.md'
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.
The commit message is used to record a short, descriptive, and specific summary of what we did to help us remember later on without having to look at the actual changes.
If we just run `git commit` without the `-m` option, Git will launch nano (or whatever other editor we configured as `core.editor`)
so that we can write a longer message.

Having made a commit, we now have a permanent record of what was changed,
along with metadata about who made the commit and at what time.

> ## Staging Area
>
> If you think of Git as taking snapshots of changes over the life of a project,
> `git add` specifies *what* will go in a snapshot
> (putting things in the staging area),
> and `git commit` then *actually takes* the snapshot, and
> makes a permanent record of it (as a commit).
> If you don't have anything staged when you type `git commit`,
> Git will prompt you to use `git commit -a` or `git commit --all`,
> which is kind of like gathering *everyone* for the picture!
> However, it's almost always better to
> explicitly add things to the staging area, because you might
> commit changes you forgot you made. (Going back to snapshots,
> you might get the extra with incomplete makeup walking on
> the stage for the snapshot because you used `-a`!)
> Try to stage things manually,
> or you might find yourself searching for "git undo commit" more
> than you would like!
{: .callout}
But 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.
We will fix that in the next episode by using GitHub to share our work.
@@ -15,7 +15,7 @@ keypoints:
- "`pull` is a Git verb for bringing changes from a remote repository to the local repository"
---

## Create a repository
## Create a repository on GitHub

When we have logged in to GitHub, we can create a new repository by clicking the **+** icon in the upper-right corner of
any page then selecting **New repository**. Let's do this now.
@@ -27,18 +27,52 @@ GitHub will ask if you want to add a README.md, license or a `.gitignore` file.

> ## Choosing a license
> Choosing a license is an important part of openly sharing your creative work online. For help in wading through the
> many types of open source licenses, please visit https://choosealicense.com/.
> many types of open source licenses, please visit <https://choosealicense.com/>.
{: .callout}
## Connecting your local repository to the GitHub repository

In the previous episode we created a local repository on our own computer.
Now we have also created a remote repository on GitHub.
But at this point, the two are completely isolated from each other.
We want to link them together to synchronize them and share our project with the world.

To do this, we need the GitHub repository URL, which should look something like this
(with "some-librarian" replaced with your username):

![The repository URL on GitHub](../fig/repository-url.png)

If the URL starts with `git@` rather than `https://`, please click the "HTTPS" button to change it.

> ## HTTPS vs. SSH
>
> We use HTTPS here because it does not require additional configuration, which vary
> from operating system to operating system. If you start using Git regularly, you would
> like to set up SSH access, which is a bit more secure and convenient, by
> following one of the great tutorials from
> [GitHub](https://help.github.com/articles/generating-ssh-keys),
> [Atlassian/BitBucket](https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git)
> and [GitLab](https://about.gitlab.com/2014/03/04/add-ssh-key-screencast/)
> (this one has a screencast).
{: .callout}
## Connecting your local repository to a remote repository
Notice that GitHub is actually helpful enough to provide instructions for us
so we don't have to remember these commands:

We now need to link the local repository that we created in the previous lesson with the remote repository that we have
just created on GitHub.
![GitHub instructions](../fig/github-instructions.png)

You can therefore choose to copy these and paste them on the command line.
Or you can choose to type them out to get them into your fingers.
I will do that. So we start with the command to link our local repository
to the GitHub repository:

~~~
$ git remote add origin <web_address_of_your_repo.git>
$ git remote add origin https://github.com/some-librarian/hello-world.git
~~~
{: .bash}

where `some-librarian` should be replaced with your own username.

> ## Why `origin`?
> `origin` in the `git remote add` line is just a short name or alias we're giving to that big long repository URL.
> It could be almost any string we want, but by convention in git, it is usually called `origin`, representing where
@@ -57,10 +91,12 @@ origin https://github.com/<your_github_username>/hello-world (push)
~~~
{: .output}

## "Pushing" changes
## Pushing changes

To add the changes we previously made to our local repository to the remote repository that we have just created on
GitHub we need to run the `git push` command.
Now we have established a connection between the two repositories, but we still haven't
synchronized their content, so the remote repository is still empty. To fix that, we
will have to "push" our local changes to the GitHub repository. We do this using the
`git push` command:

~~~
$ git push -u origin master
@@ -80,8 +116,9 @@ nothing to commit, working tree clean
~~~
{: .output}

The nickname of our remote is origin and the default local branch name is master. The `-u` flag tells git to remember
the parameters, so that next time we can simply run `git push` and Git will know what to do. Go ahead and push it!
The nickname of our remote repository is "origin" and the default local branch name is "master".
The `-u` flag tells git to remember the parameters, so that next time we can simply run `git push`
and Git will know what to do.

You may be prompted to enter your GitHub username and password to complete the command.

@@ -104,7 +141,8 @@ This output lets us know where we are working (the master branch). We can also s
and everything is in order.

We can use the `git diff` command to see changes we have made before making a commit. Open index.md with any text
editor and enter a new line.
editor and enter some text on a new line, for instance "A new line" or something else.
We will then use `git diff` to see the changes we made:

~~~
$ git diff
@@ -123,17 +161,18 @@ index aed0629..989787e 100644
~~~
{: .output}

We can see the changes we have made.
The command produces lots of informatinon and it can be a bit overwhelming at first,
but let's go through some key information here:

1. The first line tells us that Git is producing output similar to the Unix diff command comparing the old and new
1. The first line tells us that Git is producing output similar to the Unix `diff` command, comparing the old and new
versions of the file.
2. The second line tells exactly which versions of the file Git is comparing; `aed0629` and `989787e` are unique
computer-generated identifiers for those versions.
3. The third and fourth lines once again show the name of the file being changed.
4. The remaining lines are the most interesting; they show us the actual differences and the lines on which they occur.
In particular, the + markers in the first column show where we have added lines.

We can now commit these changes again:
We can now commit these changes:

~~~
$ git add index.md
@@ -169,19 +208,25 @@ commit messages when we make changes. This is especially important when we are w
be able to guess as easily what our short cryptic messages might mean. Note that it is best practice to always write
commit messages in the imperative (e.g. 'Add index.md', rather than 'Adding index.md').

We might get a lit bit lonely working away on our own and want to work with other people. Before we get to that, it is
worth learning a couple more git commands.
## Pushing changes (again)

## Pushing changes
Now, let's have a look at the repository at GitHub again
(that is, `https://github.com/some-librarian/hello-world` with `some-librarian` replaced with your username).
We see that the `index.md` file is there, but there is only one commit:

If we have another look at our repository on GitHub, we can see that only our first change is there. This is because we
haven't yet pushed our local changes to the remote repository. This might seem like a mistake in design but it is
![Only one commit on GitHub](../fig/github-one-commit.png)

And if you click on `index.md` you will see that it contains the "Hello, world!" header,
but not the new line we just added.

This is because we haven't yet pushed our local changes to the remote repository.
This might seem like a mistake in design but it is
often useful to make a lot of commits for small changes so you are able to make careful revisions later and you don't
necessarily want to push all these changes one by one.

Let's push our changes now, using the `git push` command.
Another benefit of this design is that you can make commits without being connected to internet.

## Pushing and Pulling
But let's push our changes now, using the `git push` command:

~~~
$ git push
@@ -196,6 +241,7 @@ To https://github.com/<your_github_username>/hello-world
~~~
{: .output}

And let's check on GitHub that we now have 2 commits there.

## Pulling changes

@@ -211,6 +257,7 @@ file' (The default commit message will be 'Create README.md', which is fine for
> It is good practice to add a README file to each project to give a brief overview of what the project is about. If you
> put your README file in your repository's root directory, GitHub will recognize and automatically surface your README
> to repository visitors
{: .callout}
Our local repository is now out of sync with our remote repository, so let's fix that by pulling the remote changes into
our local repository using the `git pull` command.