Git Interview Questions and Answers for experienced
-
What is Git?
- Answer: Git is a distributed version control system (DVCS) that tracks changes in files and allows you to revert to previous versions. It's designed for speed, efficiency, and data integrity. Unlike centralized VCSs, every developer has a complete copy of the repository, including its history.
-
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 features.
-
What are the three main states of a Git file?
- Answer: Modified, Staged, and Committed. Modified means the file has been changed but not yet tracked by Git. Staged means the changes are ready to be committed. Committed means the changes are permanently saved in the repository's history.
-
Explain the difference between `git add` and `git commit`.
- Answer: `git add` stages changes in the working directory, preparing them for the next commit. `git commit` saves the staged changes to the local repository's history, creating a new snapshot.
-
What is a Git branch?
- Answer: A branch is an independent line of development. It allows developers to work on new features or bug fixes without affecting the main codebase (usually the `main` or `master` branch). Branches can be merged back into the main branch once completed.
-
How do you create a new branch in Git?
- Answer: `git checkout -b
`
- Answer: `git checkout -b
-
How do you switch between branches?
- Answer: `git checkout
`
- Answer: `git checkout
-
How do you merge a branch into another branch?
- Answer: Checkout the target branch, then `git merge
`. Resolve any merge conflicts that arise.
- Answer: Checkout the target branch, then `git merge
-
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 the same file. Git will mark the conflict in the file. You must manually edit the file, choosing which changes to keep, and then stage and commit the resolved file.
-
What is `git rebase`? How does it differ from `git merge`?
- Answer: `git rebase` rewrites the commit history by moving a branch's commits onto another branch, creating a linear history. `git merge` preserves the branch history, resulting in a merge commit. Rebase is generally preferred for cleaner history, but should be avoided on publicly shared branches.
-
What is a remote repository?
- Answer: A remote repository is a version of your project that is hosted on a server, such as GitHub, GitLab, or Bitbucket. It allows collaboration with other developers.
-
How do you clone a remote repository?
- Answer: `git clone
`
- Answer: `git clone
-
How do you push your local changes to a remote repository?
- Answer: `git push
`
- Answer: `git push
-
How do you pull changes from a remote repository?
- Answer: `git pull
`
- Answer: `git pull
-
What is `git fetch`?
- Answer: `git fetch` downloads the changes from a remote repository without merging them into your local branch. This allows you to see what changes have been made before merging them.
-
What is a Git tag?
- Answer: A tag is a pointer to a specific commit, usually used to mark important points in the project's history, such as releases (e.g., v1.0.0).
-
How do you create a tag?
- Answer: `git tag
` (lightweight tag) or `git tag -a -m "message"` (annotated tag)
- Answer: `git tag
-
How do you delete a local branch?
- Answer: `git branch -d
` (use `-D` to force delete a branch that hasn't been merged)
- Answer: `git branch -d
-
How do you delete a remote branch?
- Answer: `git push origin --delete
` or `git push origin : `
- Answer: `git push origin --delete
-
What is `.gitignore`?
- Answer: A `.gitignore` file specifies files and directories that should be excluded from version control. This is useful for ignoring temporary files, build artifacts, and other files that shouldn't be tracked.
-
Explain the concept of a Git stash.
- Answer: A stash temporarily saves uncommitted changes so you can switch branches or clean up your working directory without losing your work. You can later reapply the stashed changes.
-
How do you stash your changes?
- Answer: `git stash`
-
How do you apply a stashed change?
- Answer: `git stash pop` (applies and removes from stash) or `git stash apply` (applies without removing)
-
What is `git cherry-pick`?
- Answer: `git cherry-pick` applies specific commits from one branch to another. This is useful for applying a bug fix from one branch to another without merging the entire branch.
-
What is a Git hook?
- Answer: Git hooks are scripts that run automatically before or after Git events (e.g., commit, push). They can be used to automate tasks like code style checks, testing, or deployment.
-
What is `git bisect`?
- Answer: `git bisect` is a tool to help find the commit that introduced a bug. It uses a binary search algorithm to quickly identify the problematic commit.
-
What is `git reflog`?
- Answer: `git reflog` shows the history of your local repository's head pointer, which can help recover lost commits.
-
How do you view the commit history?
- Answer: `git log` (various options like `--oneline`, `--graph`, `--decorate` are available for customization)
-
What is a detached HEAD state?
- Answer: A detached HEAD state occurs when you checkout a commit that isn't on a branch. You're working directly on a specific commit. Changes should be branched or committed before switching back to a branch.
-
How do you resolve a detached HEAD state?
- Answer: Create a new branch from the detached HEAD commit using `git checkout -b
` or commit your changes and switch back to a normal branch.
- Answer: Create a new branch from the detached HEAD commit using `git checkout -b
-
Explain the difference between `git pull --rebase` and `git pull`.
- Answer: `git pull` performs a merge, while `git pull --rebase` performs a rebase. The latter integrates changes by re-writing the commit history, making it cleaner.
-
What are some best practices for using Git?
- Answer: Write clear and concise commit messages, use branches effectively, frequently commit small changes, review your code before pushing, use pull requests for collaboration, and keep your local repository clean.
-
How can you revert a commit?
- Answer: Use `git revert
` to create a new commit that undoes the changes of a previous commit. `git reset --hard ` will rewrite the history (use with caution!).
- Answer: Use `git revert
-
What is the difference between `git reset --soft`, `git reset --mixed`, and `git reset --hard`?
- Answer: `--soft` keeps changes in the staging area, `--mixed` (default) keeps changes in the working directory, and `--hard` discards changes entirely. Use with extreme caution, especially `--hard`.
-
How do you view the differences between two commits?
- Answer: `git diff
`
- Answer: `git diff
-
How do you view the differences between the working directory and the staging area?
- Answer: `git diff --staged`
-
How do you view the differences between the working directory and the last commit?
- Answer: `git diff`
-
What is a Git submodule?
- Answer: A Git submodule allows you to include another Git repository as a part of your project. Changes to the submodule are managed separately.
-
What is a Git subtree?
- Answer: A Git subtree is an alternative to submodules. It merges the code of another repository directly into your project's history, making it simpler to manage than submodules, but potentially causing conflicts.
-
How do you handle large files in Git?
- Answer: Use Git Large File Storage (LFS) to store large binary files outside of the main repository, while keeping track of their versions within Git.
-
Explain the concept of a Git workflow (e.g., Gitflow).
- Answer: A Git workflow is a set of guidelines for using Git to manage your project's development. Gitflow is a popular workflow that uses different branches for features, releases, and hotfixes.
-
What is a fork in GitHub?
- Answer: A fork is a personal copy of a remote repository. It allows you to make changes without affecting the original repository. Pull requests can be used to contribute changes back to the original repository.
-
What is a pull request (or merge request)?
- Answer: A pull request is a request to merge changes from one branch (typically a feature branch) into another branch (typically the main branch).
-
How do you create a pull request on GitHub?
- Answer: On GitHub, navigate to your forked repository, create a new branch for your changes, push the branch, and then create a pull request from that branch to the upstream repository.
-
What is the significance of the `.git` directory?
- Answer: The `.git` directory contains all of the Git metadata for the repository, including the repository's history, branches, and other internal data. It should never be modified directly.
-
Describe the concept of a Git repository.
- Answer: A Git repository is a directory that contains all of the files and metadata for a project, including the entire history of changes.
-
What are some common Git commands you use daily?
- Answer: `git status`, `git add`, `git commit`, `git push`, `git pull`, `git checkout`, `git log`.
-
How do you find the commit hash for a specific commit?
- Answer: Use `git log` to view the commit history and identify the hash, or use other Git commands that output the commit hash as part of their output.
-
How do you undo the last commit?
- Answer: `git reset HEAD^` (or `git reset --soft HEAD^` to keep changes staged). Use caution.
-
How do you view the configuration of your Git installation?
- Answer: `git config --list`
-
How do you set your username and email in Git?
- Answer: `git config --global user.name "Your Name"` and `git config --global user.email "your.email@example.com"`
-
Explain the difference between a local and a remote branch.
- Answer: A local branch exists only on your computer, while a remote branch exists on a remote repository (e.g., on GitHub).
-
How do you rename a branch?
- Answer: `git branch -m
`
- Answer: `git branch -m
-
What is the role of the HEAD pointer in Git?
- Answer: The HEAD pointer always points to the currently checked out branch or commit.
-
How do you create a bare Git repository?
- Answer: `git init --bare
.git`
- Answer: `git init --bare
-
What are the benefits of using a bare Git repository?
- Answer: Bare repositories are typically used as central repositories, ideal for server-side deployments, and are generally not intended for direct development work since they don't have a working directory.
-
How do you create a patch file in Git?
- Answer: `git diff > patch_file.patch`
-
How do you apply a patch file in Git?
- Answer: `git apply patch_file.patch` (or `patch -p1 < patch_file.patch`)
-
Explain Git's object model.
- Answer: Git's object model is based on four main object types: blobs (files), trees (directories), commits (snapshots), and tags (pointers to commits). These objects are content-addressed, meaning their hash is determined by the content, ensuring data integrity.
-
What is a Git packfile?
- Answer: Git packfiles are files that store multiple Git objects efficiently, reducing disk space and improving performance.
-
How would you handle a situation where a large number of files have been accidentally added to the Git repository?
- Answer: Use `.gitignore` to prevent future additions and possibly use `git reset` or `git filter-branch` to remove them from the history. The latter is very resource-intensive and should be considered carefully and tested thoroughly before application.
-
How do you find all commits that modified a specific file?
- Answer: `git log --follow --
`
- Answer: `git log --follow --
-
Explain the concept of a shallow clone.
- Answer: A shallow clone downloads only a portion of the repository's history, reducing download time and disk space. Useful for large repositories where only the recent history is needed.
-
What is the significance of the `--force` flag in Git commands (e.g., `git push --force`)?
- Answer: The `--force` flag overwrites the remote branch with your local branch, potentially losing other developers' work. It should be used with extreme caution and only when absolutely necessary.
-
How to revert a merge commit?
- Answer: The best approach is to `git revert
`. This creates a new commit that undoes the changes introduced by the merge.
- Answer: The best approach is to `git revert
-
Explain how Git handles symbolic references.
- Answer: Symbolic references (like branches) act as pointers to other references (like commits). They provide a human-readable way to refer to a specific commit.
Thank you for reading our blog post on 'Git Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!