Git Interview Questions and Answers

For those preparing for Git interviews, focusing on commonly searched topics can enhance your readiness. Key areas include Git interview questions and answers for both experienced candidates and DevOps engineers, covering advanced topics like Git merge conflicts, branching strategies, and integration with CI/CD. Freshers should review basic Git interview questions, while all candidates should be prepared for Git commands and troubleshooting scenarios. Tailoring your preparation to these frequently searched keywords will boost your confidence and performance in Git interviews.

1. How do you handle large repositories in Git to maintain performance?

To manage large repositories effectively, consider using Git LFS (Large File Storage) for handling large binary files, dividing the repository into smaller, more manageable submodules, and using shallow clones to limit the history that’s fetched.

Example:

To track large files with Git LFS, you would:

  • Run git lfs track "*.psd" to track files with the .psd extension.
  • Commit the .gitattributes file with git add .gitattributes and git commit -m "Add LFS support for PSD files".

2. What is Git Reflog and how can it help in recovering lost commits?

Git Reflog keeps track of updates to the tip of branches and can be used to find commits that are no longer visible in the commit history. It’s useful for recovering lost commits or undoing changes.

Example:

To find a lost commit:

  • Execute git reflog to display a list of recent branch updates.
  • Identify the commit hash of the lost commit.
  • Restore it using git checkout <commit-hash>.

3. What are the differences between git reset --soft, git reset --mixed, and git reset --hard?

  • git reset --soft moves the HEAD to a previous commit but retains changes in the index and working directory.
  • git reset --mixed updates the HEAD and index, but leaves the working directory as is.
  • git reset --hard resets the HEAD, index, and working directory to match the specified commit, discarding all changes.

Example:

To perform a soft reset:

  • Run git reset --soft HEAD~1.

For a mixed reset:

  • Use git reset --mixed HEAD~1.

For a hard reset:

  • Execute git reset --hard HEAD~1.

4. How is git bisect used to identify a commit that introduced a bug?

git bisect helps you find the commit that introduced a bug by performing a binary search through the commit history, allowing you to test and identify the problematic commit.

Example:

To start a bisect session:

  • Begin with git bisect start.
  • Mark the current commit as bad with git bisect bad and a known good commit with git bisect good <good-commit>.
  • Test the intermediate commits and mark them as good or bad.
  • Conclude with git bisect reset once the problematic commit is identified.

5. How can you combine multiple commits into one using Git?

Combining multiple commits into one involves using interactive rebase to merge several commits into a single, consolidated commit.

Example:

To squash commits:

  • Start an interactive rebase with git rebase -i HEAD~3.
  • Change pick to squash for the commits you want to merge.
  • Save and close the editor, then modify the commit message if necessary.

6. What does git cherry-pick do and how is it used?

git cherry-pick applies the changes from a specific commit to your current branch, allowing you to selectively integrate changes from other branches.

Example:

To cherry-pick a commit:

  • Switch to the target branch with git checkout <branch-name>.
  • Apply the commit using git cherry-pick <commit-hash>.

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

Git submodules are repositories nested inside another Git repository, enabling you to include external projects. You can initialize and update submodules to keep them synchronized with their sources.

Example:

To add a submodule:

  • Use git submodule add <repository-url> <path>.

To update a submodule:

  • Run git submodule update --remote.

8. How do Git hooks work and how can they be configured?

Git hooks are scripts that run automatically at certain points in Git’s workflow, like before a commit or push. They are set up in the .git/hooks directory.

Example:

To set up a pre-commit hook:

  • Navigate to .git/hooks and rename pre-commit.sample to pre-commit.
  • Add your script to pre-commit, such as a command to run tests:
    sh npm test

9. What’s the difference between rebasing and merging in Git?

Rebasing integrates changes from one branch onto another, producing a linear history, while merging combines branches by creating a merge commit, which can lead to a more complex commit history.

Example:

To rebase a feature branch onto the main branch:

  • Switch to the feature branch with git checkout <feature-branch>.
  • Run git rebase main to apply the changes on top of the main branch.

10. How can you restore a deleted Git branch?

Restoring a deleted branch involves using Git Reflog to locate the commit hash of the deleted branch and then creating a new branch from that commit.

Example:

To recover a deleted branch:

  • Find the commit hash with git reflog.
  • Create a new branch with git checkout -b <branch-name> <commit-hash>.

11. What’s the purpose of git revert and how does it differ from git reset?

git revert creates a new commit that undoes the changes from a previous commit, preserving the history. git reset, however, alters the commit history and can discard changes, which is useful for correcting mistakes.

Example:

