Git is a must-know for any developer. Whether you're flying solo on a project or working with a team, mastering Git commands is crucial for smooth version control.
In this cheat sheet, we’ll walk through the most popular Git commands, along with a few hidden gems that can help make your workflow faster and more efficient.
Why Git is Powerful
Before we get into the commands, let’s take a quick moment to appreciate why Git is such a powerhouse.
It’s a distributed version control system, meaning it lets you track changes to your code over time and collaborate seamlessly with others. Plus, it’s super fast, flexible, and works offline—making it the go-to tool for developers managing projects of all sizes.
Now, let’s get into the fun part—the commands!
All Your Git Commands in a Cheat Sheet
Here’s a table summarizing the essential Git commands:
Command | Description | Example |
---|---|---|
git init | Initializes a new Git repository. | git init |
git clone <repository_url> | Clones a remote repository to your local machine. | git clone https://github.com/user/repo.git |
git add <file_name> | Stages specific file changes for the next commit. | git add index.html |
git add . | Stages all modified files for the next commit. | git add . |
git commit -m "<message>" | Commits the staged changes with a descriptive message. | git commit -m "Updated the homepage layout" |
git status | Shows the status of the repository, including staged, unstaged, and untracked files. | git status |
git log | Displays the commit history of the repository. | git log |
git branch | Lists all branches or creates a new branch. | git branch or git branch <branch_name> |
git checkout <branch_name> | Switches to a different branch. | git checkout development |
git merge <branch_name> | Merges changes from another branch into the current branch. | git merge feature-branch |
git push <remote_name> <branch_name> | Pushes local commits to the remote repository. | git push origin main |
git pull <remote_name> <branch_name> | Fetches and merges changes from the remote repository into your local branch. | git pull origin main |
git fetch <remote_name> | Fetches new commits from the remote without merging them. | git fetch origin |
git reset --hard <commit> | Resets the repository to a specific commit and discards all changes. | git reset --hard 123abc |
git revert <commit> | Reverts a specific commit by creating a new commit that undoes the changes. | git revert 123abc |
git stash | Temporarily saves changes that are not ready to commit. | git stash |
git stash pop | Reapplies changes that were previously stashed. | git stash pop |
git tag <tag_name> | Creates a lightweight tag for a specific commit. | git tag v1.0.0 |
git tag -a <tag_name> -m "<message>" | Creates an annotated tag with additional information. | git tag -a v1.0.0 -m "First stable release" |
git push origin <tag_name> | Pushes a tag to the remote repository. | git push origin v1.0.0 |
git log --oneline | Displays a simplified version of the commit history with one line per commit. | git log --oneline |
git clean -f | Removes untracked files from the working directory. | git clean -f |
git bisect start | Begins the binary search for a commit that introduced a bug. | git bisect start |
git config --global user.name <name> | Sets the Git username globally on your machine. | git config --global user.name "John Doe" |
git config --global user.email <email> | Sets the Git email globally on your machine. | git config --global user.email "john@example.com" |
6 Essential Git Commands to Get You Started
1. git init
This command kicks off a new Git repository in your current directory. It’s your starting point when creating a new Git project.
git init
2. git clone
Want to make a copy of an existing repository? Use git clone
. This creates a local copy of a remote repository right on your machine.
git clone <repository_url>
3. git add
To stage your changes and get them ready for the next commit, use git add
. It tells Git which files you want to include in your next snapshot.
git add <file_name> # Add a specific file
git add . # Add all changed files
4. git commit
Once your changes are staged, you’ll want to commit them. This creates a snapshot of your project at that moment in time.
git commit -m "Your commit message"
5. git status
Know what’s changed or what’s ready to commit. git status
shows you the current state of your working directory and staging area.
git status
6. git log
If you want to see the commit history, use git log
. It gives you a list of all the commits in the repository, including messages, author details, and timestamps.
git log
How to Work with Branches in Git
Branches in Git let you work on different versions of your project at the same time. Below are the commands you’ll need to manage branches effectively.
7. git branch
Use git branch
to list all the branches in your repository or create a new one.
git branch # List all branches
git branch <branch_name> # Create a new branch
8. git checkout
Switch between branches with git checkout
. You can also use it to checkout specific commits or files.
git checkout <branch_name> # Switch to an existing branch
git checkout -b <branch_name> # Create and switch to a new branch
9. git merge
When you’re ready to bring changes from one branch into another, use git merge
. This is especially handy for merging features from a development branch into your main branch.
git merge <branch_name>
Master These Advanced Git Commands
1. git rebase
Rebasing lets you integrate changes from one branch into another with a cleaner history. It's useful for keeping a feature branch up to date with the main branch.
git rebase <branch_name>
2. git cherry-pick
Use git cherry-pick
when you need to apply a specific commit from one branch to another. It’s helpful when you don’t want to merge an entire branch, just a single change.
git cherry-pick <commit_hash>
3. git revert
If you need to undo a commit but still want to keep the history intact, git revert
this is your go-to. It creates a new commit that undoes the changes of a previous commit.
git revert <commit_hash>
4. git stash
Need to switch branches but don’t want to commit your changes yet? git stash
temporarily saves your changes and reverts to the last commit. When you're ready, you can reapply them.
git stash # Save your changes
git stash pop # Reapply the changes
5. git diff
Want to see the differences between your working directory and the last commit? git diff
shows you exactly what’s changed.
git diff # See unstaged changes
git diff --staged # See staged changes
Now that you’re familiar with the basic Git commands, let’s explore how to get started with remote repositories, undo mistakes, and use tags effectively.
Get Started with Remote Repositories in Git
Git lets you work with remote repositories, making it easy to sync your local work with others. Here are the commands you'll need to manage remote repositories.
1. git remote
To view or set the remote repositories for your project, use git remote
.
git remote -v # List the remote repositories
git remote add <name> <url> # Add a new remote repository
2. git push
Push your commits to a remote repository with git push
. This updates the remote branch with your local commits.
git push <remote_name> <branch_name>
3. git pull
If you need to fetch the latest changes from a remote repository and merge them into your local branch, use git pull
.
git pull <remote_name> <branch_name>
4. git fetch
Unlike git pull
, git fetch
only downloads the new commits from the remote repository without merging them. This lets you review the changes before merging.
git fetch <remote_name>
How Can You Use Tags and Manage Versioning in Git
Tags in Git are like bookmarks for important moments in your project’s history—perfect for marking releases or milestones. They make versioning easier and let you easily refer to key commits.
Create a Lightweight Tag
A lightweight tag is a simple pointer to a commit.
git tag <tag_name>
Create an Annotated Tag
An annotated tag includes additional information like the tagger’s name, date, and message.
git tag -a <tag_name> -m "Tag message"
Push Tags to Remote
By default, tags are local. To share them with others, push them to the remote repository.
git push origin <tag_name>
Tags are super useful for keeping track of releases and other significant project changes.
How Can You Undo Changes and Fix Mistakes in Git
Mistakes happen, but Git has your back when it comes to fixing them. Whether you need to reset, revert, or discard changes, here are some commands to help you get back on track:
git reset
Use this to reset your repository to a previous commit. Depending on the option you use (--soft
, --mixed
, or --hard
), it can either just unstage changes or discard them completely.
git reset --soft <commit> # Reset the commit, keep changes staged
git reset --hard <commit> # Reset and discard all changes
git checkout
If you want to discard changes in a specific file and revert it to the last committed state, git checkout
is your go-to.
git checkout -- <file> # Discard changes in a specific file
git revert
Unlike git reset
, git revert
is safe for public history. It creates a new commit that undoes the changes from a previous commit, without messing with the commit history.
git revert <commit> # Undo a commit by creating a new one
These commands give you the flexibility to fix mistakes in a way that fits your workflow—whether you’re undoing a small error or rolling back bigger changes.
Lesser-Known Git Commands
While the basic commands cover most of what you’ll need, these lesser-known Git commands can help speed up your workflow or solve specific problems.
1. git log --oneline
For a more concise version of git log
, use --oneline
. It shows each commit on a single line, including the commit hash and message.
git log --oneline
2. git clean
If you have untracked files in your working directory that you want to delete, git clean
is the way to go. Use it with caution, as it will permanently remove files.
git clean -f # Remove untracked files
git clean -fd # Remove untracked files and directories
3. git shortlog
Need a summary of the commit history by author? git shortlog
groups commits by author and shows how many commits each author has made.
git shortlog
4. git bisect
To find the commit that introduced a bug, use git bisect
. It does a binary search through the commit history, checking out commits between a known “good” and “bad” commit to find the issue.
git bisect start
git bisect bad <commit_hash>
git bisect good <commit_hash>
5. git config
Use git config
to configure Git for your local machine or a specific repository. You can set things like your username, email, default editor, and more.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Conclusion
Git is a powerful tool that can save you from a lot of headaches when managing your code. If you’re just starting out or you’re already experienced, this cheat sheet should help you master the essentials and advanced commands.
With these tools, you’ll streamline your workflow, troubleshoot more efficiently, and keep your codebase clean.
Happy coding!