Writing and Committing Code in GitHub: Creating a Commit

This section assumes you have code that you’ve changed on your local computer and that the code is in a working state. If you need an example of working code, see the previous section in this chapter to get to this state.

After you have running code, you can commit it to the repository. To create a com­mit is a two-step process:

  1. Stage the changes you want to commit.
  2. Create the commit with a commit message.

1. Staging changes

Staging changes can be confusing to the Git beginner. In concept, it’s similar to a staging environment for a website. Staging changes is an intermediate place where you can see the changes you’re about to commit before you commit them.

Why would you want to stage changes before committing them? In Git, a commit should contain a group of related changes. In fact, Git encourages this setup.

Suppose that you’ve been working for a few hours and now have a large set of unrelated changes that aren’t committed to the Git repository.

You may be tempted to just commit everything with some generic commit message like “A bunch of changes.” In fact, an XKCD commit makes light of this phenomena at https://xkcd.com/1296.

Committing a bunch of unrelated changes is generally a bad idea. The commit his­tory of a repository tells the story of how a project changes over time. Each com­mit should represent a distinct cohesive set of changes. This approach to commits isn’t just about being fastidious and organized. Having a clean Git history has concrete benefits.

TIP: One benefit of a clean Git history is that a command like git bisect is way more useful when each commit is a logical unit of work. The git bisect command is an advanced command, and full coverage of what it does is beyond the scope of this book. In short, though, git bisect provides a way to conduct a binary search through your Git history in order to find the specific commit that introduces a particular behavior, such as a bug. If every commit contains a large group of unre­lated changes, finding the specific commit that introduces a bug isn’t as useful as it would be if every commit contains a single logical unit of change.

In the example for this chapter, we can probably stand to create two commits:

» One that just contains the README.md file.

» Another that contains the index.html and script.js files.

Because the index.html file references the script.js file, checking in one with­out the other doesn’t make sense at this point.

Start by staging the README.md file:

$ git add README.md

The README.md file is added to the Git index. The Git index is the staging area for creating commits to the repository. You can check the status of the repository to see that the file has been added to the index:

$ git status On branch master

No commits yet

Changes to be committed:

(use “git rm –cached <file>…” to unstage)

new file: README.md

Untracked files:

(use “git add <file>…” to include in what will be committed)

index.html

js/

As you can see, the README.md file is staged for commit. Meanwhile, the index. html and js/ directory aren’t yet tracked by this repository.

Why isn’t script.js listed in the untracked files section? Git is taking a shortcut here. It notices that no files within the js directory are tracked, so it can simply list the directory rather than list every file in the directory. In a larger code base, you’ll be glad Git isn’t listing every file in every subdirectory.

2. Committing a file

After you stage changes (see preceding section), you can create a commit. In this example, we use the -m flag with the git commit command to specify a short commit message. The following commands demonstrate how to create a commit and specify the commit message in one step:

$ git commit -m “Add a descriptive README file”

[master (root-commit) 8436866] Add a descriptive README file

1 file changed, 3 insertions(+), 0 deletions(-)

create mode 100644 README.md

The file is committed. If you run the git status command again, you see that you still have untracked files. The git commit command commits only the changes that are staged.

3. Committing multiple file:

After you commit the first file, you’re ready to stage the rest of the files for a commit.

$ git add -A $ git status

On branch master Changes to be committed:

(use “git reset HEAD <file>…” to unstage)

new file: index.html

new file: js/scripts.js

The -A flag indicates that you want to add all changes in the working directory to the Git index. When you run the git status command, you can see that you’ve staged two files.

TIP: When the js directory is untracked, git status lists only the js directory and none of the files in the directory. Now that you’re trying to stage the js directory, Git lists the file in the js directory. Why the discrepancy? Git doesn’t actually track directories. It tracks only files. Therefore, when you add a directory to a Git repository, it needs to add each file to the index.

Sometimes you need to write a more detailed commit message. In this example, we don’t specify a commit message when we run the commit command because we plan to write a more detailed commit message:

$ git commit

If you don’t specify a commit message using the -m flag, Git launches an editor to create a commit message. If you haven’t configured an editor with Git, it uses the system default editor, typically VI or VIM.

There are legions of jokes about how difficult it is to exit VIM, so we won’t rehash them all here. We’ll simply take a moment of silence in remembrance for our friends still stuck in the VIM editor.

For the record, to exit VIM, press the ESC key to exit the edit mode and type :wq to exit and save or :q! to exit without saving.

To change the default editor to something like Atom, run the following command in the terminal:

gitconfig —global core.editor”atom —wait”

The editor opens a temporary file named COMMIT_EDITMSG, which contains some instructions that are commented out:

# Please enter the commit message for your changes. Lines starting

# with ‘#’ will be ignored, and an empty message aborts the commit.

#

# On branch master

#

# Initial commit

#

# Changes to be committed:

# new file: README.md

# Untracked files:

# index.html

# js/

#

You will enter your commit message in the file that gets opened. You can write your message before all the comments or simply replace everything in the file with your own commit message.

In this case, we replace everything in that file with

Add index.html and script.js

This adds index.html to the project. This file is the default page when visiting the website.

This file references js/script.js, which is also added in this commit.

After you save the commit message and close the file or editor, Git creates a com­mit with the message you wrote.

Source: Guthals Sarah, Haack Phil (2019), GitHub for Dummies, Wiley.

Leave a Reply

Your email address will not be published. Required fields are marked *