Git Concepts

Git File States

The state of a file in a git project is one of four possibilities:

  1. committed: safely stored in the local database
  2. modified: file changed, but local database not yet updated
  3. staged: file is marked to go into the next commit.
  4. not tracked: file is in the directory, but changes are not tracked.

How do files become "staged"? A committed file that is changed, but not staged is "modified". The git commit command does not automatically include "modified" in the commit version.

A file is staged with the 'git add' command.

Staged file(s) are committed along with already committed files into a new version with the 'git commit' command.

Editing a staged file creates a modified and unstaged version along with the previously staged (before this edit) version. If the modified version is not staged before the next commit, the earlier version (before this edit) is the one that will be committed.

So generally, don't use 'git add' until you are ready to commit.

Basic Git Commands

Syntax:

      git <command>
    
Commands
  1. git clone ...

    Initialize a git project by copying an existing git repository.

  2. git init

    Initialize a new git project 'from scratch'. This is done when there is no git repository to clone, but you have a directory with files that you want to manage. Change to the directory with your project files. The command creates a git subdirectory (.git), but no files are tracked yet.

  3. git add <file or directory list>

    Stages the files. For a directory, all directory items are staged (?).

  4. git diff [file]

    Compares staged file with the already committed version. If file is not modified or staged, diff produces no output. With no argument, diff compares every staged file with the corresponding committed version.

  5. git commit [-a] (-m <message> | -v)

    Creates a new version. The new version contains all the unmodified files in the previous version plus the currently staged versions. Modified, but unstaged file do not go into this commit. The working directory will still contain the modified, unstaged version. So it will not be the same as the version in this commit!

    The -a option first adds all modified files - that is, stages all modified files. Then commits.

    The -m option adds the message argument as the commit message.

    The -v option opens the editor with the diff output commented (as a reminder of the changes for this commit). Adding a message and then exiting the editor saves only this mesage (commented part is omitted) as the commit message.

    (Untracked files never go into a commit.)

  6. git status

    Lists (a) staged files, (b) modified, but unstaged, (c) untracked files (if not ignored; i.e., not matched by an entry in .gitignore file)

  7. git rm [-cached] <file>

    Removes file from the staging area. On the next commit, the file is also removed from the working directory.

    With the -cached option, the file is removed from the staging area but is no longer tracked. This means it remains in the working directory as an untracked file even after the next commit. (Useful if a generated file such as an executable or object file is accidentally added to a commit.)

Viewing Previous Commmits

The log command shows the commit history. The commit information for the commits are shown in reverse order by default.

      git log
    

Some useful options for limiting the output to specific information:

  1. git log --stat

    Gives the counts of additions and deletions, but doesn't give the full diff output.

  2. git log --oneline

    Give brief one line output for each commit: 40 hex digit sha-1 value and the commit message.