Git Interview Questions and Answers
-
What is Git?
- Answer: Git is a distributed version control system (DVCS) used to track changes in source code during software development. It allows multiple developers to collaborate on a project, manage different versions of the code, and revert to previous states if necessary.
-
What is a repository in Git?
- Answer: A repository (or repo) is a directory containing all the files and directories for a particular project, along with the Git metadata (information about the history of changes).
-
Explain the difference between Git and GitHub.
- Answer: Git is a version control system; GitHub is a web-based hosting service for Git repositories. Git is the tool, and GitHub is a platform that provides additional features like collaboration tools, issue tracking, and pull requests.
-
What is the command to initialize a new Git repository?
- Answer:
git init
- Answer:
-
How do you stage changes in Git?
- Answer: Use the command
git add
orgit add .
(to stage all changes in the current directory and its subdirectories).
- Answer: Use the command
-
What is the command to commit changes in Git?
- Answer:
git commit -m "Your commit message"
- Answer:
-
What is the difference between
git commit
andgit push
?- Answer:
git commit
saves changes locally to your repository.git push
uploads your local commits to a remote repository (like GitHub).
- Answer:
-
How do you clone a Git repository?
- Answer:
git clone
- Answer:
-
What is a branch in Git?
- 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.
-
How do you create a new branch in Git?
- Answer:
git checkout -b
- Answer:
-
How do you switch between branches in Git?
- Answer:
git checkout
- Answer:
-
How do you merge a branch into another branch in Git?
- Answer: Checkout the target branch, then
git merge
- Answer: Checkout the target branch, then
-
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 editing the conflicted files, marking the changes you want to keep, and then staging and committing the resolved files.
-
What is
git rebase
?- Answer:
git rebase
rewrites the project history by moving a branch's commits onto another branch. It creates a cleaner, linear history but should be used cautiously, especially on shared branches.
- Answer:
-
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.
-
What is the command to add a remote repository?
- Answer:
git remote add origin
- Answer:
-
What is the command to fetch changes from a remote repository?
- Answer:
git fetch origin
- Answer:
-
What is the difference between
git fetch
andgit pull
?- Answer:
git fetch
downloads changes from the remote repository without merging them into your local branches.git pull
fetches changes and merges them immediately.
- Answer:
-
What is a tag in Git?
- Answer: A tag is a pointer to a specific commit, often used to mark important points in the project's history, such as releases.
-
How do you create a tag in Git?
- Answer:
git tag -a
-m "Your tag message"
- Answer:
-
How do you delete a local branch in Git?
- Answer:
git branch -d
(use-D
to force delete a branch that hasn't been merged)
- Answer:
-
How do you delete a remote branch in Git?
- Answer:
git push origin --delete
- Answer:
-
What is
.gitignore
?- Answer: A
.gitignore
file specifies files and directories that Git should ignore and not track.
- Answer: A
-
What is HEAD in Git?
- Answer: HEAD is a pointer to the currently checked-out commit.
-
What is the command to view the commit history?
- Answer:
git log
- Answer:
-
What is the command to undo the last commit?
- Answer:
git reset --soft HEAD^
(orgit reset --hard HEAD^
, but use with caution)
- Answer:
-
What is a stash in Git?
- Answer: A stash is a way to temporarily save changes without committing them. Useful when you need to switch branches or clean your working directory.
-
How do you stash changes in Git?
- Answer:
git stash push
- Answer:
-
How do you apply a stashed change in Git?
- Answer:
git stash pop
- Answer:
-
What is cherry-picking in Git?
- Answer: Cherry-picking allows you to select individual commits from one branch and apply them to another.
-
How do you cherry-pick a commit in Git?
- Answer:
git cherry-pick
- Answer:
-
What is a Git hook?
- Answer: Git hooks are scripts that run automatically before or after events like commits, pushes, or merges.
-
What is Git bisect?
- Answer: Git bisect is a tool to help find the commit that introduced a bug.
-
Explain Git's staging area.
- Answer: The staging area is a temporary holding area for changes before they are committed. It allows you to selectively choose which changes to include in a commit.
-
What is a detached HEAD state in Git?
- Answer: A detached HEAD state occurs when you're checking out a commit that isn't part of any branch. You should create a new branch if you want to make changes.
-
How do you resolve a detached HEAD state?
- Answer: Create a new branch from the detached HEAD commit using
git checkout -b
- Answer: Create a new branch from the detached HEAD commit using
-
What is the difference between `git rm` and `rm`?
- Answer: `rm` is a standard Unix command that deletes a file from your filesystem. `git rm` removes a file from both your filesystem and the Git repository.
-
How do you view the differences between two commits?
- Answer:
git diff
- Answer:
-
How do you view the differences between the working directory and the staging area?
- Answer:
git diff --staged
- Answer:
-
How do you view the differences between the working directory and the last commit?
- Answer:
git diff
- Answer:
-
What is the purpose of `git reflog`?
- Answer: `git reflog` shows the history of changes to the tip of branches and HEAD. It is useful for recovering lost commits.
-
What is a Git submodule?
- Answer: A Git submodule is a way to include another Git repository as a subdirectory within your main project.
-
What is a Git subtree?
- Answer: A Git subtree is an alternative to submodules, offering a more integrated approach where the subproject's history is merged into the main project's history.
-
Explain the concept of a working tree in Git.
- Answer: The working tree is the copy of the project files you're currently working on. Changes made here are not yet tracked by Git until staged and committed.
-
What is the command to revert a commit?
- Answer:
git revert
- Answer:
-
What is the difference between `git revert` and `git reset`?
- Answer: `git revert` creates a new commit that undoes the changes of a previous commit, preserving the project history. `git reset` moves the branch pointer, potentially losing commits.
-
What is Git's object database?
- Answer: Git stores all its data in an object database, which consists of various types of objects (blobs, trees, commits, tags) that are uniquely identified by their SHA-1 hashes.
-
Explain the different types of Git objects.
- Answer: Blobs store file data, trees represent the directory structure, commits store changes and metadata, and tags point to specific commits.
-
How does Git handle large files efficiently?
- Answer: Git LFS (Large File Storage) is designed to handle large files more efficiently by storing them outside the main Git repository and only storing pointers within the repo.
-
What are some common Git workflows?
- Answer: Gitflow, GitHub Flow, GitLab Flow, and feature branching are common workflows.
-
Explain the Gitflow workflow.
- Answer: Gitflow is a branching model that uses separate branches for development, features, releases, and hotfixes, ensuring a structured and organized development process.
-
Explain the GitHub Flow workflow.
- Answer: GitHub Flow emphasizes simplicity, using only the `main` branch and feature branches that are frequently merged. Pull requests are central to code review and collaboration.
-
How can you protect your branches in Git?
- Answer: On platforms like GitHub and GitLab, you can configure branch protection rules to prevent direct pushes to certain branches, requiring pull requests and code reviews.
-
What are some best practices for using Git?
- Answer: Write clear and concise commit messages, use feature branches, regularly push your changes, use pull requests for code review, and follow a consistent workflow.
-
How can you find a specific commit in Git?
- Answer: Use `git log` with various options to filter by author, date, message, or use `git rev-list` for more advanced searching. You can also search by parts of the commit message.
-
How do you rename a branch in Git?
- Answer: Checkout the branch, then use `git branch -m
`. For remote branch rename, you also need `git push origin --delete ` and `git push origin `
- Answer: Checkout the branch, then use `git branch -m
-
What is the significance of the `.git` folder?
- Answer: The `.git` folder is the hidden folder in a Git repository that contains all the repository's metadata, including the object database, branches, and other internal data structures.
-
How to undo changes in the working directory?
- Answer: Use `git checkout --
` to discard changes to a specific file, or `git reset --hard HEAD` to discard all changes in the working directory (use with extreme caution).
- Answer: Use `git checkout --
-
What is the difference between a shallow clone and a full clone?
- Answer: A shallow clone downloads only a portion of the repository's history, saving time and disk space. A full clone downloads the entire history.
-
How to create a shallow clone?
- Answer: Use `git clone --depth=
` to specify the number of commits to clone.
- Answer: Use `git clone --depth=
-
What is a force push and when should it be avoided?
- Answer: A force push (`git push --force`) overwrites the remote branch with your local branch. It should be avoided in collaborative environments as it can cause data loss for other developers.
-
What is `git clean`?
- Answer: `git clean` removes untracked files from your working directory. Use with caution as it permanently deletes files.
-
Explain Git's concept of "porcelain" and "plumbing".
- Answer: Porcelain refers to Git's high-level commands designed for ease of use (like `git commit`). Plumbing refers to its low-level commands for more advanced manipulation (like `git hash-object`).
-
How to find the author of a specific commit?
- Answer: Use `git log --pretty=format:"%an"
` to show the author name.
- Answer: Use `git log --pretty=format:"%an"
-
How to see which branches are merged into a particular branch?
- Answer: Use `git log --graph --oneline --decorate --all` to visualize the branch history and see which branches have merged into the one you are interested in.
-
How does Git handle binary files?
- Answer: Git can handle binary files, but it's less efficient for managing large binary files due to the way it stores changes as diffs. Git LFS is recommended for large binaries.
Thank you for reading our blog post on 'Git Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!