Overview
Teaching: 20 min
Exercises: 0 minQuestions
How do I record changes in Git?
How do I check the status of my version control repository?
How do I record notes about what changes I made and why?
Objectives
Go through the modify-add-commit cycle for one or more files.
Explain where information is stored at each stage of Git commit workflow.
Let’s edit our file index.md. We’ll use nano to edit the file;
you can use whatever editor you like.
$ nano index.md
Type the text below into the index.md file:
Two types of data will be generated
index.md now contains a line under the first header. We can see the contents of the whole document by running:
$ cat mars.txt
# Data description
Two types of data will be generated
# Roles and responsibilities
# Data standards and metadata
# Storage and security
# Access and data sharing
# Archiving and preservation
Repository with source code [here](https://github.com/clarallebot/GRAD521_DMPtemplate)
If we check the status of our project again, Git tells us that it’s noticed the change:
$ 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: index.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.
$ git add index.md
And check the status
$ 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: index.md
Git now knows what to save, the changes we just submitted in index.md are recorded in a staging area.
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 "Start descriptions of the dataset"
[master f3f9619] Start descriptions of the dataset
1 file changed, 1 insertion(+)
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 f3f9619
(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 a text editor so that we can write a longer message.
Good commit messages start with a brief (<50 characters) summary of changes made in the commit. If you want to go into more detail, add a blank line between the summary line and your additional notes.
Let’s make another change to our file. At the bottom of the file we have a link to the repository. But right now the link goes to Clara’s repository. Change it so that it goes to your repository.
$ nano index.md
# Data description
Two types of data will be generated
# Roles and responsibilities
# Data standards and metadata
# Storage and security
# Access and data sharing
# Archiving and preservation
Repository with source code [here](https://github.com/username/GRAD521_DMPSurname)
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/index.md b/index.md
index 322d170..c99e98b 100644
--- a/index.md
+++ b/index.md
@@ -11,4 +11,4 @@ Two types of data will be generated
# Archiving and preservation
-Repository with source code [here](https://github.com/clarallebot/GRAD521_DMPtemplate)
+Repository with source code [here](https://github.com/username/GRAD521_DMPSurname)
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:
diff command
comparing the old and new versions of the file.322d170 and c99e98b are unique computer-generated labels for those versions.+ marker in the first column shows where we added a line. The - marker shows where we deleted a line.After reviewing our change, it’s time to commit it:
$ git commit -m "Change link to Repository"
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
modified: index.md
no changes added to commit
Whoops:
Git won’t commit because we didn’t use git add first.
Let’s fix that:
$ git add index.md
$ git commit -m "Change link to Repository"
[master d407efb] Change link to Repository
1 file changed, 1 insertion(+), 1 deletion(-)
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 our supervisor’s work to our thesis. We might want to commit those additions, and the corresponding addition to the bibliography, but not commit the work we’re doing on 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 change set but not yet committed.
Staging Area
If you think of Git as taking snapshots of changes over the life of a project,
git addspecifies what will go in a snapshot (putting things in the staging area), andgit committhen actually takes the snapshot, and makes a permanent record of it (as a commit). If you don’t have anything staged when you typegit commit, Git will prompt you to usegit commit -aorgit 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!
Let’s look at the history of what we’ve done so far:
$ git log
commit d407efb81ac0100ad919f92021f6134ca161baf9
Author: Clara Llebot <clara.llebot@oregonstate.edu>
Date: Wed Jan 30 17:18:43 2019 -0800
Change link to Repository
commit f3f9619847e3421700dc073c1769591f7f9b7cec
Author: Clara Llebot <clara.llebot@oregonstate.edu>
Date: Wed Jan 30 17:09:58 2019 -0800
Start descriptions of the dataset
git log lists all commits made to a repository in reverse chronological order. The listing for each commit includes the commit’s full identifier (which starts with the same characters as the short identifier printed by the git commit command earlier), the commit’s author, when it was created, and the log message Git was given when the commit was created.
The list shows actually more commits than just the two we just made. It shows all the changes that I made when I was creating the repository. When we imported the repository we imported not just the files, but also the history of the files.
Now check our status:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
The working tree is clean and we have nothing to commit, but we see a message saying that the branch is ahead of origin/master. origin is a local nickname for your remote repository that is set by default by Git. Git knows what the remote repository is because we set that up when we cloned the repository. If you had started the repository locally in your computer first, you would have to set up the origin repository. What the sentence means is that git knows that what we have made changes on our local repository that are not in GitHub. Our local branch is ahead of the remote branch. Let’s see how to fix that.
Paging the Log
When the output of
git logis too long to fit in your screen,gituses a program to split it into pages of the size of your screen. When this “pager” is called, you will notice that the last line in your screen is a:, instead of your usual prompt.
- To get out of the pager, press
q.- To move to the next page, press the space bar.
- To search for
some_wordin all pages, type/some_wordand navigate throught matches pressingn.
Limit Log Size
To avoid that
git logcover all your terminal screen you can limit the numbers of commit that Git will list by using-NwhereNis the number of commits that you want to receive the information. For example, if you only want the information from the last commit you can use$ git log -1commit 005937fbe2a98fb83f0ade869025dc2636b4dad5 Author: Vlad Dracula <vlad@tran.sylvan.ia> Date: Thu Aug 22 10:14:07 2013 -0400 Discuss concerns about Mars' climate for MummyYou can also reduce the quantity of information using the
--onelineoption:$ git log --oneline* 005937f Thoughts about the climate * 34961b1 Concerns about Mars's moons on my furry friend * f22b25e Starting to think about MarsYou can also combine the
--onelineoptions with others. One useful combination is$ git log --oneline --graph --all --decorate* 005937f Thoughts about the climate (HEAD, master) * 34961b1 Concerns about Mars's moons on my furry friend * f22b25e Starting to think about Mars
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
mars.txt?
- “Changes”
- “Added line ‘But the Mummy will appreciate the lack of humidity’ to mars.txt”
- “Discuss effects of Mars’ climate on the Mummy”
Solution
Answer 1 is not descriptive enough, and answer 2 is too descriptive and redundant, but answer 3 is good: short but descriptive.
Committing Changes to Git
Which command(s) below would save the changes of
myfile.txtto 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
- 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
mars.txtnoting your decision to consider Venus as a base- Create a new file
venus.txtwith your initial thoughts about Venus as a base for you and your friends- Add changes from both files to the staging area, and commit those changes.
Solution
First we make our changes to the
mars.txtandvenus.txtfiles:$ nano mars.txt $ cat mars.txtMaybe I should start with a base on Venus.$ nano venus.txt $ cat venus.txtVenus is a nice planet and I definitely should consider it as a base.Now you can add both files to the staging area. We can do that in one line:
$ git add mars.txt venus.txtOr with multiple commands:
$ git add mars.txt $ git add venus.txtNow the files are ready to commit. You can check that using
git status. If you are ready to commit use:$ git commit -m "Wrote down my plans to start a base on Venus"[master cc127c2] Wrote down my plans to start a base on venus 2 files changed, 2 insertions(+)~~~
Author and Committer
For each of the commits you have done, Git stored your name twice. You are named as the author and as the committer. You can observe that by telling Git to show you more information about your last commits:
$ git log --format=fullWhen commiting you can name someone else as the author:
$ git commit --author="Vlad Dracula <vlad@tran.sylvan.ia>"Create a new repository and create two commits: one without the
--authoroption and one by naming a colleague of yours as the author. Rungit logandgit log --format=full. Think about ways how that can allow you to collaborate with your colleagues.Solution
$ git add me.txt $ git commit -m "Updated Vlad's bio." --author="Frank N. Stein <franky@monster.com>"[master 4162a51] Updated Vlad's bio. Author: Frank N. Stein <franky@monster.com> 1 file changed, 2 insertions(+), 2 deletions(-) $ git log --format=full commit 4162a51b273ba799a9d395dd70c45d96dba4e2ff Author: Frank N. Stein <franky@monster.com> Commit: Vlad Dracula <vlad@tran.sylvan.ia> Updated Vlad's bio. commit aaa3271e5e26f75f11892718e83a3e2743fab8ea Author: Vlad Dracula <vlad@tran.sylvan.ia> Commit: Vlad Dracula <vlad@tran.sylvan.ia> Vlad's initial bio.
Key Points
git statusshows 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 addputs files in the staging area.
git commitsaves the staged content as a new commit in the local repository.Always write a log message when committing changes.