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
- Getting Started with Git
- Basic Git Commands
- Branching and Merging
- Working with Remote Repositories
- Viewing History and Tracking Changes
- Advanced Git Commands
- Best Practices & Pro Tips
🏁 Getting Started with Git
✅ Install Git
Download and install Git from git-scm.com, then verify the installation:
git --version🎯 Initialize a Repository
git initCreates a new Git repository in the current directory.
📥 Clone an Existing Repository
git cloneCopies a remote repository to your local machine.
🔥 Basic Git Commands
🧐 Check Repository Status
git statusShows the current state of your working directory.
📌 Stage Changes
git add
git add .Adds files to the staging area (. stages all changes).
✅ Commit Changes
git commit -m "Descriptive commit message"Saves the staged changes to your local repository.
🛠️ Modify Last Commit
git commit --amend -m "Updated commit message"Allows you to edit the last commit (only use before pushing!).
🌿 Branching and Merging
🌱 Create a New Branch
git branchCreates a new branch.
🔄 Switch Branches
git checkoutSwitches to the specified branch. Alternatively, use:
git switch🚀 Create and Switch in One Step
git checkout -bCreates and switches to a new branch.
🔗 Merge Branches
git mergeIntegrates changes from the specified branch into the current branch.
🗑️ Delete a Branch
git branch -dDeletes a branch that has been merged. Use -D to force delete.
🌎 Working with Remote Repositories
🔍 View Remote Repositories
git remote -vLists the remote repositories linked to your project.
🔗 Add a Remote Repository
git remote add originLinks a local repo to a remote server.
🔄 Fetch Updates
git fetch originRetrieves changes from the remote without merging.
⬇️ Pull Changes
git pull originUpdates your local branch with remote changes.
⬆️ Push Changes
git push originUploads your commits to the remote repository.
📜 Viewing History and Tracking Changes
📚 View Commit History
git logDisplays the commit history. Use:
git log --oneline --graph --decoratefor a compact view.
🔍 Show Specific Commit Details
git showDisplays details of a particular commit.
🔬 Compare Changes
git diffShows differences between working directory and staged files.
📌 Stash Changes Temporarily
git stash
git stash popSaves unfinished work without committing and restores it later.
🛠️ Advanced Git Commands
🧹 Rebase (Reapply Commits)
git rebaseReapplies commits on top of another base.
🔄 Reset to a Previous Commit
git reset --soft
git reset --hard--soft keeps changes staged, --hard erases them.
⏪ Revert a Commit
git revertCreates a new commit that undoes a previous commit.
🍒 Cherry-Pick Specific Commits
git cherry-pickApplies 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! 👨💻✨