DEV Community

Suman Bhattarai
Suman Bhattarai

Posted on

πŸš€ Ultimate Git Cheatsheet: Your Go-To Guide for Version Control

Git is an essential tool for developers, enabling seamless version control and collaboration. Whether you're a beginner or a seasoned pro, this Git cheatsheet will help you navigate through essential commands with ease! πŸ’‘

πŸ“Œ Table of Contents

  1. Getting Started with Git
  2. Basic Git Commands
  3. Branching and Merging
  4. Working with Remote Repositories
  5. Viewing History and Tracking Changes
  6. Advanced Git Commands
  7. Best Practices & Pro Tips

🏁 Getting Started with Git

βœ… Install Git

Download and install Git from git-scm.com, then verify the installation:

git --version
Enter fullscreen mode Exit fullscreen mode

🎯 Initialize a Repository

git init
Enter fullscreen mode Exit fullscreen mode

Creates a new Git repository in the current directory.

πŸ“₯ Clone an Existing Repository

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Copies a remote repository to your local machine.


πŸ”₯ Basic Git Commands

🧐 Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

Shows the current state of your working directory.

πŸ“Œ Stage Changes

git add <file>
git add .
Enter fullscreen mode Exit fullscreen mode

Adds files to the staging area (. stages all changes).

βœ… Commit Changes

git commit -m "Descriptive commit message"
Enter fullscreen mode Exit fullscreen mode

Saves the staged changes to your local repository.

πŸ› οΈ Modify Last Commit

git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

Allows you to edit the last commit (only use before pushing!).


🌿 Branching and Merging

🌱 Create a New Branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates a new branch.

πŸ”„ Switch Branches

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switches to the specified branch. Alternatively, use:

git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸš€ Create and Switch in One Step

git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates and switches to a new branch.

πŸ”— Merge Branches

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Integrates changes from the specified branch into the current branch.

πŸ—‘οΈ Delete a Branch

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Deletes a branch that has been merged. Use -D to force delete.


🌎 Working with Remote Repositories

πŸ” View Remote Repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

Lists the remote repositories linked to your project.

πŸ”— Add a Remote Repository

git remote add origin <repository-url>
Enter fullscreen mode Exit fullscreen mode

Links a local repo to a remote server.

πŸ”„ Fetch Updates

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Retrieves changes from the remote without merging.

⬇️ Pull Changes

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Updates your local branch with remote changes.

⬆️ Push Changes

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Uploads your commits to the remote repository.


πŸ“œ Viewing History and Tracking Changes

πŸ“š View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

Displays the commit history. Use:

git log --oneline --graph --decorate
Enter fullscreen mode Exit fullscreen mode

for a compact view.

πŸ” Show Specific Commit Details

git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Displays details of a particular commit.

πŸ”¬ Compare Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Shows differences between working directory and staged files.

πŸ“Œ Stash Changes Temporarily

git stash
git stash pop
Enter fullscreen mode Exit fullscreen mode

Saves unfinished work without committing and restores it later.


πŸ› οΈ Advanced Git Commands

🧹 Rebase (Reapply Commits)

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

Reapplies commits on top of another base.

πŸ”„ Reset to a Previous Commit

git reset --soft <commit-hash>
git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

--soft keeps changes staged, --hard erases them.

βͺ Revert a Commit

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that undoes a previous commit.

πŸ’ Cherry-Pick Specific Commits

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Applies a specific commit from another branch.


🎯 Best Practices & Pro Tips

βœ… Commit Often & Use Meaningful Messages πŸ“Œ

βœ… Use Feature Branches for New Changes 🌿

βœ… Regularly Sync with Remote (git pull) πŸ”„

βœ… Resolve Merge Conflicts Carefully 🀝

βœ… Keep Your Repository Clean & Organized 🧹


🏁 Conclusion

Mastering Git is essential for efficient software development. This Git cheatsheet serves as a quick reference for everyday commands and best practices. Keep practicing, experiment with different commands, and soon you'll be a Git expert! πŸš€

Happy coding! πŸ‘¨β€πŸ’»βœ¨

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one serverβ€”search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

AWS Security LIVE! Stream

Streaming live from AWS re:Inforce

Tune into Security LIVE! at re:Inforce for expert takes on modern security challenges.

Learn More