- Gerardo Catalas | Backend Developer (.NET & SQL Server)/
- Blog/
- Git Cheat Sheet: Essential and Advanced Commands/
Git Cheat Sheet: Essential and Advanced Commands
Table of Contents
Mastering Git is fundamental in modern development. This guide is structured to serve as both a learning tool and a quick reference (Cheat Sheet).
1. The Essentials (Your Swiss Army Knife) #
If you need to start working quickly, this is the list of the most important commands that will get you out of almost any trouble in your day-to-day work.
| Command | Action |
|---|---|
git init | (Start) Turns the current folder into a local Git repository. |
git clone <url> | (Start) Downloads a complete remote repository to your machine. |
git status | (Daily) Checks which files you have modified and what branch you are on. |
git add . | (Daily) Stages all modified files to be saved. |
git add <file> | (Daily) Adds a specific file to the staging area. |
git commit -m "msg" | (Daily) Saves the changes locally with a descriptive message. |
git push / git pull | (Daily) Uploads (push) or downloads (pull) changes from the server. |
git fetch <remote> | (Daily) Downloads branches and data from the remote without modifying your code. (Safer than git pull as it allows you to review before merging). |
git switch <branch> | (Branches) Switches branches. (Modern form of the classic git checkout <branch>)1. |
git switch -c <branch> | (Branches) Creates a new branch and switches to it. (Equivalent to git checkout -b <branch>). |
2. Fundamental Concepts #
Before diving into the commands, it is vital to understand how Git manages information.
The 3 States of Git
A file in Git always lives in one of these three areas:
- Working Directory: Your local folder where you edit files.
- Staging Area (Index): The preparation area. Here you “point out” which changes you want to include in your next snapshot (commit).
- Local Repository: Where versions (commits) are permanently saved on your machine.
3. Configuration and Creation #
Initial Configuration #
Set up your identity. This is saved in your global .gitconfig file.
| Command | Description |
|---|---|
git config --global user.name "Name" | Sets your author name for commits. |
git config --global user.email "email" | Sets your author email. |
git config --global core.editor "code --wait" | Sets VS Code (or your preferred editor) as the Git editor. |
git config --global --edit | Opens the configuration file manually. |
Initialize and Clone #
| Command | Description |
|---|---|
git init <folder> | Creates a new directory and initializes it as a repository. |
git clone <url> | Downloads a complete project from a remote server (HTTP/SSH). |
4. Detailed Workflow #
Change Management (Add & Commit) #
The heart of version control consists of creating meaningful restore points.
| Command | Description |
|---|---|
git add . | Adds all changes in the current folder to the staging area. |
git add <file> | Adds a specific file to the staging area. |
git commit -m "msg" | Creates a commit with the changes prepared in the staging area. |
git commit -am "msg" | Shortcut: Adds all modified files (already tracked) and commits in one step. |
git commit --amend | Corrects the last commit (adds forgotten files or changes the message). |
git rm --cached <file> | Stops tracking a file in Git but keeps it intact on your hard drive. |
Checking the History #
To understand the evolution of the project or find out when an error was introduced.
💡 Tip: When using long log commands, press the q key to exit the reading view and Space to page down.
| Command | Description |
|---|---|
git log --oneline | Shows the history in simple, compact lines. |
git log --graph --decorate | Visualizes branches and tags graphically in the console. |
git log -p <file> | Shows the detailed code changes made to a specific file. |
git diff | Compares the changes in your working directory that are not yet in staging. |
git diff --cached | Compares what you have prepared in staging against your last commit. |
git switch -d <commit> | Travels to a specific commit in read-only mode (detached HEAD) to explore its files. The commit hash is obtained by using git log first. Finally, use git switch - to return to your original branch. |
5. Branches and Collaboration #
Branch Management #
Branches allow you to develop features in parallel without affecting the main line (main or master).
| Command | Description |
|---|---|
git branch -a | Lists all branches (both local and remote). |
git switch <branch> | Switches to an existing branch. (The classic git checkout <branch> command is still valid, but switch is recommended). |
git switch -c <branch> | Creates a new branch and switches to it immediately. (Modern alternative to git checkout -b). |
git merge <branch> | Brings changes from another branch and merges them into your current branch. |
git branch -d <branch> | Deletes a branch locally (only if it has been safely merged). |
Remote Repositories #
Synchronize your work with platforms like GitHub, GitLab, or Bitbucket.
| Command | Description |
|---|---|
git remote add origin <url> | Links your local repository with a new remote server. |
git fetch <remote> | Downloads branches and data from the remote without modifying or merging your current code. |
git push -u origin <branch> | Uploads your branch and links it so that in the future you only need to type git push. |
git push origin --delete <branch> | Deletes a branch directly on the remote repository. |
git push --force | Caution: Forces the upload, overwriting the remote. Use with extreme care2. |
6. Troubleshooting and Undoing #
Undoing Changes #
Git is very forgiving; almost anything can be recovered as long as you don’t delete the hidden .git folder.
| Command | Description |
|---|---|
git restore <file> | Discards local changes in a file and returns it to the state of the last commit1. |
git reset HEAD <file> | Removes a file from the staging area, preserving your code modifications. |
git revert <commit> | Creates a new commit that safely undoes exactly what a previous one did. |
git reset --hard <commit> | Danger: Deletes all unsaved work and reverts to the state of a specific commit. |
Temporary Storage (Stash) #
Useful when you need to switch branches urgently but don’t want to commit half-finished work.
| Command | Description |
|---|---|
git stash | Saves your current changes in a temporary stack and leaves your working directory clean. |
git stash pop | Retrieves your last saved work and removes it from the stack. |
git stash apply | Retrieves your last saved work, but keeps it in the stack (does not remove it). |
git stash list | Lists all the states you have temporarily saved. |
7. Advanced Tools #
For when you need to clean up history or perform complex maintenance tasks.
Rebase and History #
| Command | Description |
|---|---|
git rebase <branch> | Moves the base of your current branch to the end of another branch, maintaining a linear history. |
git rebase -i HEAD~3 | Opens an interactive mode to edit, squash, or delete the last 3 commits. |
git reflog | Detailed record of every movement of the HEAD pointer; it’s your lifesaver for rescuing “lost” commits. |
Tags and Cleanup #
| Command | Description |
|---|---|
git tag v1.0.0 | Creates a lightweight tag to mark an important release version. |
git push origin --tags | Uploads all local tags to the remote server (they do not upload by default). |
git clean -fd | Deletes files and folders from the disk that are not being tracked by Git. |
Notes and References #
Why is
git checkoutno longer recommended? Historically, this command was overloaded: it was used both to switch branches and to restore modified files. This mix of functions was confusing and prone to errors, especially for beginners. Therefore, starting from version 2.23 (2019), Git divided its responsibilities into two much more logical and safe commands:git switch(exclusive for managing branches) andgit restore(exclusive for undoing changes in files). ↩︎ ↩︎Best practice: Instead of
--force, try usinggit push --force-with-lease. It’s a safer version that will prevent you from overwriting a teammate’s work if they pushed changes to the remote that you haven’t downloaded yet. ↩︎