Essential git cheat sheet for loners

Posted on Wed 22 January 2025 in git

This are the basic commands to operate with git as a loner: no branches, no remotes, no merges... Just the basic stuff.

The minimum concepts you need to know are:

  • Git is a version control system. It allows you to store changes in your files. Don't mix it with GitHub, which is a platform to store your repositories.
  • Working directory: the directory where you are working.
  • Staging area: a place to store changes before committing them.
  • Repository: the place where the changes are stored.
  • Commit: a set of changes stored in the repository. They are identified by a hash. For example, commit 1234567.

The basic workflow is:

  1. Make changes in the working directory.
  2. Add changes to the staging area.
  3. Commit changes to the repository.
  4. Repeat.

Conceptually: working directory -> staging area -> repository.

Configuration

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Create a new repository

In the directory of your project:

git init

Add files to the repository

This adds all the changes in your current directory to the staging area.

git add .

If you only want to add a specific file:

git add file.txt

Commit changes

This commits the changes in the staging area to the repository.

git commit -m "Your commit message"

Check the status of the repository

git status

Check the history of the repository

git log

Compare changes

Working directory vs repository:

git diff

Staging area vs repository:

git diff --staged

Discard changes in the working directory

git checkout -- .

Caution: This removes all uncommitted changes. For specific files, use git checkout -- filename.

Discard changes in the staging area

git reset

Any other command you need, you can find it in the official documentation or, better, ask to an LLM like ChatGPT.

Probably, the next step is to learn about remotes and, then, branches.

git