gitGraph accDescr {A Git graph showing the root-commit on the main branch and a new forecast branch, branching off the root-commit, with one commit.} commit id: 'Initial commit' branch forecast commit id: 'Create a md file with the forecast'
First let’s make sure we’re still on the right branch. You should be on the forecast
branch:
$ git switch forecast
Let’s create a file called forecast.md
that contains a basic weather forecast. We’ll use nano
to edit the file; you can use whatever editor you like. In particular, this does not have to be the core.editor
you set globally earlier. But remember, the steps to create create or edit a new file will depend on the editor you choose (it might not be nano). For a refresher on text editors, check out “Which Editor?” in The Unix Shell lesson.
$ nano forecast.md
Type the text below into the forecast.md
file:
# Forecast
## Today
Cloudy with a chance of pizza.
Save the file and exit your editor. Next, let’s verify that the file was properly created by running the list command (ls
):
$ ls
forecast.md
forecast.md
contains three lines, which we can see by running:
$ cat forecast.md
# Forecast
## Today
Cloudy with a chance of pizza.
If we check the status of our project again, Git tells us that it’s noticed the new file:
$ git status
On branch forecast
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
forecast.md
nothing added to commit but untracked files present (use "git add" to track)
The “untracked files” message means that there’s a file in the directory that Git isn’t keeping track of. We can tell Git to track a file using git add
:
$ git add forecast.md
and then check that the right thing happened:
$ git status
On branch forecast
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: forecast.md
Git now knows that it’s supposed to keep track of forecast.md
, but it hasn’t recorded these changes as a commit yet. To get it to do that, we need to run one more command:
$ git commit -m "Create a md file with the forecast"
[forecast f22b25e] Create a md file with the forecast
1 file changed, 5 insertions(+)
create mode 100644 forecast.md
When we run git commit
, Git takes everything we have told it to save by using git add
and stores a copy permanently inside the special .git
directory. This permanent copy is called a commit (or revision) and its short identifier is f22b25e
. Your commit may have another identifier.
We use the -m
flag (for “message”) to record a short, descriptive, and specific comment that will help us remember later on what we did and why. 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.
Good commit messages start with a brief (<50 characters) statement about the changes made in the commit. Generally, the message should complete the sentence “If applied, this commit will”
The whatthecommit site can be used to show example commit messages, good and bad, pulled from public repos on GitHub. You should note that there is no safe for work filter. Some of the commit messages may include inappropriate language.
Using git add .
Using git add .
or the -a
flag with git commit
will add all your unstaged changes in your repository.
This might include things you didn’t mean to add. Always use git status
to check your changes before adding them. We recommend you avoid using git add .
and git commit -a
.
Running git add
followed by git commit
is equivalent to:
$ fcm commit
Our repository now looks like this:
If we run git status
now:
$ git status
On branch forecast
nothing to commit, working tree clean
it tells us everything is up to date.
Now suppose you want to more information to the file. (Again, we’ll edit with nano
and then cat
the file to show its contents; you may use a different editor, and don’t need to cat
.)
$ nano forecast.md
$ cat forecast.md
# Forecast
## Today
Cloudy with a chance of pizza.
## Tomorrow
Morning rainbows followed by light showers.
When we run git status
now, it tells us that a file it already knows about has been modified:
$ git status
On branch forecast
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: forecast.md
no changes added to commit (use "git add" and/or "git commit -a")
The last line is the key phrase: “no changes added to commit”. We have changed this file, but we haven’t told Git we will want to save those changes (which we do with git add
) nor have we saved them (which we do with git commit
). So let’s do that now. It is good practice to always review our changes before saving them. We do this using git diff
. This shows us the differences between the current state of the file and the most recently saved version:
$ git diff
diff --git a/forecast.md b/forecast.md
index df0654a..315bf3a 100644
--- a/forecast.md
+++ b/forecast.md
@@ -3,3 +3,7 @@
## Today
Cloudy with a chance of pizza.
+
+## Tomorrow
+
+Morning rainbows followed by light showers.
The output is cryptic because it is actually a series of commands for tools like editors and patch
telling them how to reconstruct one file given the other. If we break it down into pieces:
- 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. - The second line tells exactly which versions of the file Git is comparing;
df0654a
and315bf3a
are unique computer-generated labels for those versions. - The third and fourth lines once again show the name of the file being changed.
- The remaining lines are the most interesting, they show us the actual differences and the lines on which they occur. In particular, the
+
marker in the first column shows where we added a line.
After reviewing our change, it’s time to commit it:
$ git commit -m "Add tomorrows forecast to forecast.md"
On branch forecast
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: forecast.md
no changes added to commit (use "git add" and/or "git commit -a")
Whoops: Git won’t commit because we didn’t use git add
first. Let’s fix that:
$ git add forecast.md
$ git commit -m "Add tomorrows forecast to forecast.md"
[forecast 34961b1] Add tomorrows forecast to forecast.md
1 file changed, 4 insertions(+)
Git insists that we add files to the set we want to commit before actually committing anything. This allows us to commit our changes in stages and capture changes in logical portions rather than only large batches. For example, suppose we’re adding a few citations to relevant research to our thesis. We might want to commit those additions, and the corresponding bibliography entries, but not commit some of our work drafting the conclusion (which we haven’t finished yet).
To allow for this, Git has a special staging area where it keeps track of things that have been added to the current changeset but not yet committed.
In FCM there is no concept of a staging area. FCM will commit all file modifications at once. This can lead to rather large commits. In Git remember to break down commits into small logical chunks.
$ git add <file>
$ git commit
is equivalent to:
$ fcm commit
Our repository now looks like this:
gitGraph accDescr {A Git graph showing the root-commit on the main branch and a new forecast branch, branching off the root-commit, with two commits.} commit id: 'Initial commit' branch forecast commit id: 'Create a md file with the forecast' commit id: 'Add tomorrows forecast to forecast.md'
Let’s watch as our changes to a file move from our editor to the staging area and into long-term storage. First, we’ll improve our forecast by changing ‘pizza’ to ‘Sun’:
$ nano forecast.md
$ cat forecast.md
# Forecast
## Today
Cloudy with a chance of Sun.
## Tomorrow
Morning rainbows followed by light showers.
$ git diff
diff --git a/forecast.md b/forecast.md
index 315bf3a..b36abfd 100644
--- a/forecast.md
+++ b/forecast.md
@@ -2,7 +2,7 @@
## Today
-Cloudy with a chance of pizza.
+Cloudy with a chance of Sun.
## Tomorrow
So far, so good: we’ve replaced one line (shown with a -
in the first column) with a new line (shown with a +
in the first column). Now let’s put that change in the staging area and see what git diff
reports:
$ git add forecast.md
$ git diff
There is no output: as far as Git can tell, there’s no difference between what it’s been asked to save permanently and what’s currently in the directory. However, if we do this:
$ git diff --staged
diff --git a/forecast.md b/forecast.md
index 315bf3a..b36abfd 100644
--- a/forecast.md
+++ b/forecast.md
@@ -2,7 +2,7 @@
## Today
-Cloudy with a chance of pizza.
+Cloudy with a chance of Sun.
## Tomorrow
it shows us the difference between the last committed change and what’s in the staging area. Let’s save our changes:
$ git commit -m "Modify the forecast to add a chance of Sun"
[forecast 005937f] Modify the forecast to add a chance of Sun
1 file changed, 1 insertion(+), 1 deletion(-)
check our status:
$ git status
On branch forecast
nothing to commit, working tree clean
Our repository now looks like this:
gitGraph accDescr {A Git graph showing the root-commit on the main branch and a new forecast branch, branching off the root-commit, with three commits.} commit id: 'Initial commit' branch forecast commit id: 'Create a md file with the forecast' commit id: 'Add tomorrows forecast to forecast.md' commit id: 'Modify the forecast to add a chance of Sun'
To recap, when we want to add changes to our repository, we first need to add the changed files to the staging area (git add
) and then commit the staged changes to the repository (git commit
):
Choosing a Commit Message
Which of the following commit messages would be most appropriate for the last commit made to forecast.md
?
- “Changes”
- “Modify the forecast”
- “Modify the forecast to add a chance of Sun”
Solution (Solution). Answer 1 is not descriptive enough, and the purpose of the commit is unclear; and answer 2 is redundant to using “git diff” to see what changed in this commit; but answer 3 is good: short, descriptive, and imperative.
Committing Changes to Git
Which command(s) below would save the changes of myfile.txt
to my local Git repository?
$ git commit -m "my recent changes"
$ git init myfile.txt $ git commit -m "my recent changes"
$ git add myfile.txt $ git commit -m "my recent changes"
$ git commit -m myfile.txt "my recent changes"
Solution (Solution).
- Would only create a commit if files have already been staged.
- Would try to create a new repository.
- Is correct: first add the file to the staging area, then commit.
- Would try to commit a file “my recent changes” with the message myfile.txt.
Committing Multiple Files
The staging area can hold changes from any number of files that you want to commit as a single snapshot.
- Add some text to
forecast.md
noting the expected temperature. - Create a new file
atlas.md
with a list of common weather such as rain, sunshine, fog etc. - Add changes from both files to the staging area, and commit those changes.
Solution (Solution). First we make our changes to the forecast.md
and atlas.md
files:
$ nano forecast.md
$ cat forecast.md
# Forecast
## Today
Cloudy with a chance of sun.
Mild temperatures around 16 °C.
## Tomorrow
Morning rainbows followed by light showers.
$ nano atlas.md
$ cat atlas.md
# Weather Atlas
- rain
- sunshine
- fog
Now you can add both files to the staging area. We can do that in one line:
$ git add forecast.md atlas.md
Or with multiple commands:
$ git add forecast.md
$ git add atlas.md
Now the files are ready to commit. You can check that using git status
. If you are ready to commit use:
$ git commit -m "Add in the temperature to the forecast and create the weather atlas file"
[forecast cc127c2] Add in the temperature to the forecast and create the weather atlas file
2 files changed, 6 insertions(+)
create mode 100644 atlas.md
bio
Repository
- Create a new Git repository on your computer called
bio
. - Write a three-line biography for yourself in a file called
me.txt
, commit your changes. - Modify one line, add a fourth line
- Display the differences. between its updated state and its original state.
Solution (Solution). If needed, move out of the weather
folder:
$ cd ..
Create a new folder called bio
and ‘move’ into it:
$ mkdir bio
$ cd bio
Initialise the repository:
$ git init
Create your biography file me.txt
using nano
or another text editor. Once in place, add and commit it to the repository:
$ git add me.txt
$ git commit -m "Add biography file"
Modify the file as described (modify one line, add a fourth line). To display the differences between its updated state and its original state, use git diff
:
$ git diff me.txt
Keypoints
git status
shows the status of a repository.- Files can be stored in a project’s working directory (which users see), the staging area (where the next commit is being built up) and the local repository (where commits are permanently recorded).
git add
puts files in the staging area.git commit
saves the staged content as a new commit in the local repository.- Write a commit message that accurately describes your changes.