Add Local Commits With Git

After you have finished making your changes, it is time to commit them.

gif of following the directions below

  1. Determine your file’s status. Remember that git status allows us to see the status of the files on our branch at any given time. Your file is listed under the heading Untracked files.

     git status
    
  2. Add your file to the staging area so it’s prepared to become part of the next commit.

     git add <FILE-NAME>
    
  3. See your file’s current status. Your file is now listed under the heading Changes to be committed. This tells us that the file is in the staging area. It also indicates this is a new file.

     git status
    
  4. Commit your file. A commit tells Git to collect all of the files in the staging area and store them to version control as a single unit of work. Git will open your default text editor where you can enter the commit message.

     git commit
    
  5. Type the commit message, save and quit your editor.

    • The default text editor associated with Git is vi in most cases, which requires that you press the Esc key then type :wq to save and quit after entering your commit message.
    • Alternatively, you can bypass vi altogether and enter your commit message inline with git commit -m "your message"
  6. See the history of commits:

     git log
    
Tell me why

Understanding the GitHub Flow

Add and commit files on the command line.

  1. :white_check_mark: Create a branch
  2. Add commits :arrow_left: (This section deals with this step.)
  3. Open a Pull Request
  4. Collaborate, and make more commits
  5. Merge the Pull Request

The Two Stage Commit

After you have finished making your changes, it is time to commit them. When working from the command line, you will need to be familiar with the idea of the two stage commit.

two stage commit: three empty boxes representing the working directory, staging directory, and committed history directory

When you work locally, your files exist in one of four states. They are either untracked, modified, staged, or committed.

two stage commit: three boxes representing the working directory, staging directory, and committed history directory with new and modified work shown in working directory

An untracked file is one that is not currently part of the version controlled directory. These are typically new files.

Modified files are ones that Git already knows about, they have at least one other version in history.

To add these files to version control, you will create a collection of files that represent a discrete unit of work. We build this unit in the staging area.

two stage commit: three boxes representing the working directory, staging directory, and committed history directory with work shown in the staging directory

When we are satisfied with the unit of work we have assembled, we will commit everything in the staging area.

two stage commit: three boxes representing the working directory, staging directory, and committed history directory with work shown in the committed history directory

In order to make a file part of the version controlled directory we will first do a git add and then we will do a git commit.

two stage commit: showing command git add between working directory and staging directory, and git commit between staging directory and committed history directory

Stuck? Open an issue in the repository for this class and mention @githubteacher for help from one of the GitHub trainers!
Continue