Logo of the site

My cheatsheet

Git cheatsheet

This is a quick reference cheat sheet for understanding and writing Git.

# Getting started

Introduction

Git is the industry-standard version control system that allows multiple developers to work on a project simultaneously by tracking changes and coordinating work.

git workflow described visually

# Basics

Initialize a new Git repository

Creates a new .git folder and initializes version control.

git init

Response

Initialized empty Git repository in /home/ccuser/workspace/manhattan-zoo-1/.git/

Check local changes

Shows untracked or modified files, and branch info.

git status

Response

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        scene-1.txt

nothing added to commit but untracked files present (use "git add" to track)

Add files to staging area

Adds single, multiple or all modified files to staging.

git add filename_you_are_editing
git add file1 file2
git add .
git status

Response

 On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   scene-1.txt

Check diff between working directory and staging

Shows what changed in your files before committing.

git diff

Response

diff --git a/scene-1.txt b/scene-1.txt
index c33ce4c..1e73963 100644
--- a/scene-1.txt
+++ b/scene-1.txt
@@ -1 +1,2 @@
 Harry Programmer and the Sorcerer’s Code: Scene 1
+Dumblediff: I should've known you would be here, Professor McGonagit.
\ No newline at end of file

Commit staged changes

Add a message with -m in present tense, 50 chars max.

git commit -m "Complete first line of dialogue"

Commit message conventions

  • Use quotation marks
  • Write in present tense
  • Keep it short (≤ 50 characters)
[master (root-commit) a659c51] Prima riga di codice fatta
 1 file changed, 2 insertions(+)
 create mode 100644 scene-1.txt

View commit history

git log

Lists commits in reverse chronological order.

commit a659c518f418547ce82b4557a86ffcee61ee33f4
Author: codecademy <ccuser@codecademy.com>
Date:   Sun Oct 27 22:00:38 2024 +0000

    Prima riga di codice fatta

Output info includes:

  • Commit SHA (unique 40-character ID)
  • Author name/email
  • Date and time
  • Commit message

# Backtrack

Show HEAD commit

Displays the latest commit’s metadata and all file changes it includes.

git show HEAD

Response

commit fa2706aa2214a3e6b3ba1d2f62b4acbf9c04fcce
Author: codecademy <ccuser@codecademy.com>
Date:   Sun Oct 27 22:18:14 2024 +0000

    ghost response scene 5

diff --git a/scene-5.txt b/scene-5.txt
index b12dd97..fa75b29 100644
--- a/scene-5.txt
+++ b/scene-5.txt
@@ -11,4 +11,7 @@ Mark me.
 Hamlet:
 I will.

-
+Ghost:
+My hour is almost come,
+When I to sulphurous and tormenting flames
+Must render up myself.

Restore last commit's version

Reverts a file to its state in the last commit.

git checkout HEAD filename

Checkout specific file

Restores a file from the latest commit.

git checkout -- filename

Same as git checkout HEAD filename

Reset I - Unstage file

Removes a file from the staging area (keeps changes in working directory).

git reset HEAD filename

Response

Unstaged changes after reset:
M       scene-2.txt
  • M = modified

Reset II - Back to a commit

Moves the repo back to a previous commit (by SHA).

git reset 5d69206

Modes of reset

  • --soft: Only moves HEAD, keeps staged changes
  • --mixed: Updates HEAD & staging area (default)
  • --hard: Removes all changes
reset schema

Discard all uncommitted changes

Resets all files in working directory to match HEAD.

git checkout -- .

This discards local changes not yet staged or committed.

# Stash

What is Git Stash?

Temporarily stores changes in the working directory that are not yet ready to be committed. Useful when you need to switch branches or fix a different issue before resuming work.

Stash changes

Saves uncommitted changes and clears the working directory.

git stash

Used when an interruption occurs and you need to save progress temporarily.

Pop stashed work

Restores the most recently stashed changes and removes them from the stash list.

git stash pop

Use after resolving the interruption to resume your previous work.

# Advanced Commands

Log - One line

Displays each commit on one line (short SHA and message).

git log --oneline

Log - Search keyword

Finds commits where the number of keyword occurrences changes.

git log -S "Add"

Matches commits where "Add" was added or removed.

Log - Graph view

Shows a graphical representation of branch history.

git log --oneline --graph

Helpful to visualize merges and branch structure.

Commit --amend

Rewrites the last commit (useful to fix message or add changes).

git commit --amend

Replaces the last commit instead of adding a new one.

If you want to keep the same commit message:

git commit --amend --no-edit

Create aliases

Shorten frequently used commands via Git config.

git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.glop "log --pretty=format:\"%h %s\" --graph"

After defining, you can use git co example_branch instead of git checkout example_branch

Enhanced Git Log

Show decorated, compressed graph of all commits:

git log --graph --decorate --oneline --all