Git is an essential tool for developers, enabling efficient version control and collaboration. Below, we answer the most popular Git questions to help you master this powerful tool.
General Git Basics
1. What is Git, and how does it differ from other version control systems?
Git is a distributed version control system that tracks changes to files and supports collaboration. Unlike centralized systems, Git allows every user to have a full copy of the repository.
2. What is the difference between Git and GitHub?
Git is a version control tool, while GitHub is a cloud-based platform for hosting and managing Git repositories.
3. How do you initialize a Git repository?
Run:
git init
This creates a new Git repository in your project directory.
4. What is a commit in Git, and how do you create one?
A commit records changes in the repository. To create a commit:
git add .
git commit -m "Commit message"
5. How do you check the status of your repository?
Use:
git status
6. What is the purpose of the .gitignore file?
It specifies files and directories Git should ignore, e.g., node_modules/.
7. How do you view the history of commits in a repository?
Run:
git log
Branching and Merging
8. How do you create a new branch in Git?
git branch branch_name
9. How do you switch between branches in Git?
git checkout branch_name
Or, in modern Git versions:
git switch branch_name
10. What is the difference between merge and rebase?
-
Merge: Combines branches, creating a merge commit.
-
Rebase: Integrates changes linearly, rewriting commit history.
11. How do you resolve merge conflicts in Git?
Open the conflicting file, edit manually, then:
git add file_name
git commit
12. What is the purpose of the main or master branch?
It’s the default branch containing stable code.
13. How do you delete a branch in Git?
git branch -d branch_name
Staging and Changes
14. What is the difference between git add and git commit?
-
git add: Moves changes to the staging area. -
git commit: Saves staged changes to the repository.
15. How do you undo the last commit in Git?
git reset --soft HEAD~1
16. How do you discard changes in your working directory?
git checkout -- file_name
17. What is the staging area in Git?
A space where changes are prepared (staged) before committing.
18. How do you compare changes between commits or branches?
git diff commit_id1 commit_id2
Remote Repositories
19. How do you clone a repository in Git?
git clone repository_url
20. What is the difference between git pull and git fetch?
-
git pull: Fetches and integrates changes. -
git fetch: Only fetches changes without integrating.
21. How do you push changes to a remote repository?
git push origin branch_name
22. What is the difference between origin and upstream?
-
origin: Your fork’s remote repository. -
upstream: The original repository you forked from.
23. How do you add a remote repository in Git?
git remote add origin repository_url
24. How do you fork a repository and contribute to it?
-
Fork on GitHub.
-
Clone your fork.
-
Push changes and create a pull request.
Advanced Git Topics
25. What is a detached HEAD in Git, and how do you fix it?
It means you’re not on a branch. Fix it by switching to a branch:
git checkout branch_name
26. How do you squash commits in Git?
git rebase -i HEAD~n
Replace pick with squash for commits you want to combine.
27. What is cherry-picking in Git?
Applying a specific commit from one branch to another:
git cherry-pick commit_id
28. How do you rebase a branch in Git?
git rebase branch_name
29. What is the purpose of Git tags, and how do you create one?
Tags mark specific points in history, like releases.
git tag -a v1.0 -m "Version 1.0"
30. What is the difference between soft, mixed, and hard resets?
-
Soft: Keeps changes in staging.
-
Mixed: Keeps changes in working directory.
-
Hard: Discards all changes.
Git Configuration and Tools
31. How do you configure your username and email in Git?
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
32. How do you generate and use SSH keys with Git?
Generate keys:
ssh-keygen -t rsa
Add the public key to your Git service.
33. What are Git hooks, and how do you use them?
Scripts triggered by Git events (e.g., pre-commit). Place scripts in .git/hooks.
34. How do you set up and use Git aliases?
git config --global alias.co checkout
35. What is Git stash, and how do you use it?
It temporarily saves changes:
git stash
git stash apply
Collaboration and Workflow
36. How do you contribute to open-source projects using Git?
-
Fork the repository.
-
Make changes on a branch.
-
Push and create a pull request.
37. What is a pull request, and how do you create one?
A request to merge your changes into a repository. Use GitHub or GitLab UI.
38. How do you revert a commit that has already been pushed?
git revert commit_id
39. How do you handle large files in Git repositories?
Use Git Large File Storage (Git LFS).
40. How do you enforce a specific workflow in a Git project?
Adopt workflows like Git Flow, which define branching and merging strategies.
Conclusion
Git is a versatile and powerful tool for developers. Mastering these topics ensures smooth project management and collaboration. With consistent practice, you’ll become a Git pro in no time!
Need help mastering Git? Explore more tutorials or contact us for personalized Git training!

It’s very effortless to find out any matter on net as compared
to books, as I found this post at this site.