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

🎯 Initialize a Repository

git init

Creates a new Git repository in the current directory.

📥 Clone an Existing Repository

git clone

Copies a remote repository to your local machine.


🔥 Basic Git Commands

🧐 Check Repository Status

git status

Shows 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 branch

Creates a new branch.

🔄 Switch Branches

git checkout

Switches to the specified branch. Alternatively, use:

git switch

🚀 Create and Switch in One Step

git checkout -b

Creates and switches to a new branch.

🔗 Merge Branches

git merge

Integrates changes from the specified branch into the current branch.

🗑️ Delete a Branch

git branch -d

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


🌎 Working with Remote Repositories

🔍 View Remote Repositories

git remote -v

Lists the remote repositories linked to your project.

🔗 Add a Remote Repository

git remote add origin

Links a local repo to a remote server.

🔄 Fetch Updates

git fetch origin

Retrieves changes from the remote without merging.

⬇️ Pull Changes

git pull origin

Updates your local branch with remote changes.

⬆️ Push Changes

git push origin

Uploads your commits to the remote repository.


📜 Viewing History and Tracking Changes

📚 View Commit History

git log

Displays the commit history. Use:

git log --oneline --graph --decorate

for a compact view.

🔍 Show Specific Commit Details

git show

Displays details of a particular commit.

🔬 Compare Changes

git diff

Shows differences between working directory and staged files.

📌 Stash Changes Temporarily

git stash
git stash pop

Saves unfinished work without committing and restores it later.


🛠️ Advanced Git Commands

🧹 Rebase (Reapply Commits)

git rebase

Reapplies 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 revert

Creates a new commit that undoes a previous commit.

🍒 Cherry-Pick Specific Commits

git cherry-pick

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! 👨‍💻✨