Master Git with These 50 Essential Commands: The Ultimate Cheat Sheet for Developers and DevOps Professionals

Table of contents
- Master Git: Commands Explained in Detail
- Why Learn Git?
- Git Cheat Sheet: The Top 50 Commands You Need to Know
- Git Cheat Sheet: The 50 Most Important Commands
- 1. Git Basics
- 2. Working with Files
- 3. Branching and Merging
- 4. Remote Repositories
- 5. Undoing Changes
- 6. Debugging and Monitoring
- 7. Tagging
- 8. Advanced Techniques
- 9. Maintenance
- 10. Miscellaneous
- 12. Working with Submodules
- 13. Advanced Stashing Commands
- 14. Ignoring Files
- 15. Dealing with Conflicts
- 16. Resetting and Rewriting History
- Conclusion
- Conclusion
Master Git: Commands Explained in Detail
Git has revolutionized the way teams collaborate, track changes, and manage codebases. Whether you’re a software developer, a DevOps engineer, or just starting your coding journey, Git is a must-have skill in your toolbox.
In this blog, I’m sharing a comprehensive Git cheat sheet with 50 essential commands—complete with clear explanations and examples. This guide will help you streamline your workflows and avoid common pitfalls.
Why Learn Git?
Git is more than just a version control system. It enables:
Seamless collaboration across teams.
Efficient tracking of changes in code.
Robust recovery from mistakes or bugs.
Integration with CI/CD pipelines and other DevOps tools.
Whether you're working on a solo project or contributing to a massive open-source repository, Git makes everything easier.
Git Cheat Sheet: The Top 50 Commands You Need to Know
Git Cheat Sheet: The 50 Most Important Commands
1. Git Basics
git config
Configures your Git settings like username and email address. This information is included in every commit.git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
--global
: Applies the configuration to all repositories.Without
--global
: Applies only to the current repository.
git init
Initializes a new Git repository in the current directory.git init
- Creates a
.git
folder to track changes.
- Creates a
git clone
Downloads a remote repository to your local machine.git clone <repository_url>
Example:
git clone https://github.com/user/repo.git
git help
Displays help for any Git command.git help <command>
Example:
git help commit
git version
Shows the installed Git version.git version
2. Working with Files
git add
Adds changes to the staging area.git add file.txt git add . # Adds all changes
- Tracks new files or updates existing ones.
git commit
Saves changes from the staging area to the repository.git commit -m "Your commit message"
- The message should describe the changes made.
git status
Displays the current state of the repository (staged, modified, or untracked files).git status
git diff
Shows the differences between changes.git diff git diff file.txt
- Use before staging or committing to review changes.
git log
Displays the commit history.git log git log --oneline # Short format
git show
Shows details about a specific commit.git show <commit-hash>
git mv
Moves or renames a file.git mv oldname.txt newname.txt
3. Branching and Merging
git branch
Manages branches.git branch # List branches git branch new-branch # Create a branch git branch -d old-branch # Delete a branch
git checkout
Switches to a different branch or commit.git checkout new-branch git checkout <commit-hash> # View a specific commit
git switch
A simpler way to switch branches.git switch main git switch -c new-branch # Create and switch
git merge
Merges another branch into the current one.git merge feature-branch
git rebase
Reapplies commits from one branch onto another.git rebase main
git branch -m
Renames the current branch.git branch -m new-name
4. Remote Repositories
git remote
Connects a local repository to a remote one.git remote add origin <repository_url> git remote -v # List remotes
git fetch
Retrieves changes from a remote repository without merging.git fetch origin
git pull
Fetches and merges changes from a remote repository.git pull origin main
git push
Uploads local commits to a remote repository.git push origin main
git push -u
Sets the upstream branch for future pushes.git push -u origin new-branch
5. Undoing Changes
git reset
Unstages changes or reverts commits.git reset file.txt git reset --hard HEAD~1 # Remove last commit
git revert
Creates a new commit to undo changes.git revert <commit-hash>
git clean
Removes untracked files.git clean -f
git stash
Temporarily saves changes.git stash git stash apply # Restore changes
6. Debugging and Monitoring
git blame
Shows who modified each line of a file.git blame file.txt
git bisect
Finds the commit that introduced a bug.git bisect start
git log --graph
Visualizes the commit history as a graph.git log --graph --oneline
7. Tagging
git tag
Creates a tag to mark a specific commit.git tag v1.0 git push origin --tags # Push tags
8. Advanced Techniques
git cherry-pick
Applies a specific commit to the current branch.git cherry-pick <commit-hash>
git archive
Creates a compressed archive of your repository.git archive --format=tar main > repo.tar
9. Maintenance
git gc
Cleans up unnecessary files.git gc
git fsck
Verifies repository integrity.git fsck
10. Miscellaneous
git describe
Shows the most recent tag associated with the current commit.git describe
git reflog
Logs all actions in the repository.git reflog
11. Advanced Remote and Collaboration Commands
git remote remove
Removes a connection to a remote repository.
git remote remove origin
- Useful when you no longer need to track a remote repository.
git remote rename
Renames a remote repository.
git remote rename oldname newname
- Example: Rename
origin
toupstream
.
git pull --rebase
Fetches changes and rebases your local commits on top of the fetched branch.
git pull --rebase origin main
- Keeps a cleaner commit history by avoiding unnecessary merge commits.
12. Working with Submodules
git submodule add
Adds another repository as a submodule.
git submodule add <repository_url>
- Example: Add a library repository to your project.
git submodule update
Updates submodules to the latest commit.
git submodule update --remote
- Use this to sync submodules with the latest version.
13. Advanced Stashing Commands
git stash list
Lists all stashed changes.
git stash list
- Each stash is labeled with an index for reference.
git stash drop
Deletes a specific stash.
git stash drop stash@{0}
- Replace
stash@{0}
with the index of the stash you want to remove.
git stash pop
Applies and removes the most recent stash.
git stash pop
- Combines
git stash apply
andgit stash drop
.
14. Ignoring Files
.gitignore
Specifies files or directories to ignore in the repository.
echo "node_modules/" >> .gitignore
git add .gitignore
git commit -m "Add .gitignore"
- Commonly used to ignore temporary files or dependencies.
15. Dealing with Conflicts
git mergetool
Launches a merge tool to resolve conflicts.
git mergetool
- Use a GUI tool like
vimdiff
,kdiff3
, ormeld
to simplify conflict resolution.
git diff --cached
Displays differences between the staged changes and the latest commit.
git diff --cached
- Useful for reviewing staged changes before committing.
16. Resetting and Rewriting History
git reset --soft
Moves the HEAD pointer but leaves changes staged.
git reset --soft HEAD~1
- Use when you want to revise the last commit message or add more changes.
git reset --mixed
Moves the HEAD pointer and unstages changes but keeps them in the working directory.
git reset --mixed HEAD~1
- Allows you to modify and re-stage files.
Conclusion
These 50 Git commands will take your version control skills to the next level. Whether you’re just starting or looking to optimize your workflows, this cheat sheet is your ultimate guide.
Save this page for quick reference, and don’t forget to practice these commands in real projects to master them!
📢 What’s Your Favorite Git Command? Drop a comment below!
#Git #DevOps #VersionControl #Coding #CheatSheet
Conclusion
Git is a game-changer for managing code effectively. By mastering these 50 commands, you’ll be able to handle everything from simple commits to complex merges and recoveries with confidence.
If you’re new to Git, start with the basics like git init
, git add
, and git commit
. As you grow more comfortable, dive into advanced features like branching, stashing, and rebasing.
What’s Next?
1️⃣ Start practicing these commands in your projects.
2️⃣ Share this cheat sheet with your team or peers who are learning Git.
3️⃣ Drop a comment below with your favorite Git tip or trick!
📢 Pro Tip: Bookmark this page for quick reference when you’re working with Git.
If you found this cheat sheet helpful, consider sharing it on social media or with your network. Let’s spread the knowledge! 🌟
Happy Learning…
Subscribe to my newsletter
Read articles from Saurabh Namdeo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
