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

Saurabh NamdeoSaurabh Namdeo
7 min read

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

  1. 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.

  2. git init
    Initializes a new Git repository in the current directory.

     git init
    
    • Creates a .git folder to track changes.
  3. git clone
    Downloads a remote repository to your local machine.

     git clone <repository_url>
    
    • Example:

        git clone https://github.com/user/repo.git
      
  4. git help
    Displays help for any Git command.

     git help <command>
    
    • Example:

        git help commit
      
  5. git version
    Shows the installed Git version.

     git version
    

2. Working with Files

  1. git add
    Adds changes to the staging area.

     git add file.txt  
     git add .  # Adds all changes
    
    • Tracks new files or updates existing ones.
  2. git commit
    Saves changes from the staging area to the repository.

     git commit -m "Your commit message"
    
    • The message should describe the changes made.
  3. git status
    Displays the current state of the repository (staged, modified, or untracked files).

     git status
    
  4. git diff
    Shows the differences between changes.

     git diff  
     git diff file.txt
    
    • Use before staging or committing to review changes.
  5. git log
    Displays the commit history.

    git log  
    git log --oneline  # Short format
    
  6. git show
    Shows details about a specific commit.

    git show <commit-hash>
    
  7. git mv
    Moves or renames a file.

    git mv oldname.txt newname.txt
    

3. Branching and Merging

  1. git branch
    Manages branches.

    git branch  # List branches  
    git branch new-branch  # Create a branch  
    git branch -d old-branch  # Delete a branch
    
  2. git checkout
    Switches to a different branch or commit.

    git checkout new-branch  
    git checkout <commit-hash>  # View a specific commit
    
  3. git switch
    A simpler way to switch branches.

    git switch main  
    git switch -c new-branch  # Create and switch
    
  4. git merge
    Merges another branch into the current one.

    git merge feature-branch
    
  5. git rebase
    Reapplies commits from one branch onto another.

    git rebase main
    
  6. git branch -m
    Renames the current branch.

    git branch -m new-name
    

4. Remote Repositories

  1. git remote
    Connects a local repository to a remote one.

    git remote add origin <repository_url>  
    git remote -v  # List remotes
    
  2. git fetch
    Retrieves changes from a remote repository without merging.

    git fetch origin
    
  3. git pull
    Fetches and merges changes from a remote repository.

    git pull origin main
    
  4. git push
    Uploads local commits to a remote repository.

    git push origin main
    
  5. git push -u
    Sets the upstream branch for future pushes.

    git push -u origin new-branch
    

5. Undoing Changes

  1. git reset
    Unstages changes or reverts commits.

    git reset file.txt  
    git reset --hard HEAD~1  # Remove last commit
    
  2. git revert
    Creates a new commit to undo changes.

    git revert <commit-hash>
    
  3. git clean
    Removes untracked files.

    git clean -f
    
  4. git stash
    Temporarily saves changes.

    git stash  
    git stash apply  # Restore changes
    

6. Debugging and Monitoring

  1. git blame
    Shows who modified each line of a file.

    git blame file.txt
    
  2. git bisect
    Finds the commit that introduced a bug.

    git bisect start
    
  3. git log --graph
    Visualizes the commit history as a graph.

    git log --graph --oneline
    

7. Tagging

  1. git tag
    Creates a tag to mark a specific commit.

    git tag v1.0  
    git push origin --tags  # Push tags
    

8. Advanced Techniques

  1. git cherry-pick
    Applies a specific commit to the current branch.

    git cherry-pick <commit-hash>
    
  2. git archive
    Creates a compressed archive of your repository.

    git archive --format=tar main > repo.tar
    

9. Maintenance

  1. git gc
    Cleans up unnecessary files.

    git gc
    
  2. git fsck
    Verifies repository integrity.

    git fsck
    

10. Miscellaneous

  1. git describe
    Shows the most recent tag associated with the current commit.

    git describe
    
  2. git reflog
    Logs all actions in the repository.

    git reflog
    

    11. Advanced Remote and Collaboration Commands

  3. 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.
  1. git remote rename
    Renames a remote repository.
git remote rename oldname newname
  • Example: Rename origin to upstream.
  1. 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

  1. git submodule add
    Adds another repository as a submodule.
git submodule add <repository_url>
  • Example: Add a library repository to your project.
  1. 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

  1. git stash list
    Lists all stashed changes.
git stash list
  • Each stash is labeled with an index for reference.
  1. 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.
  1. git stash pop
    Applies and removes the most recent stash.
git stash pop
  • Combines git stash apply and git stash drop.

14. Ignoring Files

  1. .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

  1. git mergetool
    Launches a merge tool to resolve conflicts.
git mergetool
  • Use a GUI tool like vimdiff, kdiff3, or meld to simplify conflict resolution.
  1. 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

  1. 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.
  1. 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…

0
Subscribe to my newsletter

Read articles from Saurabh Namdeo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Namdeo
Saurabh Namdeo