To revert a commit:

  • Use git revert <commit-hash> to generate a new commit that reverses the specified commit’s changes.

12. How can you use git log to filter commits by a specific range?

You can use options with git log to view commits from a specific date range or other criteria.

Example:

To view commits between January 1, 2024, and January 31, 2024:

  • Run git log --since="2024-01-01" --until="2024-01-31".

13. What does it mean to be in a detached HEAD state, and how can you resolve it?

Being in a detached HEAD state means your HEAD is pointing to a specific commit rather than a branch. You can resolve this by creating a new branch from the current commit.

Example:

To create a branch from a detached HEAD:

  • Use git checkout -b <new-branch>.

14. How do you resolve merge conflicts in Git and what tools are available?

Merge conflicts arise when changes in different branches clash. Tools like git mergetool, as well as graphical tools such as KDiff3 or Meld, can help resolve these conflicts.

Example:

To resolve conflicts using git mergetool:

  • Execute git mergetool to open the configured merge tool and manually resolve conflicts.

15. What is git tag used for and how do you create and push tags?

git tag is used to create markers for specific commits, often used for releases. Tags can be lightweight or annotated.

Example:

To create an annotated tag:

  • Run git tag -a <tag-name> -m "Tag message".

To push the tag to a remote repository:

  • Use git push origin <tag-name>.

16. How do you create and switch to a new branch in one step?

You can use git checkout -b to create a new branch and switch to it simultaneously.

Example:

To create and switch to a new branch:

  • Execute git checkout -b <new-branch>.

17. What is the function of git stash and how do you apply or remove stashed changes?

git stash temporarily saves your changes so you can switch branches or work on something else. You can apply or discard these changes later.

Example:

To stash changes and apply them:

  • Run git stash to save changes.
  • Use git stash apply to reapply the stashed changes.
  • To discard stashed changes, use git stash drop.

18. What does git config do and what are some common settings?

git config sets Git configuration options, including user information and preferences.

Example:

To set your user name and email globally:

  • Run git config --global user.name "Your Name".
  • Run git config --global user.email "you@example.com".

19. What are Git aliases and how do you create and use them?

Git aliases are custom shortcuts for Git commands that simplify your workflow.

Example:

To create an alias for git status:

  • Run git config --global alias.st status.
  • Use git st as a shortcut for git status.

20. How do you edit commit history with git rebase -i?

Interactive rebase allows you to modify, reorder, or remove commits.

Example:

To start an interactive rebase:

  • Run git rebase -i HEAD~3 to rebase the last three commits.
  • In the editor, reorder or change commits as needed and save.

21. What does git diff do and how can it be used to compare branches?

git diff shows differences between commits, branches, or the working directory and index.

Example:

To compare changes between two branches:

  • Use git diff <branch1> <branch2>.

22. What is the role of git fetch versus git pull?

git fetch retrieves updates from a remote repository without merging them into your current branch, while git pull fetches and merges changes into the current branch.

Example:

To fetch updates:

  • Run git fetch origin.

To fetch and merge:

  • Use git pull origin <branch-name>.

23. How can you use git grep to search for content in your repository?

git grep searches for specific patterns within files in the repository.

Example:

To search for the string “example” in your repository:

  • Run git grep "example".

24. Explain how to use git archive to create a snapshot of your repository.

git archive creates an archive file of your repository or a part of it, useful for distributing or backing up.

Example:

To create a zip archive of the current branch:

  • Run git archive -o latest.zip HEAD.

25. How do you apply patches with git apply?

git apply is used to apply changes from a patch file to your working directory.

Example:

To apply a patch file:

  • Run git apply <patch-file>.

26. What is the git revert command and how is it different from git reset?

git revert creates a new commit that reverses changes made by a previous commit, while git reset alters the commit history and can discard changes.

Example:

To revert a commit:

  • Use git revert <commit-hash>.

27. How do you use git blame to identify the author of specific lines in a file?

git blame shows who last modified each line of a file, useful for tracking changes and understanding code history.

Example:

To blame a file:

  • Run git blame <file-name>.

28. What is the function of git rebase and how does it differ from merging?

git rebase integrates changes from one branch onto another, creating a linear history, while merging creates a merge commit, preserving the branch history.

Example:

To rebase a branch:

  • Use git rebase <base-branch>.

29. How do you handle submodules in Git?

Git submodules allow you to include and manage external repositories within your main repository.

Example:

To add a submodule:

  • Run git submodule add <repository-url> <path>.

To update a submodule:

  • Use git submodule update --remote.

30. How do you configure git hooks for automated tasks?

