Git Interview Questions and Answers for internship
-
What is Git?
- Answer: Git is a distributed version control system (DVCS) used for tracking changes in computer files and coordinating work on those files among multiple people. It's incredibly powerful and flexible, allowing for branching, merging, and collaborative development.
-
What is a repository?
- Answer: A repository (or repo) is a central location where all the files and their revision history are stored. It can be local (on your computer) or remote (on a server like GitHub, GitLab, or Bitbucket).
-
Explain the difference between Git and GitHub.
- Answer: Git is the version control system; GitHub is a web-based hosting service for Git repositories. Git is the underlying technology, while GitHub provides a platform for collaboration, code review, and project management.
-
What is the command to clone a Git repository?
- Answer:
git clone
- Answer:
-
What is the command to add changes to the staging area?
- Answer:
git add .
(adds all changes) orgit add
(adds specific files)
- Answer:
-
What is the staging area?
- Answer: The staging area is a temporary holding place for changes you've made to your files before committing them to the repository's history. It allows you to selectively choose which changes to include in a commit.
-
What is the command to commit changes?
- Answer:
git commit -m "Your commit message"
- Answer:
-
What is a commit message? Why is it important?
- Answer: A commit message is a short description of the changes made in a commit. It's crucial for tracking the history of the project and understanding why specific changes were made. Clear and concise messages are essential for collaboration.
-
What is the command to push changes to a remote repository?
- Answer:
git push origin
- Answer:
-
What is the command to pull changes from a remote repository?
- Answer:
git pull origin
- Answer:
-
What is a branch?
- Answer: A branch is an independent line of development. It allows you to work on new features or bug fixes without affecting the main codebase. Once complete, branches can be merged back into the main branch.
-
What is the command to create a new branch?
- Answer:
git checkout -b
- Answer:
-
What is the command to switch between branches?
- Answer:
git checkout
- Answer:
-
What is the command to merge a branch?
- Answer:
git merge
- Answer:
-
What is a merge conflict? How do you resolve it?
- Answer: A merge conflict occurs when two branches have made changes to the same lines of code. You resolve it by manually editing the conflicted files, choosing which changes to keep, and then staging and committing the resolved files.
-
What is
git rebase
?- Answer:
git rebase
rewrites the commit history by applying your commits on top of the target branch. It creates a cleaner, linear history but should be used cautiously, especially on shared branches.
- Answer:
-
What is
git stash
?- Answer:
git stash
temporarily saves your uncommitted changes, allowing you to switch branches or clean your working directory without losing your work. You can later apply the stashed changes.
- Answer:
-
What is a remote repository?
- Answer: A remote repository is a version of your project that is hosted on a server, like GitHub, GitLab, or Bitbucket. It allows for collaboration and backups.
-
What is the command to add a remote repository?
- Answer:
git remote add origin
- Answer:
-
What is
git fetch
?- Answer:
git fetch
downloads commits, files, and refs from a remote repository without merging them into your local branches.
- Answer:
-
What is
git pull
? How is it different fromgit fetch
?- Answer:
git pull
fetches changes from a remote repository and merges them into your current branch.git fetch
only downloads the changes; you need a separate merge command to integrate them.
- Answer:
-
What is a tag in Git?
- Answer: A tag is a pointer to a specific commit, often used to mark important points in a project's history, such as releases (v1.0, v2.0, etc.).
-
What is the command to create a tag?
- Answer:
git tag -a
-m "Tag message"
- Answer:
-
How do you delete a local branch?
- Answer:
git branch -d
(use-D
to force delete a branch that hasn't been merged).
- Answer:
-
How do you delete a remote branch?
- Answer:
git push origin --delete
- Answer:
-
What is
HEAD
in Git?- Answer:
HEAD
is a symbolic reference that points to the currently checked-out commit.
- Answer:
-
What is
.gitignore
?- Answer: A
.gitignore
file specifies files and directories that Git should ignore, preventing them from being tracked in the repository (e.g., temporary files, build artifacts).
- Answer: A
-
What is the difference between
git reset --hard
andgit revert
?- Answer:
git reset --hard
rewrites the project history by moving the HEAD pointer, potentially losing commits.git revert
creates a new commit that undoes the changes of a previous commit, preserving the history.
- Answer:
-
What is cherry-picking in Git?
- Answer: Cherry-picking allows you to select individual commits from one branch and apply them to another.
-
Explain the concept of a Git workflow (e.g., Gitflow).
- Answer: A Git workflow is a set of conventions and best practices for managing branches and collaborating on a project. Gitflow is a popular example that defines different branches for features, releases, and hotfixes.
-
What are some common Git aliases?
- Answer: Examples include:
git config --global alias.co checkout
,git config --global alias.st status
,git config --global alias.br branch
- Answer: Examples include:
-
How do you view the commit history?
- Answer:
git log
- Answer:
-
How do you view the differences between two commits?
- Answer:
git diff
- Answer:
-
How do you view the differences between your working directory and the staging area?
- Answer:
git diff --staged
- Answer:
-
How do you view the differences between your working directory and the last commit?
- Answer:
git diff
- Answer:
-
What is a detached HEAD state?
- Answer: A detached HEAD state occurs when your HEAD pointer is pointing to a specific commit instead of a branch. This is common when browsing history. You should create a new branch before making changes in this state.
-
How do you resolve a detached HEAD state?
- Answer: Create a new branch from the current commit using
git checkout -b
.
- Answer: Create a new branch from the current commit using
-
What is the significance of the SHA-1 hash in Git?
- Answer: The SHA-1 hash is a unique identifier for each commit. It ensures data integrity and allows for efficient referencing of commits.
-
What is a submodule in Git?
- Answer: A submodule allows you to include another Git repository as a part of your project. Changes to the submodule are managed separately.
-
What is a sub-tree in Git?
- Answer: A subtree is similar to a submodule but integrates the code of the other repository directly into your project's history.
-
What is Git LFS (Large File Storage)?
- Answer: Git LFS is an extension to Git that allows you to manage large files more efficiently by storing them outside the main repository and tracking only pointers to them.
-
Describe a situation where you used Git to resolve a conflict.
- Answer: [Provide a specific example from your experience. If you lack experience, describe a hypothetical scenario and how you would approach it.]
-
What are some best practices for using Git?
- Answer: Write clear commit messages, use branches effectively, regularly push your changes, resolve conflicts promptly, use a consistent workflow.
-
How would you handle a situation where you accidentally deleted a commit?
- Answer: If it's not pushed to the remote, I'd try `git reflog` to find the commit hash and then checkout that commit or create a new branch from it. If pushed, recovering it might require contacting collaborators or using backups depending on the remote repository setup.
-
How do you revert a merge commit?
- Answer: It's usually best to use `git revert` on the merge commit itself. This creates a new commit that undoes the changes introduced by the merge.
-
What are some common Git GUI tools?
- Answer: SourceTree, GitKraken, GitHub Desktop, TortoiseGit
-
How would you explain Git to someone with no programming experience?
- Answer: I would explain it as a tool that tracks changes to a document, allowing you to revert to previous versions, see who made changes, and collaborate easily with others on the same document.
-
What is the importance of a consistent branching strategy?
- Answer: A consistent branching strategy ensures a clean and understandable project history, simplifies collaboration, and helps avoid merge conflicts.
-
How familiar are you with Git hooks?
- Answer: [Answer honestly about your familiarity, providing specific examples if you have experience.]
-
Explain the concept of a Git bisect.
- Answer: Git bisect is a tool used to find the commit that introduced a bug. It works by repeatedly dividing the commit history in half, allowing you to quickly narrow down the source of the issue.
-
What's the difference between a hard reset and a soft reset?
- Answer: A hard reset discards all changes in the working directory and staging area. A soft reset only moves the HEAD pointer, leaving changes in the working directory and staging area.
-
How do you find the differences between the current branch and another branch?
- Answer: `git diff
`
- Answer: `git diff
-
How do you undo your last commit?
- Answer: `git reset --soft HEAD~1` (if not pushed), or `git revert HEAD` (if pushed).
-
What does `git clean` do?
- Answer: `git clean` removes untracked files from your working directory. Use with caution, as it permanently deletes files.
-
What is the purpose of a `.git` folder?
- Answer: The `.git` folder contains all the metadata for the Git repository, including the commit history, objects, refs, etc. It should not be modified directly.
-
How do you resolve merge conflicts using a merge tool?
- Answer: Git supports various merge tools (like Meld, KDiff3). You would configure Git to use your preferred tool and then run `git mergetool` to launch the tool to resolve the conflicts visually.
-
What is the command to show the changes made in a specific commit?
- Answer: `git show
`
- Answer: `git show
-
How can you find all commits that changed a specific file?
- Answer: `git log --follow --
`
- Answer: `git log --follow --
-
How familiar are you with using Git with different IDEs? (e.g., VS Code, IntelliJ)?
- Answer: [Answer honestly, detailing your experience with any relevant IDEs.]
-
What is the role of the `origin` remote?
- Answer: `origin` is the default name for the remote repository you clone from. It's the remote repository you push and pull changes to/from.
-
Explain how you would contribute to an open-source project using Git.
- Answer: I would fork the repository, create a new branch for my changes, make my modifications, commit and push my changes to my fork, then create a pull request for the maintainer to review and merge my contribution.
-
What is the difference between a local and a remote branch?
- Answer: A local branch exists only on your computer, while a remote branch is a branch that is tracked on a remote repository like GitHub.
-
How do you rename a branch in Git?
- Answer: `git branch -m
`
- Answer: `git branch -m
-
What is the command to see the current branch?
- Answer: `git branch` or `git status`
-
What are some ways to improve the readability of your Git commit history?
- Answer: Use descriptive commit messages, squash related commits, rebase for a linear history (carefully!), use consistent message formatting.
Thank you for reading our blog post on 'Git Interview Questions and Answers for internship'.We hope you found it informative and useful.Stay tuned for more insightful content!