As developers, we run Git commands every day, for pushing code to GitHub, pulling changes from a remote repository, etc directly from our system command line.
No doubt Git and GitHub are amazing, but the real problem is remembering all those Git commands and their flags. Although general commands are simple, like git add, git push, etc, when we need something specific, we need to Google it.
But what if, instead of memorising Git commands, you actually understood the meaning behind each of them, the purpose of its flags, and when to use them? That’s exactly what this guide is for.
In this complete Git cheat sheet, I will explain all Git commands with real examples, so you don’t just copy-paste them, but truly understand how Git works.
Essential Git Commands for Configuration
We start by setting up our environment. These commands help us configure Git to work correctly for our specific needs.
git config
This command sets configuration values for your user name and email. We use it to tell Git who is making the changes. It ensures that commits are attributed to the correct person. It can be set globally or for a single project.
Example: git config --global user.name "Your Name"
git help
This command displays the help pages for other Git commands. We use it when we forget the options or flags for a specific tool. It opens a detailed manual in our terminal or browser.
Example: git help commit
Basic Git Commands for Starting Projects
These Git commands are the first steps we take when creating a new version controlled project.
git init
This command initializes a new empty Git repository. We use it to start tracking a project folder. It creates a hidden subdirectory that stores all the version history metadata.
Example: git init
git clone
This command copies an existing repository from a remote location to our local machine. We use it to download a project so we can work on it. It saves a full copy of the history on our computer.
Example: git clone https://github.com/user/project.git
Git Commands for Saving Changes
Once we start working, we need to save our progress. These Git commands let us stage and save our snapshots.
git status
This command shows the state of the working directory. We use it to see which files have changed or are ready to be committed. It lists untracked, modified, and staged files.
Example: git status
git add
This command moves files from the working directory to the staging area. We use it to select specific changes to include in our next snapshot. It prepares content for the commit.
Example: git add filename.txt
git commit
This command saves the staged changes to the local repository history. We use it to create a permanent record of our project at a specific point in time. We usually include a message to describe the update.
Example: git commit -m "Fixed login bug"
Git Commands for Viewing History
We often need to look back at what happened. These Git commands help us explore the timeline of our project.
git log
This command displays a list of previous commits in reverse order. We use it to see the history of changes, authors, and dates. It helps us understand how the project evolved.
Example: git log
git show
This command shows detailed information about a specific commit. We use it to view the changes made in that snapshot. It displays the commit metadata and the file diffs.
Example: git show abc1234
git diff
This command shows differences between various states of the project. We use it to see exactly what lines changed in a file. It compares the working directory with the staging area or commits.
Example: git diff
Git Commands for Branching and Merging
Branching allows us to work on different tasks separately. These Git commands manage our workflow structure.
git branch
This command lists, creates, or deletes branches. We use it to organize our work into separate lines of development. It lets us isolate features from the main codebase.
Example: git branch feature-x
git checkout
This command switches between branches or restores files. We use it to move our working directory to a different branch. It updates the files in our folder to match the chosen branch.
Example: git checkout main
git switch
This command is a newer way to switch branches. We use it to move to another branch more clearly than checkout. It is designed specifically for changing our view of the tree.
Example: git switch dev
git merge
This command combines the history of two branches together. We use it to bring changes from one branch into another. It integrates our feature work back into the main line.
Example: git merge feature-x
Git Commands for Remote Repositories
Collaboration requires connecting with servers. These Git commands handle the interaction with remote hosting services.
git remote
This command manages the set of remote repositories tracked. We use it to view, add, or remove connections to servers. It links our local project to the cloud.
Example: git remote add origin https://github.com/user/repo.git
git fetch
This command downloads data from remote repositories to our local machine. We use it to see what others have committed without changing our files. It updates our remote tracking references.
Example: git fetch origin
git pull
This command fetches data and immediately merges it into our current branch. We use it to update our local copy with the latest changes from the server. It combines fetch and merge in one step.
Example: git pull origin main
git push
This command uploads our local commits to a remote repository. We use it to share our work with the rest of the team. It sends our changes to the server for storage.
Example: git push origin main
Git Commands for Undoing Changes
Mistakes happen often in development. These Git commands help us fix errors safely.
git reset
This command resets the current branch to a specific state. We use it to unstage files or move the branch pointer. It can change the history if used improperly.
Example: git reset HEAD~1
git revert
This command creates a new commit that undoes a previous change. We use it to safely remove a bug without deleting history. It keeps the timeline linear and intact.
Example: git revert abc1234
git restore
This command discards changes in the working directory. We use it to undo local edits to a file. It resets the file to match the staged commit or HEAD.
Example: git restore file.txt
Advanced Git Commands for Maintenance
For complex workflows, we use these powerful tools to keep our repository clean.
git stash
This command temporarily saves changes that are not ready to commit. We use it when we need to switch context quickly but want to keep our work. It stores the modifications safely on a stack.
Example: git stash
git clean
This command removes untracked files from the working directory. We use it to delete clutter and build artifacts. It cleans up the folder to match the repository state.
Example: git clean -f
git rebase
This command reapplies commits on top of another base tip. We use it to maintain a linear and clean history. It moves our branch to start at the newest point of the main branch.
Example: git rebase main
git cherry-pick
This command applies a specific commit from one branch to another. We use it to bring a single fix to our current branch without merging everything. It is useful for backports.
Example: git cherry-pick abc1234
git reflog
This command shows a log of where our HEAD pointer has been. We use it to recover lost commits or find mistakes. It acts as a safety net for almost any action.
Example: git reflog
Conclusion
Git commands give us full control over code versioning and collaboration. By understanding how each command works, we can handle everyday coding tasks and complex workflows with ease. This Git cheatsheet is meant to be both a learning guide and a quick reference.
To further improve your workflow, check out:
A Guide to Using GitHub Copilot in VS Code (With Video)
You can also check out the official Git cheatsheet, take a screenshot, and save it on your phone for quick reference:
Git Cheat Sheet