Git hooks are scripts that run automatically at various stages of Git operations, configured in the .git/hooks directory.

Example:

To set up a pre-commit hook:

  • Rename pre-commit.sample to pre-commit.
  • Add your script or commands to pre-commit and make it executable.

31. What is git stash and how do you use it to manage uncommitted changes?

git stash temporarily saves your uncommitted changes, allowing you to switch contexts or branches without losing work.

Example:

To stash changes:

  • Run git stash.

To apply stashed changes:

  • Use git stash apply.

32. How do you use git log to explore commit history?

git log shows the commit history, with options to format and filter the output.

Example:

To view detailed logs:

  • Run git log --oneline --graph --decorate.

33. What is git cherry-pick and when would you use it?

git cherry-pick applies changes from a specific commit to your current branch, useful for applying individual commits from other branches.

Example:

To cherry-pick a commit:

  • Use git cherry-pick <commit-hash>.

34. How can you use git diff to compare changes between commits or branches?

git diff shows differences between two commits, branches, or the working directory and index.

Example:

To compare two branches:

  • Run git diff <branch1> <branch2>.

35. What are Git aliases and how can you create them?

Git aliases are shortcuts for commonly used Git commands, configured in your Git settings.

Example:

To create an alias:

  • Run git config --global alias.co checkout to create a shortcut for git checkout.

36. How do you use git rebase to clean up commit history?

git rebase can be used to reorder, squash, or edit commits, helping to clean up the commit history.

Example:

To interactively rebase:

  • Run git rebase -i HEAD~3 to modify the last three commits.

37. What is the purpose of git tag and how do you create and push tags?

git tag creates markers for specific commits, often used for releases or important points in the history.

Example:

To create a tag:

  • Run git tag -a v1.0 -m "Version 1.0".

To push a tag:

  • Use git push origin v1.0.

38. How do you handle merge conflicts in Git?

Merge conflicts occur when changes in different branches are incompatible. They must be resolved manually.

Example:

To resolve a conflict:

  • Open the conflicting files, make necessary changes, and then add and commit the resolved files.

39. How do you use git fetch and git pull to update your local repository?

git fetch retrieves updates from a remote repository without merging, while git pull fetches and merges updates into the current branch.

Example:

To fetch updates:

  • Run git fetch.

To fetch and merge:

  • Use git pull.

40. What is git archive and how can it be used to create an archive of your repository?

git archive creates a compressed file of your repository or part of it, useful for distributing or backing up.

Example:

To create a tar archive:

  • Run git archive --format=tar HEAD > archive.tar.

41. How do you recover a deleted commit using git reflog?

git reflog tracks updates to the tip of branches and can help you find and recover deleted commits.

Example:

To recover a commit:

  • Run git reflog to find the commit hash and then use git checkout -b <branch-name> <commit-hash>.

42. What is the difference between git reset and git revert?

git reset changes the commit history and can discard changes, while git revert creates a new commit that reverses changes from a previous commit.

Example:

To revert a commit:

  • Run git revert <commit-hash>.

43. How do you use git branch to manage branches?

git branch lists, creates, or deletes branches in your repository.

Example:

To create a new branch:

  • Run git branch <branch-name>.

To delete a branch:

  • Use git branch -d <branch-name>.

44. How can you use git log to search for commits by author?

You can filter commits by author using git log with the --author option.

Example:

To find commits by a specific author:

  • Run git log --author="Author Name".

45. What is the purpose of git diff and how can you use it to view changes?

git diff shows the differences between files or commits, allowing you to review changes.

Example:

To view changes between your working directory and the index:

  • Run git diff.

46. How do you use git stash to save and apply changes?

git stash saves uncommitted changes temporarily, allowing you to switch branches or work on other tasks.

Example:

To stash changes:

  • Run git stash.

To apply stashed changes:

  • Use git stash apply.

47. How can you create and apply patches using Git?

Patches represent changes in files and can be applied to other repositories or branches.

Example:

To create a patch:

  • Run git format-patch -1 <commit-hash>.

To apply a patch:

  • Use git apply <patch-file>.

48. What does git grep do and how is it used for searching?

git grep searches for specific patterns within files in your repository.

Example:

To search for the pattern “search-term”:

  • Run git grep "search-term".

49. How do you use git log to display a concise commit history?

git log can be formatted to show a more compact view of commit history.

Example:

To display a concise log:

  • Run git log --oneline --graph --decorate.

50. What is the function of git clean and how can it be used to manage untracked files?

git clean removes untracked files or directories from your working directory.

Example:

To remove untracked files:

  • Run git clean -f.

To remove untracked directories:

  • Use git clean -fd.