Git & GitHub Interview Questions
1. What is Git, and how is it different from GitHub?
Answer: Git is a version control system that helps you track changes in your code. It lets you go back in time, see what changes were made, and even roll them back if necessary. GitHub, on the other hand, is a platform that hosts Git repositories in the cloud, allowing you to collaborate with others, manage projects, and share code.
2. Explain the purpose of git init
.
Answer: git init
is the command used to create a new Git repository. It initializes a new Git project in your folder, enabling you to start tracking changes in your code.
3. What is a commit in Git?
Answer: A commit is like taking a snapshot of your project at a specific point in time. Each commit saves your changes and provides a unique ID (hash) to reference later.
4. What does git clone
do?
Answer: git clone
creates a local copy of a remote repository (like one from GitHub) on your machine. This allows you to work on the project offline, make changes, and later push those changes back to the remote repository.
5. How do branches work in Git?
Answer: Branches allow you to work on different parts of your project without affecting the main codebase. For example, you can create a new feature or fix bugs on separate branches, and then merge them into the main branch when they’re ready.
6. What is the difference between git merge
and git rebase
?
Answer: Both git merge
and git rebase
combine changes from one branch into another. However, git merge
creates a new commit that brings in the changes, while git rebase
rewrites the commit history by applying changes from one branch on top of another in a linear sequence. Merging is great for preserving history, while rebasing keeps things clean.
7. What is a pull request in GitHub, and why is it important?
Answer: A pull request is a way of proposing changes to a repository. It lets other contributors review, discuss, and approve your code before it’s merged into the main project. This helps keep the codebase clean and ensures that every change is reviewed for quality.
8. How do you resolve conflicts in Git?
Answer: A conflict occurs when Git cannot automatically merge changes between branches. To resolve it, you need to manually edit the files where the conflict occurred, choose which version of the code to keep, and then commit the resolved changes.
9. What does git stash
do?
Answer: git stash
temporarily saves your changes without committing them, allowing you to switch to another branch or task without losing your work. You can apply the stashed changes later using git stash apply
.
10. What is the use of .gitignore
?
Answer: The .gitignore
file tells Git which files or directories it should ignore and not track. This is useful for excluding things like log files, temporary files, or dependencies that don't need to be versioned.
11. How can you undo the last commit in Git?
Answer: To undo the last commit, you can use git reset --soft HEAD~1
(if you want to keep the changes in your working directory) or git reset --hard HEAD~1
(if you want to discard the changes entirely).
12. What does forking a repository mean in GitHub?
Answer: Forking a repository creates a personal copy of someone else's repository under your GitHub account. It allows you to experiment, make changes, or contribute to the original project by sending pull requests.
13. What is git pull
?
Answer: git pull
is a command that fetches the latest changes from a remote repository and merges them into your current branch. It’s a combination of git fetch
(to get the updates) and git merge
(to apply the updates).
14. What is Continuous Integration (CI), and how is it related to GitHub?
Answer: Continuous Integration (CI) is a practice where code changes are automatically tested and integrated into the main branch frequently. GitHub supports CI pipelines (like GitHub Actions) that automatically run tests on your code whenever new commits or pull requests are made, ensuring your code is always in a working state.
15. What is the difference between git fetch
and git pull
?
Answer: git fetch
retrieves the latest changes from a remote repository without merging them into your current branch, allowing you to inspect the changes first. git pull
fetches and merges the changes in one step.
16. What is the difference between git reset
and git revert
?
Answer: git reset
changes the current branch’s history by moving the HEAD pointer to a previous commit, potentially discarding changes. It’s destructive for history. On the other hand, git revert
creates a new commit that undoes the changes made by a specific previous commit, preserving the history.
17. What is the HEAD in Git?
Answer: The HEAD is a pointer that refers to the current commit or the latest commit in your current branch. When you switch branches or make new commits, the HEAD moves to the latest commit in the branch you’re working on.
18. Can you explain the difference between git diff
and git status
?
Answer: git status
shows the state of your working directory and staging area—whether files have been modified, staged, or are untracked. git diff
shows the actual differences between files in your working directory and the last commit, or between staged changes and the last commit.
19. What is a fast-forward merge in Git?
Answer: A fast-forward merge happens when the branch being merged in doesn’t have any divergent changes, and the commit history can be “fast-forwarded” to the new branch’s tip without creating a merge commit. In other words, Git simply moves the pointer ahead to the newer commits, without introducing a new merge commit.
20. How do you revert a file to a previous version in Git?
Answer: You can revert a file to a previous version using the git checkout
command with the commit hash and the file name, like this: git checkout <commit_hash> -- <file_path>
. Afterward, you need to commit the reverted version.
21. What are submodules in Git?
Answer: Git submodules are repositories inside another Git repository. They allow you to include external projects as part of your repository, but manage their versions separately. It’s useful for managing dependencies while keeping the main project and subproject histories independent.
22. What is a git cherry-pick
, and when would you use it?
Answer: git cherry-pick
is a command used to apply a specific commit from one branch to another. It’s useful when you want to bring in just one or a few specific changes from another branch without merging the entire branch.
23. How do you rename a branch in Git?
Answer: To rename a branch, you can use the following commands:
If you're on the branch you want to rename:
git branch -m new-branch-name
If you're renaming a branch you're not currently on:
git branch -m old-branch-name new-branch-name
Don't forget to push the renamed branch and delete the old branch from the remote usinggit push origin --delete old-branch-name
.
24. What is Git Flow, and how does it structure development?
Answer: Git Flow is a branching model that defines a clear workflow for working with Git. It involves two main branches (master
for production-ready code and develop
for ongoing development) and several supporting branches (feature
, release
, and hotfix
) for managing features, releases, and urgent fixes. Git Flow is ideal for large teams and structured projects.
25. What does the git tag
command do?
Answer: git tag
is used to create a tag that points to a specific commit. Tags are typically used to mark specific points in the repository's history, such as releases (e.g., v1.0.0
). There are two types of tags: lightweight (just a pointer) and annotated (a full object in Git’s database, which can include a message, author, etc.).
26. What does git log
do, and how can you customize its output?
Answer: git log
displays the commit history of a repository, showing a list of commits, along with details such as commit hashes, authors, dates, and commit messages. You can customize its output with options like:
git log --oneline
to see a simplified one-line view of each commit.git log --graph
to show a graphical representation of the branch history.git log --since="2 weeks ago"
to filter commits by time.
27. What is the purpose of git blame
?
Answer: git blame
shows who last modified each line of a file, which can help identify when and by whom a change was introduced. It’s helpful for tracking down the cause of bugs or understanding the history of specific lines of code.
28. How does Git handle large files or binary files?
Answer: Git isn’t optimized for large or binary files since it tracks changes at the line level (text files). For large files, it’s recommended to use Git LFS (Large File Storage), which handles large files by storing pointers in the repository and keeping the actual file in a separate location.
29. Explain the concept of 'detached HEAD' in Git.
Answer: A detached HEAD occurs when Git’s HEAD is pointing to a specific commit rather than a branch. This can happen if you checkout a commit directly (instead of a branch). Any new commits made in this state will not be associated with any branch unless you explicitly create a branch to track them.
30. How do you delete a branch in Git locally and remotely?
Answer: To delete a branch locally, use the command: git branch -d branch_name
. If the branch hasn’t been merged, use the -D
flag to force delete. To delete a branch from a remote repository, use: git push origin --delete branch_name
.
31. What is the git reflog
, and when would you use it?
Answer: git reflog
records every move of the HEAD in your repository, even those that are not reflected in the commit history (like resets or branch switches). It’s a useful command to recover lost commits after a git reset
or any other destructive operation.
32. What is squashing in Git, and how do you squash commits?
Answer: Squashing in Git combines multiple commits into one, often used to clean up messy commit histories before merging a branch. You can squash commits using an interactive rebase: git rebase -i <commit_hash>
, then mark the commits you want to squash with s
(for "squash").
33. What are hooks in Git, and how are they used?
Answer: Git hooks are scripts that run automatically at different stages of the Git workflow (e.g., before or after commits, merges, pushes). They can be used to enforce coding standards, run tests, or automate tasks. Hooks live in the .git/hooks/
directory and can be customized per project.
34. What is a Git rebase, and when would you choose to rebase instead of merging?
Answer: Git rebase moves the commits of one branch on top of another branch, creating a linear history. It’s often used to avoid merge commits and keep the commit history cleaner. Rebasing is great for feature branches before merging them into the main branch, but avoid it on public branches since it rewrites history.
35. How do you undo a git rebase
if things go wrong?
Answer: If a rebase goes wrong, you can use git rebase --abort
to stop the rebase and return to the state before it started. If you’ve already completed the rebase and want to undo it, you can use git reflog
to find the previous commit and use git reset
to return to it.