Tracking Changes

Going Through the Modify-Add-Commit Cycle

First let’s make sure we’re still in the right directory. You should be in the practicegit directory under your home directory.

cd ~/practicegit/

Let’s create a file called git.txt that contains some information about git as a base. 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 bash command to 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 software carpentry Unix Shell lesson.

nano git.txt

Type the text below into the git.txt file:

Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel

Let's first verify that the file was properly created by running the list command (ls)

ls

Output

git.txt

git.txt contains a single line, which we can see by running:

cat git.txt

Output

Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel

If we check the status of our project again, Git tells us that it’s noticed the new file:

git status

Output

# On branch main
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       git.txt
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 git.txt

and then check that the right thing happened:

git status

Output

# On branch main
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   git.txt
#

Git now knows that it’s supposed to keep track of git.txt, 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 "The original author and date of git creation"

Output

[main (root-commit) 7c980e5] The original author and date of git creation
 1 file changed, 1 insertion(+)
 create mode 100644 git.txt

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 7c980e5. 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” . If you want to go into more detail, add a blank line between the summary line and your additional notes. Use this additional space to explain why you made changes and/or what their impact will be.

If we run git status now:

git status

Output

# On branch main
nothing to commit, working directory clean

it tells us everything is up to date. If we want to know what we’ve done recently, we can ask Git to show us the project’s history using git log:

git log

Output

commit 7c980e53253b87c7183bf678631e4eee33262957
Author: Duan Ma <duan@mit.edu>
Date:   Tue Feb 14 10:35:49 2023 -0500

    The original author and date of git creation

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.

  • If we run ls at this point, we will still see just one file called git.txt. That’s because Git saves information about files’ history in the special .git directory mentioned earlier so that our filesystem doesn’t become cluttered (and so that we can’t accidentally edit or delete an old version).

Now we want to add 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 git.txt
cat git.txt

Output

Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."

When we run git status now, it tells us that a file it already knows about has been modified:

git status

Output

# On branch main
# 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.txt
#
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

Output

diff --git a/git.txt b/git.txt
index 6005f14..907c125 100644
--- a/git.txt
+++ b/git.txt
@@ -1 +1,2 @@
 Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
+Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."

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:

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; 6005f14 and 907c125 are unique computer-generated labels 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 + marker in the first column shows where we added a line.

After reviewing our change, it’s time to commit it:

git commit -m "how git was named--citation from Torvalds"

Output

# On branch main
# 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.txt
#
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 git.txt
git commit -m "how git was named--citation from Torvalds"

Output

[main af50a10] how git was named--citation from Torvalds
 1 file changed, 1 insertion(+)

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. We will talk about Staging Area in the next section.

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 to take a group photo! 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 the group photo simile, you might get an extra with incomplete makeup walking on the stage for the picture 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 watch as our changes to a file move from our editor to the staging area and into long-term storage. First, we’ll add another line to the file:

nano git.txt
cat git.txt

Output

Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."
"git" can mean anything, depending on your mood.
git diff

Output

diff --git a/git.txt b/git.txt
index 907c125..38241f3 100644
--- a/git.txt
+++ b/git.txt
@@ -1,2 +1,3 @@
 Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
 Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."
+"git" can mean anything, depending on your mood.

So far, so good: we’ve added one line to the end of the file (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 git.txt
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

Output

diff --git a/git.txt b/git.txt
index 907c125..38241f3 100644
--- a/git.txt
+++ b/git.txt
@@ -1,2 +1,3 @@
 Git was originally authored by Linus Torvalds in 2005 for development of the Linux kernel
 Torvalds said "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."
+"git" can mean anything, depending on your mood.

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 "the meaning of git"

Output

main 1d7cc11] the meaning of git
 1 file changed, 1 insertion(+)

check our status:

git status

Output

# On branch main
nothing to commit, working directory clean

and look at the history of what we’ve done so far:

git log

Output

commit 1d7cc11f2ffd09ef89a412d074658facb9f337a2
Author: Duan Ma <duan@mit.edu>
Date:   Tue Feb 14 12:34:05 2023 -0500

    the meaning of git

commit af50a10b13c1c2ac15002719da41926ce038f9f3
Author: Duan Ma <duan@mit.edu>
Date:   Tue Feb 14 11:25:46 2023 -0500

    how git was named--citation from Torvalds

commit 7c980e53253b87c7183bf678631e4eee33262957
Author: Duan Ma <duan@mit.edu>
Date:   Tue Feb 14 10:35:49 2023 -0500

    The original author and date of git creation

Tricks

  • Word-based diffing

    • Sometimes, e.g. in the case of the text documents a line-wise diff is too coarse. That is where the --color-words option of git diff comes in very useful as it highlights the changed words using colors.

  • Paging the log

    • When the output of git log is too long to fit in your screen, git uses 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 Spacebar.

      • To search for some_word in all pages, press / and type some_word.

      • Navigate through matches pressing N.

  • Limit log size

    • To avoid having git log cover your entire terminal screen, you can limit the number of commits that Git lists by using -N, where N is the number of commits that you want to view. For example, if you only want information from the last commit you can use:

      • git log -1

      • Output

        • commit 1d7cc11f2ffd09ef89a412d074658facb9f337a2
          Author: Duan Ma <duan@mit.edu>
          Date:   Tue Feb 14 12:34:05 2023 -0500
          
              the meaning of git
      • You can also reduce the quantity of information using the --oneline option:

        • git log --oneline

        • Output

          • 1d7cc11 the meaning of git
            af50a10 how git was named--citation from Torvalds
            7c980e5 The original author and date of git creation

        • You can also combine the --oneline option with others. One useful combination adds --graph to display the commit history as a text-based graph and to indicate which commits are associated with the current HEAD, the current branch main, or other Git references:

        • git log --oneline --graph

        • Output

          • * 1d7cc11 the meaning of git
            * af50a10 how git was named--citation from Torvalds
            * 7c980e5 The original author and date of git creation
    • Directories

      • Two important facts you should know about directories in Git.

        • Git does not track directories on their own, only files within them. Try it for yourself:

        • If you create a directory in your Git repository and populate it with files, you can add all files in the directory at once by:

          • git add <directory-of-files>

          • Make a folder named meaning_of_git and populate it with files Cambridge_English_Dictionary, man_page, and readme_file

          • git status
            git add meaning_of_git
            git status
            # On branch main
            # Changes to be committed:
            #   (use "git reset HEAD <file>..." to unstage)
            #
            #       new file:   meaning_of_git/Cambridge_English_Dictionary
            #       new file:   meaning_of_git/man_page
            #       new file:   meaning_of_git/readme_file

          • Before moving on, we will commit these changes.

            • git commit -m "The meaning of git from different resources"
              [main c2cf0a7] The meaning of git from different resources
              3 files changed, 9 insertions(+)
              create mode 100644 meaning_of_git/Cambridge_English_Dictionary
              create mode 100644 meaning_of_git/man_page
              create mode 100644 meaning_of_git/readme_file

            • 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):

Last updated

Massachusetts Institute of Technology