Progressive Full Stack Application Development with Live Projects

DevOpsMock Interview

Git Mock Interview for Experienced Developers

Git Mock Interview

When interviewing for a position that requires experience with Git, you can expect a range of questions that test both your technical knowledge and practical experience. Here are some advanced Git Interview Questions along with detailed answers for experienced software developers:

1. Explain the difference between git merge and git rebase. When would you use one over the other?

Both git merge and git rebase are used to integrate changes from one branch into another, but they do so in different ways:

git merge: This command combines the changes from one branch into another. It creates a merge commit that preserves the history of both branches. This is ideal for preserving the complete history of a project and is often used for integrating feature branches into the main branch.

git rebase: This command takes the commits from one branch and applies them on top of another branch. It essentially rewrites the commit history to create a linear sequence of commits. When we want to keep a clean, linear history in our feature branch before merging it into the main branch. It’s also useful for keeping a feature branch up-to-date with the main branch without creating a merge commit.

2. What is the purpose of git cherry-pick and how does it differ from git merge?

git cherry-pick is used to apply the changes introduced by a specific commit from one branch onto another branch. It effectively copies a commit from one branch and applies it to the current branch.

git cherry-pick: This command is used to select individual commits from one branch and apply them to another branch. This is useful when we want to apply specific changes from one branch without merging the entire branch. For e.g. When we need to apply a specific bug fix or feature that was introduced in one branch to another branch without merging the entire branch.

git merge: This command integrates the entire history of changes from one branch into another, creating a merge commit if necessary.

3. How does Git handle conflicts during a merge, and what steps would you take to resolve them?

When Git encounters conflicting changes during a merge, it stops the process and marks the conflicts in the affected files. Here’s how you handle it:

  1. Identify Conflicts: Git will indicate which files have conflicts by marking them as unmerged. Open these files to see conflict markers (<<<<<<, ======, >>>>>>).
  2. Resolve Conflicts: Manually edit the files to resolve conflicts. Choose which changes to keep or combine changes from both branches.
  3. Mark as Resolved: After resolving conflicts, mark the files as resolved using git add <file>.
  4. Complete the Merge: Commit the changes with git commit. If you were merging, this creates a merge commit that records the merge process.
  5. Test Changes: Ensure that the merge did not introduce any issues by running tests or reviewing the changes.

4. What is git stash and how would you use it in a scenario where you need to switch branches?

git stash is used to temporarily save changes in your working directory and index that are not yet ready to be committed. This is useful when you need to switch branches but have uncommitted changes that you don’t want to commit yet.

  1. Save Changes: Use git stash to save your changes. This will clean your working directory and allow you to switch branches.
  2. Switch Branches: Use git checkout <branch> to switch to the desired branch.
  3. Apply Stashed Changes: Once you’re on the correct branch, use git stash apply to reapply your stashed changes. If you no longer need the stashed changes, you can use git stash drop to remove them from the stash list.
  4. Stash List: You can view stashed changes with git stash list and apply a specific stash with git stash apply stash@{n}, where n is the stash index.

5. Describe a situation where you might use git bisect and how it works.

git bisect is used to identify the commit that introduced a bug by performing a binary search between a known good commit and a known bad commit.

  1. Start Bisecting: Use git bisect start to begin. Mark the current commit as bad using git bisect bad and mark a known good commit using git bisect good <commit>.
  2. Binary Search: Git will automatically check out a commit halfway between the good and bad commits. Test this commit to determine if it is good or bad.
  3. Continue: Use git bisect good or git bisect bad based on your findings. Git will continue narrowing down the range until it identifies the specific commit that introduced the bug.
  4. Finish Bisecting: Once the problematic commit is found, use git bisect reset to return to your previous branch.

6. What are Git hooks and how can you use them to automate tasks?

Git hooks are scripts that Git executes before or after certain events, such as committing or pushing. They are located in the .git/hooks directory of a repository.

  • Types of Hooks: Common hooks include pre-commit, commit-msg, pre-push, and post-merge.
  • Use Cases:
    • Pre-commit Hook: Run tests or linters before a commit is made to ensure code quality.
    • Commit-msg Hook: Enforce commit message conventions.
    • Pre-push Hook: Check that code passes all tests before pushing changes to a remote repository.
  • Creating Hooks: Write your script in the .git/hooks directory and make it executable. For example, a pre-commit hook might look like:
#!/bin/sh
# Run tests before commit
npm test

7. What are Git submodules and how do you work with them?

Git submodules allow you to include and manage repositories within other repositories. This is useful for managing dependencies or integrating external libraries.

  • Adding a Submodule: Use git submodule add <repository-url> [path] to add a submodule. This command initializes and clones the submodule repository into the specified path.
  • Updating Submodules: Use git submodule update --init --recursive to update and initialize submodules.
  • Cloning Repositories with Submodules: Use git clone --recursive <repository-url> to clone a repository along with its submodules.
  • Removing Submodules: To remove a submodule, delete the relevant section from .gitmodules, then run git rm --cached <submodule-path>, and delete the submodule directory.

8. Explain the concept of “detached HEAD” in Git and how you can recover from it.

A detached HEAD state occurs when you check out a specific commit rather than a branch. This means you are no longer on a branch, and any new commits you make are not associated with any branch.

  • Identifying Detached HEAD: When you are in a detached HEAD state, your prompt or git status will indicate that you are not on any branch.
  • Recovering:
    • Create a New Branch: If you want to keep the changes you’ve made, create a new branch with git checkout -b <new-branch>. This will move your changes onto a new branch.
    • Checkout a Branch: If you don’t need the changes, you can simply check out an existing branch with git checkout <branch>.

9. How does Git handle large binary files, and what are some strategies to manage them?

Git is not optimized for large binary files, as they can bloat the repository and affect performance. Strategies for managing large files include:

  • Git Large File Storage (LFS): Git LFS is an extension that replaces large files with pointers in your Git repository, storing the actual content in a separate LFS server. Use git lfs track <file> to track large files and git lfs push to push them to the LFS server.
  • Git Annex: Git Annex is another tool for managing large files in Git repositories. It allows you to manage files with Git while storing the actual content in various storage locations.
  • Avoid Storing Large Files: Where possible, avoid storing large binaries in the repository. Use external storage solutions or file hosting services for large files.

10. Discuss the advantages and disadvantages of using Git’s --force option.

The --force option is used with commands like git push to forcefully push changes to a remote repository, potentially overwriting commits.

  • Advantages:
    • Override Conflicts: Useful when you need to overwrite remote changes or reset a branch to a previous state.
    • Rewriting History: Allows you to rewrite history or clean up commits in a branch.
  • Disadvantages:
    • Risk of Data Loss: Force pushing can overwrite commits in the remote repository, leading to loss of work.

Leave a Reply