Unit 7: Git - Changes to Code

Table of Contents

  1. Changes to Code
  2. Git Commands
    1. git add
    2. git commit

Changes to Code

When you make changes to the code in your repository, Git tracks that. However, it will not automatically version your files - it needs you to tell it when is a good time to preserve the current state of the code.

The general workflow when files are modified goes as follows:

  1. File is modified
  2. File is added to the staging area (see git add)
  3. File in the staging area is committed (see git commit)

The staging area is basically a place to put code right before you commit your changes. Think of a commit has a snapshot in time. You will later learn how to use these commits to “go back in time” and look at how code used to be, or even undo commits.

Git Commands

git add

Use git add to add file(s) to the staging area. You must add a file to the staging area before committing it.

$ git add <path>

Note that you can add multiple files at the same time.

$ git add hello.txt world.txt

You can also use globs to add files that match a certain pattern. You can also add a directory, which will add all unstaged modified files in that directory.

To add all files in a directory, use:

$ git add .

git commit

Use git commit to commit files added to the staging area. You should always have a commit message attached to a commit. Be sure to follow good commit message conventions.

$ git commit -m <message>

For example:

# Start in the root directory of the repo
$ touch hello.txt
$ git add hello.txt
$ git commit -m "Initial commit"
[master (root-commit) 9159807] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

Amend a commit

If you accidentally commit prematurely or want to edit the commit message BEFORE pushing to remote, you can use the following command:

$ git commit --amend

Make sure that BEFORE you execute the command above, you have added any files you want to the staging area so that they are included in the amended commit.

Also note that once the command executes, your default text editor (such as vim, vi, nano, or emacs) will open so that you can edit the commit message.

Multi-line commit messages

There will come a time when you don’t have enough space to write everything you want to write in the header of the commit message. Thus, you’ll need a multi-line commit message.

You should be able to just press Enter after writing the commit message header in order to move to new lines. However, in the event that this does not work for you, you can use the -m flag multiple times. The separate messages will be treated as separate paragraphs.

$ git commit -m <paragraph1> -m <paragraph2> ...

For example:

$ git commit -m "First line" -m "Second line"

Adding & Committing

You can skip using git add with the -a flag. This will stage all modified files that were previously tracked (files that aren’t new) and then commits them. (Thus, if you want to commit untracked files, you will need to use git add.)

$ git commit -am <message>

For example:

$ git commit -am "I am adding and committing at the same time"