Git Interview Questions and Answers for freshers
-
What is Git?
- Answer: Git is a distributed version control system (DVCS) used to track changes in source code and other files during software development. It allows multiple developers to collaborate on a project efficiently, managing different versions and merging their contributions.
-
What is a repository in Git?
- Answer: A repository (or repo) is a directory containing all the project files, along with the Git metadata that tracks changes to those files over time. It's essentially a database for your project's history.
-
What is the difference between Git and GitHub?
- Answer: Git is a version control system, a command-line tool. GitHub is a web-based platform that provides hosting for Git repositories, along with additional features like issue tracking, collaboration tools, and wikis.
-
Explain the basic Git workflow.
- Answer: The basic workflow typically involves cloning a repository, making changes in your local working directory, staging changes using `git add`, committing changes using `git commit`, and pushing changes to a remote repository using `git push`.
-
What is the command to clone a Git repository?
- Answer: `git clone
`
- Answer: `git clone
-
What does `git add` do?
- Answer: `git add` stages changes in your working directory. Staged changes are those that will be included in the next commit.
-
What does `git commit` do?
- Answer: `git commit` saves a snapshot of your staged changes along with a commit message describing the changes.
-
What does `git push` do?
- Answer: `git push` uploads your local commits to a remote repository.
-
What does `git pull` do?
- Answer: `git pull` downloads commits from a remote repository and merges them into your local branch.
-
What is a branch in Git?
- 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.
-
How do you create a new branch?
- 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?
- Answer: Checkout the target branch (e.g., `main`) and then use `git merge
`
- Answer: Checkout the target branch (e.g., `main`) and then use `git merge
-
What is a merge conflict?
- Answer: A merge conflict occurs when two branches have made changes to the same lines of code. Git cannot automatically merge these changes, requiring manual resolution.
-
How do you resolve a merge conflict?
- Answer: Manually edit the conflicting files, resolving the differences. Then, stage the changes using `git add` and commit the resolution.
-
What is `git rebase`?
- Answer: `git rebase` rewrites the commit history by moving a branch onto another branch's tip. It results in a cleaner, linear history but should be used cautiously.
-
What is the difference between `git merge` and `git rebase`?
- Answer: `git merge` preserves the original branch history, creating a merge commit. `git rebase` rewrites the history, making it appear as a linear sequence of commits.
-
What is `git stash`?
- Answer: `git stash` temporarily saves uncommitted changes, allowing you to switch branches or clean your working directory without losing your work. You can later apply the stashed changes.
-
What is a remote repository?
- Answer: A remote repository is a version of your repository hosted on a server, like GitHub, GitLab, or Bitbucket. It allows collaboration among multiple developers.
-
What is `git remote add`?
- Answer: `git remote add
` adds a new remote repository to your local repository.
- Answer: `git remote add
-
What is `git fetch`?
- Answer: `git fetch` downloads commits, files, and refs from a remote repository without merging them into your local branches.
-
What is HEAD in Git?
- Answer: HEAD refers to the currently checked-out branch.
-
What is a tag in Git?
- Answer: A tag is a pointer to a specific commit, usually used to mark important points in the project's history, such as releases.
-
How do you create a tag?
- Answer: `git tag
` or `git tag -a -m "message"` for annotated tags
- Answer: `git tag
-
How do you delete a local branch?
- Answer: `git branch -d
`
- Answer: `git branch -d
-
How do you delete a remote branch?
- Answer: `git push origin --delete
`
- Answer: `git push origin --delete
-
What is `git log`?
- Answer: `git log` displays the commit history.
-
What is `git status`?
- Answer: `git status` shows the state of your working directory and staging area.
-
What is `git diff`?
- Answer: `git diff` shows the differences between files or commits.
-
What is `.gitignore`?
- Answer: A `.gitignore` file specifies files and directories that should be ignored by Git, preventing them from being tracked in the repository.
-
What is Git cherry-pick?
- Answer: `git cherry-pick` applies a specific commit from one branch to another.
-
What is a detached HEAD state?
- Answer: A detached HEAD state occurs when you're not on a branch, but instead directly on a specific commit. Changes made in this state will not be saved unless you create a new branch.
-
How do you resolve a detached HEAD state?
- Answer: Either create a new branch from the current commit or checkout an existing branch.
-
Explain Git's staging area.
- Answer: The staging area is a temporary holding area for changes that you want to include in your next commit. It acts as a buffer between your working directory and the repository's history.
-
What is a SHA-1 hash in Git?
- Answer: A SHA-1 hash is a unique identifier for each commit in a Git repository. It's a 40-character hexadecimal string.
-
What is the command to view the SHA-1 hash of the last commit?
- Answer: `git log -1 --pretty=format:"%H"`
-
What is a lightweight tag vs. an annotated tag?
- Answer: A lightweight tag is simply a pointer to a commit. An annotated tag stores more metadata, including tagger information and a message.
-
How to undo a commit?
- Answer: Several methods exist, including `git reset`, `git revert`, and interactive rebasing. `git reset` rewrites history (use cautiously), `git revert` creates a new commit undoing the changes, and interactive rebase allows for selective undo.
-
What is `git reflog`?
- Answer: `git reflog` shows a log of changes to the HEAD pointer and other references. It can be useful for recovering lost commits.
-
What is the difference between `git rm` and `rm`?
- Answer: `rm` is a standard shell command that deletes files. `git rm` removes files from both the working directory and the Git staging area, marking them for deletion in the next commit.
-
How to move a file using Git?
- Answer: Use `git mv
`
- Answer: Use `git mv
-
What is Git bisect?
- Answer: Git bisect is a tool for finding the commit that introduced a bug. It uses a binary search algorithm to quickly pinpoint the problematic commit.
-
What is a Git hook?
- Answer: Git hooks are scripts that run before or after Git events, such as commit, push, or receive. They can automate tasks and enforce coding standards.
-
What are some common Git workflows?
- Answer: Common workflows include Gitflow (for managing releases), GitHub Flow (simpler workflow focused on branching from main), and GitLab Flow (adaptable workflow).
-
What is a submodule in Git?
- Answer: A submodule allows you to include another Git repository as a subdirectory within your main repository.
-
What is a subtree in Git?
- Answer: A subtree is similar to a submodule but integrates the sub-project directly into the main project's history.
-
What is the difference between submodules and subtrees?
- Answer: Submodules maintain a separate Git repository within the main project, while subtrees merge the sub-project's history directly into the main project's history. Subtrees result in a more integrated history but can be more complex to manage.
-
How do you resolve a merge conflict using a merge tool?
- Answer: Configure a merge tool (like Meld, Beyond Compare, or KDiff3) in Git and use the `git mergetool` command to launch the tool to visually resolve the conflicts.
-
Explain the concept of a Git LFS (Large File Storage).
- Answer: Git LFS is an extension that manages large files more efficiently. Instead of storing large files directly in the Git repository, it stores pointers to the files in a remote LFS server.
-
What is Git's object model?
- Answer: Git stores data as a series of objects: blobs (files), trees (directories), and commits (snapshots).
-
What is a sparse checkout?
- Answer: A sparse checkout allows you to only download specific parts of a repository, saving disk space and improving cloning speed when you only need a portion of the project.
-
What is `git worktree`?
- Answer: `git worktree` allows you to have multiple working directories linked to the same repository, enabling you to work on different branches simultaneously without switching contexts.
-
How do you find a specific commit in Git?
- Answer: You can use `git log` with various options to search for commits based on author, date, message, or part of a SHA-1 hash.
-
What are some best practices for using Git?
- Answer: Write clear commit messages, use branches effectively, regularly push to remote repositories, use a `.gitignore` file, and understand the difference between `git merge` and `git rebase`.
-
What is the command to revert a specific commit?
- Answer: `git revert
`
- Answer: `git revert
-
How do you view the history of a file?
- Answer: `git log --follow --
`
- Answer: `git log --follow --
-
What is a shallow clone?
- Answer: A shallow clone only downloads a portion of the repository's history, saving time and space when you don't need the entire history.
-
How to create a shallow clone?
- Answer: `git clone --depth=
`
- Answer: `git clone --depth=
-
How do you update a shallow clone?
- Answer: `git fetch --depth=
` or `git fetch --unshallow`
- Answer: `git fetch --depth=
-
What are some common Git aliases?
- Answer: Common aliases include `co` for `checkout`, `ci` for `commit`, `st` for `status`, `br` for `branch`, etc. These make commands easier to type.
-
How do you create a Git alias?
- Answer: `git config --global alias.
`
- Answer: `git config --global alias.
-
What is the command to show the difference between the working directory and the last commit?
- Answer: `git diff HEAD`
-
What is the command to show the difference between the staging area and the last commit?
- Answer: `git diff --staged`
-
What is the command to show the difference between the working directory and the staging area?
- Answer: `git diff`
-
How do you ignore specific files within a directory?
- Answer: Add entries like `path/to/directory/*.log` to your .gitignore file.
-
How do you undo a `git add`?
- Answer: `git reset HEAD
`
- Answer: `git reset HEAD
-
How do you undo changes in the working directory?
- Answer: `git checkout --
`
- Answer: `git checkout --
-
How do you force push changes to a remote repository (and why should you be cautious)?
- Answer: `git push --force` or `git push --force-with-lease`. Force pushing overwrites the remote branch's history and can cause problems for collaborators. It should only be used in exceptional circumstances.
Thank you for reading our blog post on 'Git Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!