Skip to content

Essential Commands

We will mainly use so-called "porcelain" commands: they are high-level and consist of a single word (log, add, commit...), unlike "plumbing" commands, which are lower-level and consist of multiple words (cat-file, write-tree...).

git init

This command initializes a new Git repository by creating a .git folder where the command is executed, and this folder will contain all the necessary files for the repository. Otherwise, it will contain all the data of the content database of this repository.

git log

The git log command allows you to view the commits of one or more branches (usually one, and by default the current branch) in reverse chronological order. By default, this command displays the following data for each commit:

  • commit hash (SHA-1)
  • optionally the refs associated with this commit
  • commit author (name and email)
  • commit date
  • full commit message

There are a (very) large number of options for this command, here are a few:

  • --oneline allows displaying each commit on a single line (abbreviated hash, optionally the associated refs, and the first line of the commit message)
  • --graph allows displaying the commit graph
  • -<n> where n is an integer, allows displaying the n most recent commits (e.g., git log -2 displays the last 2 commits)
  • -S <query> allows displaying commits that contain the string <query> in the added, deleted, or modified lines (e.g., git log -S 'Promise' will list all commits where a line containing the word 'Promise' was added, deleted, or modified)
  • --grep <regex> allows displaying all commits that have the given pattern in their commit message (e.g., git log --grep '^f.{2,3}:')

git add

The git add command allows adding an entire folder, and/or specific files or specific parts of certain folders and/or files to the index.

The index is called the "staging area" in English (sometimes "preparation area") and corresponds to the temporary state of the next commit: only what is in the index will be added to the commit by the git commit command (see below).

To add one or more specific files, simply add the full relative path to the file(s):

shell
git add index.html package.json

You can also add an entire folder, in which case all files and subfolders in that folder will be added to the index:

shell
git add src

The simplest (and highly discouraged, as it is very unspecific) option to add your changes to the index is to use the -A option of git add: all changes present in the working copy will be added to the index.

It is highly recommended to add only specific parts, using the -p option (for patch). Note: this option only allows adding changes to already tracked files, it will not add new files to the index.

shell
git add -p

This command will launch a script that will stop at each fragment to ask if the fragment should be added to the index (y then Enter) or not (n then Enter). To see other options, type ? and Enter. One interesting option is s which allows further splitting the fragments if you only want some of the proposed changes.

git commit

The git commit command allows you to validate a set of changes: everything in the index will be present in the commit. A commit must have a non-empty commit message.

shell
git commit -m "chore: 🎉 Init project"

You can easily modify the last commit: make the desired changes, add them to the index, and run the following command:

shell
git commit --amend

This will replace the commit with a new one (the hash will necessarily be different), which will contain the previous changes plus the newly added ones.