Git Commands Cheat Sheet: 50+ Commands You Need to Know
Git is an essential tool for developers, enabling seamless version control, collaboration, and code management. Whether you’re a beginner or an experienced developer, mastering Git commands is crucial for improving your workflow and efficiency.
But with so many commands, it can feel overwhelming. That’s why we’ve compiled this comprehensive list of 50+ essential Git commands, complete with a free downloadable cheat sheet to keep them at your fingertips!
- Git Commands Cheat Sheet: 50+ Commands You Need to Know
- 📌 What is Git?
- Setting Up Git
- Basic Git Commands
- Branching and Merging
- Remote Repositories: Collaboration and Backup
- Stashing: Saving Temporary Changes
- Rewriting History (Use with Caution!)
- Advanced Git Commands
- Git Configuration Tricks
- Working with Patches
- Undoing Changes
- Bonus Tip: Using .gitignore
- 🚀 Bonus: Free Git Commands Cheat Sheet
- ❓ Frequently Asked Questions (FAQs) – Git Commands
- 🎯 Conclusion
In this article, we’ll walk through 50+ essential Git commands, categorized for easy understanding. Plus, we have a free downloadable cheat sheet to keep handy for quick reference!
📌 What is Git?
Git is a distributed version control system that helps developers track changes in their code, collaborate with team members, and maintain a history of their work. It is widely used in software development, especially when working on projects with multiple contributors.
Setting Up Git
Before using Git, you need to install and configure it.
Install Git
To install Git, visit Git’s official website and download the latest version for your operating system.
Before diving into commands, let’s make sure Git is properly configured.
Command | Description |
---|---|
git config --global user.name "Your Name" | Sets your name for Git commits. Absolutely essential! |
git config --global user.email "your.email@example.com" | Sets your email address for Git commits. Crucial for tracking contributions. |
git config --list | Displays your current Git configuration. A handy way to double-check everything. |
Basic Git Commands
These are the bread and butter of Git. You’ll use them constantly.
Command | Description |
---|---|
git init | Initializes a new Git repository in the current directory. The first step in any Git project. |
git clone <repository_url> | Clones a repository from a URL (e.g., GitHub, GitLab, Bitbucket). Gets you a copy of an existing project. |
git add <file> | Stages a file for commit. Tells Git you want to include changes to this file in the next snapshot. |
git add. | Stages all changes in the current directory. Use with caution! Double-check what you’re adding. |
git commit -m "Your commit message" | Creates a new commit with the staged changes and a descriptive message. The “why” is just as important as the “what.” |
git status | Shows the current status of your repository. Tells you which files are modified, staged, or untracked. Your go-to command. |
git log | Displays the commit history. See who made what changes and when. |
git log --oneline | A more concise version of git log , showing one commit per line. |
git log --graph | Visualizes the commit history as a graph, especially helpful for understanding branching. |
git diff | Shows the changes between your working directory and the staged files. See what you’re about to commit. |
git diff --staged | Shows the changes between the staged files and the last commit. |
git rm <file> | Removes a file from Git. |
git mv <file> <new_file> | Renames or moves a file. |
Branching and Merging
Branching is essential for developing new features without affecting the main codebase.
Command | Description |
---|---|
git branch | Lists all branches in your repository. |
git branch <branch_name> | Creates a new branch. |
git checkout <branch_name> | Switches to a different branch. |
git checkout -b <branch_name> | Creates a new branch and immediately switches to it. A convenient shortcut. |
git merge <branch_name> | Merges a branch into the current branch. Brings changes from one branch into another. |
git branch -d <branch_name> | Deletes a branch (only if it has been merged). |
git branch -D <branch_name> | Force-deletes a branch (use with caution!). |
Remote Repositories: Collaboration and Backup
Remote repositories are where you collaborate with others and back up your code.
Command | Description |
---|---|
git remote | Lists all remote repositories. |
git remote add <name> <url> | Adds a new remote repository. |
git fetch <remote_name> | Downloads changes from a remote repository but doesn’t merge them. |
git pull <remote_name> <branch_name> | Downloads changes and merges them into the current branch. A common workflow. |
git push <remote_name> <branch_name> | Uploads changes to a remote repository. |
Stashing: Saving Temporary Changes
Stashing is useful for saving changes you’re not ready to commit.
Command | Description |
---|---|
git stash | Saves your uncommitted changes. |
git stash pop | To reapply stashed changes |
git stash list | Lists all your stashed changes. |
git stash apply | Applies the most recent stashed changes. |
git stash apply stash@{n} | Applies a specific stashed change (where n is the stash index). |
git stash drop stash@{n} | Removes a specific stashed change. |
git stash clear | Removes all stashed changes. |
Rewriting History (Use with Caution!)
These commands can be powerful but should be used carefully as they alter the commit history.
Command | Description |
---|---|
git reset <file> | Unstages a file. |
git reset --hard <commit_hash> | Resets the repository to a previous commit (use with extreme caution!). |
git revert <commit_hash> | Creates a new commit that undoes the changes of a previous commit. A safer way to undo changes. |
git rebase <branch_name> | Reapplies a series of commits onto another branch. Can be used to clean up commit history. |
Advanced Git Commands
These commands are for more specialized tasks.
Command | Description |
---|---|
git bisect | Helps find the commit that introduced a bug. |
git blame <file> | Shows who made changes to each line of a file. |
git grep <pattern> | Searches for a pattern in your codebase. |
git submodule | Manages submodules within your repository. |
git cherry-pick <commit_hash> | Applies a specific commit to your current branch. |
git reflog | Shows a log of all your local actions in Git, including changes to branches and resets. |
git commit --amend -m "New commit message" | Amend Last Commit |
Git Configuration Tricks
Command | Description |
---|---|
git config --global alias.<alias_name> "<command>" | Creates an alias for a Git command. |
git config --global color.ui true | Enables colored output for Git commands. |
Working with Patches
Command | Description |
---|---|
git format-patch <commit_hash> | Creates a patch file from a commit. |
git am <patch_file> | Applies a patch file. |
Undoing Changes
Command | Description |
---|---|
git checkout -- <file> | Discards changes in your working directory for a specific file. |
git clean -fd | Removes untracked files and directories. Be very careful with this! |
Bonus Tip: Using .gitignore
Command | Description |
---|---|
Create .gitignore | Create a .gitignore file to specify files Git should ignore. |
🚀 Bonus: Free Git Commands Cheat Sheet
To make your Git journey even smoother, we’ve created a free downloadable cheat sheet with all these commands. Print it out, keep it handy, and you’ll be a Git master in no time!
![50+ Essential Git Commands (With Downloadable Cheat Sheet) 1 Git Commands Cheat Sheet](https://www.aveshost.com/blog/wp-content/uploads/2025/02/Git-Commands-Cheat-Sheet-1024x576.png)
Grab your free 2560×1440 cheat sheet wallpaper for quick access.
Download The Git Cheat Sheet❓ Frequently Asked Questions (FAQs) – Git Commands
Git is used for tracking changes in code, collaborating with developers, and maintaining a history of code changes.
Use git revert commit-hash
to create a new commit that undoes the changes, or git reset --hard HEAD~1
to remove it entirely.
git fetch
and git pull
?git fetch
downloads changes without applying them, while git pull
fetches and merges changes into your current branch.
Use git status
to identify conflicting files, manually edit them, then use git add filename
and git commit
to finalize.
Use git commit --amend -m "New message"
to modify the last commit message before pushing.
git stash
do?git stash
temporarily saves changes without committing them, allowing you to switch branches or work on something else.
Use git push origin --delete branch-name
to remove a branch from the remote repository.
Git is essential for any software developer. It allows you to track changes to your code, collaborate effectively with others, revert to previous versions, and experiment without fear of losing your work. It’s a core skill in the industry.
git add
and git commit
?git add
stages your changes, meaning you tell Git which specific modifications you want to include in the next snapshot. git commit
then creates that snapshot, permanently recording the staged changes in the repository’s history. Think of add
as selecting the ingredients and commit
as baking the cake.
Branching is crucial for working on new features or bug fixes in isolation. It prevents your changes from affecting the main codebase until you’re ready to merge them in. It’s a best practice for collaborative development.
Yes, Git provides several ways to undo changes. The specific command depends on what you want to undo. We cover some of these in the “Rewriting History” and “Undoing Changes” sections, but always be cautious when altering history. git reflog
can often be a lifesaver!
.gitignore
file?The .gitignore
file lets you specify files and folders that Git should ignore. This is important for excluding things like temporary files, build artifacts, or sensitive information that you don’t want to include in your repository.
🎯 Conclusion
Mastering Git takes time and practice, but with this comprehensive guide and your handy GIT Commands Cheat Sheet, you’ll be well on your way. Don’t be afraid to experiment, explore, and dive deeper into the commands that are most relevant to your workflow.
Did we miss any important commands? Let us know in the comments! 🚀
Happy coding!
Suggested Reading:
- How to Remove .html, .php, or Both from URLs
- How to Set Up a MySQL Database & User in cPanel (2 Easy Methods)
- How to Upload Your Website (in 3 Simple Steps)
- How to Deploy Laravel Project on cPanel
- How to Buy cPanel Hosting for Your Website: Beginner’s Guide
- How to Flush DNS Cache on Windows, Mac, Linux & Browsers
- How to Start a Blog and Make Money: Beginner’s Guide
- How to Remove Public From Laravel URL (4 Easy Methods)