Git Interview Questions and Answers for 2 years experience

Git Interview Questions and Answers
  1. What is Git?

    • Answer: Git is a distributed version control system (DVCS) used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project, managing different versions and merging their work efficiently.
  2. Explain the difference between Git and GitHub.

    • Answer: Git is a version control system; GitHub is a web-based hosting service that uses Git for version control. Git is the underlying technology, while GitHub provides a platform for collaboration, code review, and project management features.
  3. What are the three main states of a file in Git?

    • Answer: Modified, Staged, and Committed. Modified means the file has been changed but not yet saved to the repository. Staged means the changes are ready to be committed. Committed means the changes are permanently stored in the repository's history.
  4. What is the Git repository?

    • Answer: A Git repository (or repo) is a directory containing all the project files, along with a hidden `.git` directory that stores the entire version history.
  5. Explain the command `git init`.

    • Answer: `git init` initializes a new Git repository in the current directory. It creates the hidden `.git` directory and sets up the necessary files for version control.
  6. What does `git clone` do?

    • Answer: `git clone ` creates a local copy of a remote Git repository. This downloads the entire repository's history and files to your machine.
  7. How do you add changes to the staging area?

    • Answer: Use the command `git add ` to add individual files or `git add .` to add all modified files in the current directory and its subdirectories to the staging area.
  8. What is the purpose of `git commit`?

    • Answer: `git commit -m "Your commit message"` saves the staged changes to the local repository. The commit message provides a description of the changes made.
  9. Explain `git status`.

    • Answer: `git status` shows the current state of your working directory and staging area. It indicates which files have been modified, staged, or are untracked.
  10. How do you view the commit history?

    • Answer: `git log` displays the commit history. Options like `--oneline` or `--graph` can provide a more concise or visual representation.
  11. 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. This promotes parallel development.
  12. How do you create a new branch?

    • Answer: `git branch ` creates a new branch. To switch to the new branch, use `git checkout `.
  13. How do you merge a branch into another branch?

    • Answer: Checkout the target branch (`git checkout `), then merge the source branch (`git merge `). Resolve any merge conflicts that may arise.
  14. What is a merge conflict?

    • Answer: A merge conflict occurs when two branches have made changes to the same lines of the same file. Git cannot automatically determine which changes to keep, requiring manual resolution.
  15. How do you resolve a merge conflict?

    • Answer: Manually edit the conflicted files, indicating which changes to keep. Then stage the resolved files (`git add `) and commit the merge (`git commit`).
  16. What is `git rebase`?

    • Answer: `git rebase` rewrites the commit history by moving a branch's commits onto another branch. This creates a cleaner, linear history but should be used cautiously, especially on shared branches.
  17. What is the difference between `git merge` and `git rebase`?

    • Answer: `git merge` preserves the branch history, creating a merge commit. `git rebase` rewrites the history, creating a linear history but potentially altering shared history, which can cause issues in collaborative environments.
  18. What is a remote repository?

    • Answer: A remote repository is a version of your project hosted on a server, such as GitHub, GitLab, or Bitbucket. It allows for collaboration and backups.
  19. How do you add a remote repository?

    • Answer: `git remote add ` adds a remote repository with a given name and URL.
  20. How do you fetch changes from a remote repository?

    • Answer: `git fetch ` downloads changes from the remote repository without merging them into your local branches.
  21. How do you pull changes from a remote repository?

    • Answer: `git pull ` fetches changes from the remote repository and merges them into your current local branch.
  22. How do you push changes to a remote repository?

    • Answer: `git push ` uploads your local commits to the remote repository.
  23. 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 (e.g., v1.0, v2.0).
  24. How do you create a tag?

    • Answer: `git tag ` creates an annotated tag. `git tag -a -m "Your tag message"` creates an annotated tag with a message.
  25. How do you delete a local branch?

    • Answer: `git branch -d ` deletes a local branch. Use `-D` to force delete a branch that hasn't been merged.
  26. How do you delete a remote branch?

    • Answer: `git push origin --delete ` deletes a remote branch. Or, `git push origin :`.
  27. What is `git stash`?

    • Answer: `git stash` temporarily saves your uncommitted changes, allowing you to switch branches or clean up your working directory without losing your work. You can later apply the stashed changes using `git stash pop`.
  28. Explain `git cherry-pick`.

    • Answer: `git cherry-pick ` applies a specific commit from one branch to another. This is useful for applying a single fix or feature from a different branch without merging the entire branch.
  29. What is `.gitignore`?

    • Answer: `.gitignore` is a file that specifies patterns of files or directories that should be ignored by Git. This prevents accidental tracking of temporary files, build artifacts, or sensitive data.
  30. How do you undo your last commit?

    • Answer: `git reset --soft HEAD~1` moves the HEAD pointer back one commit, keeping the changes in the staging area. `git reset --hard HEAD~1` discards the changes completely. Use caution with `--hard`.
  31. How do you revert a commit?

    • Answer: `git revert ` creates a new commit that undoes the changes introduced by a specific commit. This is safer than `git reset --hard` as it preserves the original commit history.
  32. What is Git Hooks?

    • Answer: Git hooks are scripts that run automatically before or after Git events, such as commit, push, or receive. They can be used for various purposes, including enforcing coding standards, running tests, or automating deployments.
  33. What is a Gitflow workflow?

    • Answer: Gitflow is a branching model that uses separate branches for developing features, releasing software, and managing hotfixes. It provides a structured approach to managing releases and maintaining a stable main branch.
  34. What is GitHub Actions?

    • Answer: GitHub Actions is a CI/CD platform that allows you to automate workflows directly within your GitHub repository. You can automate tasks such as building, testing, and deploying your code.
  35. Explain the concept of a HEAD pointer in Git.

    • Answer: The HEAD pointer refers to the currently checked-out branch or commit. It indicates which version of the code you are currently working on.
  36. What are detached HEAD states?

    • Answer: A detached HEAD state occurs when your HEAD pointer is pointing to a specific commit, rather than a branch. This can happen when checking out a tag or a specific commit hash. You should create a new branch to avoid losing your work.
  37. What is a shallow clone?

    • Answer: A shallow clone downloads only a portion of the repository's history, typically the most recent commits. This is useful for large repositories where you only need the recent changes, saving time and disk space.
  38. How to find a specific commit in git?

    • Answer: Use `git log --grep=""` to search for commits containing a specific keyword. You can also search by author, date, or commit message using various `git log` options.
  39. How to revert changes in a file?

    • Answer: `git checkout -- ` discards changes in a file and reverts it to the last committed version.
  40. What is Git LFS (Large File Storage)?

    • Answer: Git LFS is an extension to Git that allows you to manage large files more efficiently. Instead of storing large files directly in the repository, it stores pointers and manages the files externally, keeping the repository smaller and faster.
  41. How to create a patch file in Git?

    • Answer: `git diff > patch_file.patch` creates a patch file containing the differences between your current working tree and the last commit. This patch can be applied to another repository.
  42. How to apply a patch file in Git?

    • Answer: `git apply patch_file.patch` applies a patch file to your working tree. Use `git am` for applying patches created with `format-patch` which is more robust for collaborative work.
  43. What is the difference between `git fetch` and `git pull`?

    • Answer: `git fetch` downloads the changes from a remote repository but doesn't merge them. `git pull` is equivalent to `git fetch` followed by `git merge`.
  44. What is the command to view the differences between two commits?

    • Answer: `git diff ` shows the differences between two commits. You can use commit hashes or branch names.
  45. Explain the concept of a working directory, staging area, and repository in Git.

    • Answer: The working directory is where you make changes to your files. The staging area is where you prepare changes to be committed. The repository is where the complete version history is stored.
  46. How to check out a specific commit?

    • Answer: `git checkout ` checks out a specific commit. This puts you in a detached HEAD state. Remember to create a branch if you want to make changes to this commit.
  47. How to find the author of a specific commit?

    • Answer: Use `git log -p --author=` or inspect the output of `git show ` to find the author of a commit.
  48. What is the significance of the `.git` directory?

    • Answer: The `.git` directory is a hidden directory that contains all the internal data of the Git repository. It should not be modified manually.
  49. How to ignore specific files or directories when committing?

    • Answer: Add the patterns of the files or directories you want to ignore in the `.gitignore` file. Git will then ignore those files when you commit.
  50. How do you rename a branch in Git?

    • Answer: You cannot directly rename a branch. You must create a new branch with the desired name and then delete the old branch.
  51. How to undo a `git add` command?

    • Answer: `git reset HEAD ` removes a file from the staging area without removing the changes from the working directory.
  52. How to see the difference between the working directory and the last commit?

    • Answer: `git diff` shows the difference between your current working directory and the last commit.
  53. How to move a file in Git?

    • Answer: `git mv ` moves and renames a file. This updates Git's tracking of the file.
  54. What is `git reflog`?

    • Answer: `git reflog` shows a log of all changes to the HEAD pointer, even those not reflected in the commit history. This is useful for recovering lost commits or branches.
  55. How to create a new repository on GitHub?

    • Answer: Create a new repository through the GitHub website interface, providing a repository name, description, and optionally, choosing to initialize the repository with a README file.
  56. How to fork a repository on GitHub?

    • Answer: Click the "Fork" button on the GitHub repository page. This creates a copy of the repository in your own GitHub account.
  57. How to create a pull request on GitHub?

    • Answer: After forking and making changes in your fork, click "New pull request" on GitHub to propose your changes to the original repository.
  58. What is a Git submodule?

    • Answer: A Git submodule allows you to include another Git repository as a part of your project. This keeps the submodule's history separate but allows you to include its code in your project.

Thank you for reading our blog post on 'Git Interview Questions and Answers for 2 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!