♠ Interview Questions
- 1. What is Git and What are the advantages of using Git?
-
Answer: Git is a distributed version control system (VCS) that helps developers track changes in source code and collaborate effectively.
Advantages of using Git:
- Tracks every change with detailed history.
- Supports branching and merging for parallel development.
- Works offline (distributed).
- Fast and efficient performance.
- Allows easy collaboration via platforms like GitHub or GitLab.
- 2. What is the difference between Git and GitHub?
-
Answer: Git is a local version control system, while GitHub is a cloud-based platform for hosting Git repositories.
Key Differences:
- Git: Tracks changes in code locally on your computer.
- GitHub: Allows you to share code online, collaborate, and manage repositories.
- 3. How do you create a new branch in Git?
-
Answer: You can create and switch to a new branch using the command below:
git checkout -b [new-branch-name]
Or, you can create and switch in two steps:
git branch [new-branch-name]
git checkout [new-branch-name]
- 4. What is a merge conflict and how do you resolve it?
-
Answer: A merge conflict occurs when Git cannot automatically merge changes because the same part of a file was modified differently in two branches.
To resolve it:
- Git highlights the conflict markers in the file.
- You manually edit the file to choose or combine changes.
- Then, stage and commit the resolved file.
git add [filename]
git commit
- 5. Explain the purpose of the staging area in Git.
-
Answer: The staging area in Git is a place where changes are prepared before committing. It allows you to review and control exactly what will go into your next commit.
- 6. How do you revert a commit in Git?
-
Answer: You can undo changes from a specific commit using:
git revert <commit-hash>
// creates a new commit that reverses the changes.
To completely remove a commit (use with caution):
git reset --hard HEAD~1
- 7. What is the purpose of the
.gitignore
file? -
Answer: The .gitignore file tells Git which files or directories to ignore. It helps keep unnecessary, temporary, or sensitive files out of your repository.
Common examples:
node_modules/
.env
dist/
- 8. How can you view the commit history in Git?
-
Answer: You can use the following commands to view commit history:
git log
// shows detailed commit information.git log --oneline
// displays a compact view with one line per commit.git log --graph
// shows a visual representation of the branch structure.git log --oneline --graph
// combines both compact view and graph for a clear summary.