Day 83: Git Interview Questions

Day 83: Git Interview Questions

Introduction:

Git is a successful version control software. It was created in 2005 and got rapidly adopted by software developers, especially because it is very fast and scalable. Its functions allow for parallel development and maintenance of large projects, like linux development.

In the ever-evolving landscape of software development, efficient version control is the backbone of collaborative coding endeavors. It is a distributed version control system that has revolutionized the way teams manage and track changes in their codebase. Born out of the necessity for speed, flexibility, and collaboration, Git, created by Linus Torvalds, empowers developers to seamlessly work on projects, experiment with new features, and navigate the complexities of collaborative coding.

At its core, Git offers a decentralized approach to version control, providing each developer with a complete copy of the project's history. This decentralization not only enhances offline productivity but also allows for agile branching and merging, streamlining development workflows. Git's popularity extends far beyond individual projects, as it serves as the foundation for platforms like GitHub and GitLab, facilitating expansive open-source collaboration.


  1. What is Git?

    • Answer: Git is a distributed version control system designed for tracking changes in source code during software development. It allows multiple developers to collaborate efficiently on projects.
  2. What is a repository in Git?

    • Answer: A Git repository, often referred to as a "repo," is a storage location that holds a project's files, including version history, branches, and configuration data.
  3. What are the advantages of using GIT?

    • Answer: Advantages of Git include decentralized version control, efficient branching and merging, faster performance, and the ability to work offline.
  4. What are Git’s limitations?

    • Answer: Git limitations include a learning curve for beginners, potential for repository size increase, and the need for careful handling in case of large binary files.
  5. What is the distinction between Git & SVN?

    • Answer: Git is a distributed version control system, while SVN (Subversion) is centralized. Git allows users to work offline, has a more efficient branching model, and performs faster.
  6. What is a distributed version control system?

    • Answer: A distributed version control system, like Git, allows every user to have a complete copy of the repository. This enables offline work, faster operations, and better collaboration.
  7. What are the advantages of using a distributed version control system?

    • Answer: Advantages include offline access, faster operations, easier branching, and enhanced collaboration among developers.
  8. What is GIT PUSH?

    • Answer: git push is a command used to upload local repository content to a remote repository. It is used to share changes with others.
  9. What is the difference between GIT PULL, FETCH & CLONE?

    • Answer:

      • git pull: Fetches changes from a remote repository and merges them into the current branch.

      • git fetch: Retrieves changes from a remote repository without merging.

      • git clone: Creates a copy of a remote repository on the local machine.

  10. How to initialize, add & commit in GIT?

    • Answer:

      • git init: Initializes a new Git repository.

      • git add <filename>: Adds a file to the staging area.

      • git commit -m "Commit message": Commits changes to the repository.

  11. How do you revert a commit that has already been pushed and made public?

    • Answer: Use git revert to create a new commit that undoes the changes of a previous commit. This is a safer method for reverting changes in a shared/public branch.
  12. Why do we require branching in GIT?

    • Answer: Branching allows for parallel development, experimentation with new features, and isolation of changes, making development more organized and manageable.
  13. What does a git log command do?

    • Answer: git log displays a chronological log of commits, showing commit messages, authors, dates, and commit hashes.
  14. What does a git diff command do?

    • Answer: git diff shows the differences between working directory changes and the last commit. It helps track modifications before committing.
  15. What does a git config do?

    • Answer: git config is used to set or get configuration options, such as user information, preferences, and aliases.
  16. What is the definition of "Index" or "Staging Area" in GIT?

    • Answer: The "Index" or "Staging Area" in Git is a space where changes are prepared before being committed. It acts as a buffer between the working directory and the repository.
  17. What is a 'conflict' in git?

    • Answer: A conflict in Git occurs when two branches have changes that cannot be automatically merged. Manual intervention is required to resolve the conflicting changes.
  18. What is 'bare repository' in Git?

    • Answer: A bare repository in Git is a repository without a working directory. It only contains the version history and configuration, making it suitable for remote sharing.
  19. What is the purpose of GIT stash?

    • Answer: git stash is used to temporarily save changes that are not ready for committing, allowing the user to switch branches or perform other operations.
  20. What is GIT stash drop?

    • Answer: git stash drop removes the most recent stash entry, discarding the changes. It is used when the stashed changes are no longer needed.
  21. What does the committed item contain?

    • Answer: The committed item in Git contains changes that have been staged (added to the index) and then committed to the repository.
  22. Name a few graphical Git clients for the Linux platforms?

    • Answer: Some graphical Git clients for Linux include GitKraken, Sourcetree, and Gitg.
  23. What is the significance of Git version control?

    • Answer: Git version control enables tracking and managing changes in source code, facilitating collaboration, improving code quality, and providing a history of modifications.
  24. What are some different branching strategies?

    • Answer: Different branching strategies include Feature Branching, Gitflow, GitHub Flow, and Trunk-Based Development.
  25. What are the advantages of the forking workflow?

    • Answer: The forking workflow provides a clean and isolated environment for contributing to a project, allows for easy collaboration, and simplifies the process of submitting pull requests.
  26. What is SubGit and why is it used?

    • Answer: SubGit is a tool used to create a bidirectional Git and Subversion (SVN) synchronization. It allows teams to work with Git while still providing access to projects in Subversion. SubGit ensures that changes made in Git are reflected in Subversion and vice versa.
  27. Explain these commands one by one – git status, git log, git diff, git revert <commit>, git reset <file>.

    • git status: Shows the status of changes as untracked, modified, or staged.

    • git log: Displays the commit history with information like commit hash, author, date, and commit message.

    • git diff: Shows the differences between the working directory and the last commit.

    • git revert <commit>: Creates a new commit that undoes the changes introduced by the specified commit.

    • git reset <file>: Unstages a file, keeping the changes in the working directory.

  28. What exactly is tagging in Git?

    • Answer: Tagging in Git is a way to mark a specific commit as important, often used to label release points. Tags can be annotated (contain additional information) or lightweight (just a pointer to a specific commit).
  29. What exactly is forking in Git?

    • Answer: Forking in Git refers to creating a personal copy of someone else's project. This is often done on platforms like GitHub. The forked repository allows contributors to make changes independently, and if they wish, they can create pull requests to merge their changes back into the original project.
  30. How to change any older commit messages?

    • Answer: Use the git rebase -i command to interactively rebase the commit. Change the word "pick" to "reword" in front of the commit you want to modify. Git will then prompt you to edit the commit message.
  31. Difference between head, working tree, and index.

    • Answer:

      • HEAD: Points to the latest commit in the branch and is the snapshot of the working tree.

      • Working Tree: The directory where the actual files reside and where modifications are made.

      • Index (Staging Area): Acts as a middle-ground where changes are prepared before committing.

  32. What exactly is the forking workflow?

    • Answer: The forking workflow involves creating a personal copy (fork) of a repository, making changes in the forked repository, and then creating pull requests to merge changes into the original repository. It is commonly used in collaborative, open-source development.
  33. How might you recover a branch that has previously pushed changes in the main repository yet has been coincidentally erased from each team member's local machines?

    • Answer: To recover a branch that has been accidentally deleted, you can use the reflog or branch recovery tools to find the commit hash associated with the lost branch and recreate the branch from that commit.
  34. What command helps us to know the branches merged into master and which are not?

    • Answer: The command git branch --merged shows the branches that have been merged into the current branch, while git branch --no-merged shows the branches that have not been merged.
  35. What are some of the most important features of Git?

    • Answer: Some important features of Git include distributed version control, fast performance, efficient branching and merging, data integrity, and the ability to work offline.
  36. How will you resolve conflict in Git?

    • Answer: Conflicts in Git can be resolved by manually editing the conflicted files, marking the conflicts as resolved, and then committing the changes. The git merge or git rebase commands often lead to conflicts that need resolution.
  37. Is Git and GitHub the same thing?

    • Answer: No, Git and GitHub are not the same. Git is a distributed version control system, while GitHub is a web-based platform that provides hosting for Git repositories. GitHub adds collaboration features, issue tracking, and pull requests on top of Git.
  38. What about Git reflog?

    • Answer: Git reflog is a reference log that keeps a record of changes to the tips of branches. It provides a history of all the local branch and HEAD movements, helping to recover lost commits or branches.
  39. Explain the different points when a merge can enter a conflicted stage.

    • Answer: A merge can enter a conflicted stage when changes in the branches being merged overlap or conflict with each other. This occurs when modifications have been made to the same lines in the same file in both branches.
  40. What has to be run to squash the last N commits into a single commit?

    • Answer: To squash the last N commits into a single commit, use the command git rebase -i HEAD~N and change "pick" to "squash" for the commits you want to squash.
  41. What is the difference between fork, branch, and clone?

    • Answer:

      • Fork: Creates a copy of a repository on a platform like GitHub, allowing independent development.

      • Branch: A separate line of development within a repository, often used for features or bug fixes.

      • Clone: Creates a copy of a repository on the local machine, allowing development, typically from a remote repository.

  42. How is Git merge different from Git rebase?

    • Answer: Git merge combines changes from different branches into a new commit, preserving the commit history. Git rebase integrates changes by moving or combining commits, creating a linear history.
  43. What’s the difference between reverting and resetting?

    • Answer:

      • Reverting: Creates a new commit that undoes the changes made in a previous commit, leaving the commit history intact.

      • Resetting: Moves the branch pointer to a specified commit, discarding all subsequent commits. It can be done with or without preserving changes.

  44. What is “git cherry-pick”?

    • Answer: git cherry-pick is a command that allows you to apply changes from a specific commit to the current branch. It takes the changes introduced by the commit and applies them as a new commit on the current branch.
  45. What does the git status command do?

    • Answer: git status shows the status of changes as untracked, modified, or staged. It provides information about the current state of the working directory and staging area.
  46. List out the functions provided by Git repository hosting service.

    • Answer: Functions provided by Git repository hosting services (like GitHub, GitLab, Bitbucket) include repository management, collaboration features, issue tracking, pull requests, continuous integration, and access control.
  47. How to handle huge binary files in Git?

    • Answer: To handle large binary files in Git, consider using Git LFS (Large File Storage) or other external storage solutions. Git LFS replaces large files with text pointers, reducing the repository size.
  48. What do you understand about the Staging area in Git?

    • Answer: The Staging area in Git, also known as the Index, is an intermediate area where changes are prepared before committing. It allows selective inclusion of changes in the next commit.
  49. What does the git reset --mixed and git merge --abort commands do?

    • Answer:

      • git reset --mixed: Unstages changes and resets the index to the specified commit, leaving changes in the working directory.

      • git merge --abort: Aborts a merge operation, resetting the branch to its state before the merge began. Uncommitted changes are preserved.

  50. How is a bare repository different from the standard way of initializing a Git repository?

    • Answer: A bare repository does not have a working directory, making it suitable for server use. It only contains version control information, while a standard Git repository includes a working directory with tracked files.

Conclusion:

Wrapping up our Git interview questions journey! Knowing Git isn't just about memorizing commands; it's about understanding the roadmap of how software developers work together. The questions we covered, from the basics like branches and tags to handling teamwork and conflicts, are like tools in a developer's toolbox.

Think of it as learning how to use a powerful tool that helps teams build awesome things together.

Just remember, it's not just about typing commands; it's about getting the hang of how Git makes coding with others a breeze.

Hope you like my post. Don't forget to like, comment, and share.