Skip to main content

Command Palette

Search for a command to run...

🚀Git Interview Qusetions

Crack the Devops Interview

Updated
16 min read
🚀Git Interview Qusetions

Git & GitHub Interview Questions (1–20)

Level: Senior DevOps | Focus: Real-world Scenarios, Tricky Use Cases


1. What’s the difference between git fetch and git pull?

Answer:

  • git fetch retrieves updates from a remote without applying them.

  • git pull does git fetch + git merge (or git rebase depending on config).


2. How do you resolve a merge conflict in Git?

Answer:

  • Open conflicted files.

  • Manually edit sections marked with <<<<<<<, =======, >>>>>>>.

  • Stage resolved files and commit.


3. You accidentally committed sensitive data. How do you remove it from the repo's history?

Answer:

  • Use git filter-branch (older) or git filter-repo (new tool).

  • Remove sensitive file and force-push.

  • Rotate secrets immediately.


4. What’s the difference between git reset and git revert?

Answer:

  • git reset alters commit history (destructive).

  • git revert adds a new commit that reverses changes (non-destructive).


5. Scenario: How do you cherry-pick a commit from another branch without switching branches?

Answer:

git cherry-pick <commit-hash>

6. What’s the use of .gitignore vs .gitkeep?

Answer:

  • .gitignore tells Git which files/folders to ignore.

  • .gitkeep is not a Git feature; it's a placeholder to keep empty folders tracked.


7. You deleted a branch locally but it still exists remotely. How do you delete it from GitHub?

Answer:

git push origin --delete <branch-name>

8. How do you recover a commit that was just reset with git reset --hard?

Answer:
Use git reflog to find the commit and reset to it:

git reflog
git reset --hard <commit>

9. Scenario: A junior dev accidentally did a git push --force and erased history. How do you recover?

Answer:

  • Use reflog from another clone.

  • Create a patch or fetch backup from CI/CD artifact, if available.

  • Encourage enabling branch protection.


10. What is the use of Git tags in CI/CD pipelines?

Answer:

  • Tags represent stable release points (v1.0.0, etc.).

  • Pipelines often trigger on push tags to deploy releases.


11. How do you squash multiple commits into one after a feature is complete?

Answer:

git rebase -i HEAD~n
  • Replace pick with squash or s on later commits.

12. How can you prevent accidental force-pushes to the main branch?

Answer:

  • GitHub branch protection rules.

  • receive.denyNonFastForwards = true on Git server.

  • Set push.default = simple.


13. Scenario: Git history is bloated with unnecessary binary files. What do you do?

Answer:

  • Use git filter-repo to clean history.

  • Add *.bin or similar to .gitignore.

  • Use Git LFS if binaries are needed.


14. How do you create a Git alias for frequent commands like git log --oneline --graph?

Answer:

git config --global alias.lg "log --oneline --graph --all"

15. Explain Git LFS and when to use it.

Answer:
Git LFS (Large File Storage) stores large files (media, binaries) outside normal Git storage to reduce repo size and improve performance.


16. Scenario: You get non-fast-forward error when pushing. What do you do?

Answer:

  • First try: git pull --rebase

  • If intended to overwrite: git push --force-with-lease (safer than --force)


17. What is a detached HEAD state, and how do you get out of it?

Answer:

  • Happens when you checkout a commit instead of a branch.

  • Fix by:

      git checkout <branch-name>
    

18. How do you revert a specific file from an older commit?

Answer:

git checkout <commit-hash> -- path/to/file

19. How do you track changes to submodules in Git?

Answer:

  • Submodules are fixed to a specific commit.

  • To update:

      git submodule update --remote
    

20. Scenario: Multiple developers push conflicting changes to the same branch. How do you manage this?

Answer:

  • Enable protected branches with review rules.

  • Encourage feature branches + PR process.

  • Rebase locally before push to reduce conflicts.

Git & GitHub Interview Questions (21–40)

Level: Senior DevOps | Focus: Advanced Workflows, Troubleshooting, CI/CD Use Cases


21. Scenario: You need to test a feature locally from a remote GitHub PR without merging it. How would you do that?

Answer:

git fetch origin pull/<PR_ID>/head:pr-branch
git checkout pr-branch

22. How do you enforce commit message standards in a GitHub repo?

Answer:

  • Use commit hooks via Husky or pre-commit.

  • Enforce checks using CI/CD (e.g., GitHub Actions, GitLab CI).

  • Optionally integrate tools like commitlint.


23. What is the purpose of a .gitmodules file?

Answer:
It records submodule information — remote URL and path inside the main repo.


24. Scenario: CI pipeline triggers multiple times for a single PR. What might be wrong in GitHub Actions workflow?

Answer:
Likely caused by using both pull_request and push triggers. Solution:

  on:
    pull_request:
      branches: [main]

25. How can you automate version tagging in Git during CI/CD releases?

Answer:

  • Use git tag v<version> and git push origin --tags.

  • Automate with tools like semantic-release or write custom scripts in CI.


26. Scenario: GitHub Actions is failing to checkout a private submodule. How do you fix it?

Answer:

  • Provide SSH or token access in the workflow:

      - uses: actions/checkout@v3
        with:
          submodules: recursive
          token: ${{ secrets.GITHUB_TOKEN }}
    

27. What’s the difference between origin/master and master?

Answer:

  • master: Your local branch.

  • origin/master: The state of master on the remote the last time you fetched.


28. How do you ensure your GitHub repository is secure for production code?

Answer:

  • Enable branch protection and required reviews.

  • Use Dependabot for dependency scanning.

  • Enforce signed commits.

  • Use CodeQL analysis.


29. What is the difference between git stash and git stash pop?

Answer:

  • git stash saves changes temporarily.

  • git stash pop restores changes and deletes stash entry.


30. Scenario: Your Git repo is 1 GB due to logs and build artifacts. What’s your cleanup strategy?

Answer:

  • Add them to .gitignore.

  • Clean history with git filter-repo or BFG.

  • Use Git LFS if needed for large files.


31. What is git rebase --onto used for?

Answer:
Rebase a branch onto a different base:

git rebase --onto new-base old-base feature

32. Scenario: GitHub Actions workflow needs to skip a step if commit message contains [skip ci]. How do you do that?

Answer:
Use an if condition:

if: "!contains(github.event.head_commit.message, '[skip ci]')"

33. How do you create a signed Git commit?

Answer:

    git commit -S -m "Your commit message"
  • Requires GPG key setup.

34. Scenario: Someone force-pushed to the main branch and removed 10 commits. How do you restore?

Answer:

  • If reflog is available:

      git reflog
      git reset --hard <old_commit>
    

    If not, retrieve from backup clone or CI artifacts.


35. What’s the difference between git archive and git bundle?

Answer:

  • git archive: Creates a tar/zip of the current tree — no Git history.

  • git bundle: Backs up complete Git history, used for offline clones.


36. How do you generate a patch from Git commits and apply it in another repo?

Answer:

git format-patch -1 <commit>
git apply patchfile.patch

37. Scenario: A developer is working directly on the main branch. How would you enforce feature branches only?

Answer:

  • Protect main branch.

  • Require PRs and reviews.

  • Add push restrictions.


38. What are orphan branches in Git?

Answer:

Created with no parent history:

git checkout --orphan gh-pages

39. How do you force Git to overwrite local changes during pull?

Answer:

git fetch origin
git reset --hard origin/main

40. Scenario: You accidentally committed to the wrong branch. What do you do?

Answer:

git checkout correct-branch
git cherry-pick <commit>
git checkout wrong-branch
git reset --hard HEAD~1

Git & GitHub Interview Questions (41–60)

Level: Senior DevOps | Focus: CI/CD, Collaboration, Troubleshooting, Advanced GitOps


41. Scenario: How would you create a GitHub Action that only runs on changes in a backend/ folder?

Answer:

    on:
      push:
        paths:
          - 'backend/**'

42. What is the difference between HEAD, HEAD^, and HEAD~1 in Git?

Answer:

  • HEAD: current commit.

  • HEAD^: parent of HEAD.

  • HEAD~1: first ancestor of HEAD (same as HEAD^ but can go deeper, e.g., HEAD~2).


43. How do you configure Git to use a specific user/email for a specific repo only?

Answer:

git config user.name "DevOps Sr"
git config user.email "devops@example.com"

44. Scenario: You want to prevent direct commits to the main branch but allow merges via PR only. How?

Answer:

  • Enable Branch Protection Rules on GitHub:

  • Require pull requests.

  • Disallow force pushes.

  • Require status checks or approvals.


45. What does git describe do and how is it useful?

Answer:

  • Shows human-readable name for the current commit using tags.

  • Useful for release versions:

      git describe --tags
    

46. How do you clean up all local branches that have been deleted on the remote?

Answer:

git fetch -p
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d

47. Scenario: You want to mirror an internal Git repo to GitHub. How do you do that?

Answer:

git clone --mirror git@internal/repo.git
cd repo.git
git push --mirror git@github.com:user/repo.git

48. What is the difference between shallow and deep clones in Git?

Answer:

  • Shallow clone (--depth=1): only latest commit.

  • Full clone: entire history.

  • Use shallow clone for CI/CD to speed up builds.


49. Scenario: Your team commits large files by mistake. How to prevent this proactively?

Answer:

  • Use .gitattributes + Git LFS.

  • Pre-commit hook with size checks:

      find . -size +5M
    

50. What are the different merge strategies available in Git?

Answer:

  • recursive (default).

  • ours.

  • theirs.

  • octopus (for merging >2 branches).

  • resolve.


51. Scenario: You are working on multiple features in parallel. How do you manage this efficiently in Git?

Answer:

  • Use feature branches.

  • Rebase regularly from main to avoid drift.

  • Stash uncommitted changes between contexts.


52. What is a Git worktree and when should you use it?

Answer:

  • It allows multiple working directories linked to the same repo.

  • Useful for working on multiple branches simultaneously.


53. Scenario: A developer complains their GitHub Action secrets are not being read. What could be wrong?

Answer:

  • Secrets are not available to forks by default.

  • Secrets must be defined at repo or org level.

  • Check for typos: secrets.SECRET_NAME.


54. How does git bisect work?

Answer:

  • Helps identify the commit that introduced a bug using binary search.

  • Command flow:

      git bisect start
      git bisect bad
      git bisect good <commit>
    

55. Scenario: You need to trigger a GitHub Action from another repo. How do you approach it?

Answer:

  • Use repository_dispatch:

  • Trigger with a token from one repo.

  • Accept in the target repo’s workflow:

      on: repository_dispatch
    

56. What is the difference between git blame and git log -L?

Answer:

  • git blame: shows the author of each line.

  • git log -L: tracks history of a specific line/range of lines.


57. How do you manage Git credentials securely on CI/CD runners?

Answer:

  • Use environment variables or GitHub/GitLab secrets.

  • Avoid storing .netrc or plaintext tokens in the repo.

  • Use deploy keys or fine-scoped PATs.


58. Scenario: A developer pushes directly to main via CLI even though PRs are required. Why?

Answer:

  • Likely branch protection rule is not enforced for admins.

  • Check and enable “Include administrators” option.


59. How do you detect and fix a “dangling commit”?

Answer:

  • Use:

      git fsck --lost-found
      git reflog
    

    Recover with:

      git checkout <dangling-commit>
    

60. Scenario: You want to enable signed commits for all contributors. How?

Answer:

  • Enforce via GitHub branch protection rules: Require signed commits.

  • Each user must configure GPG key locally.

Git & GitHub Interview Questions (61–80)

Level: Senior DevOps | Focus: GitOps, CI/CD, Enterprise GitHub Usage, Recovery & Automation


61. Scenario: You’re implementing GitOps for Kubernetes deployments. What role does Git play in GitOps?

Answer:

  • Git acts as the single source of truth for the desired cluster state.

  • Git commits trigger reconciliation by tools like ArgoCD or Flux.


62. How do you handle secrets in a GitOps setup with GitHub repositories?

Answer:

  • Never commit secrets to Git.

  • Use sealed secrets, external secret stores (like HashiCorp Vault or SOPS).

  • Manage access through GitHub Actions secrets or encrypted files.


63. What is the purpose of the --force-with-lease option in git push?

Answer:

  • It ensures you're only force-pushing if no one else has updated the branch since your last fetch, adding safety over --force.

64. Scenario: A developer merged a large binary file, which bloated the repository. How do you remove it efficiently?

Answer:

  • Use:

      git filter-repo --path filename --invert-paths
    

    Then run:

      git gc --prune=now --aggressive
    

65. What is git bundle and when would you use it?

Answer:

  • It creates a portable Git repository as a single file.

  • Useful for offline backup, air-gapped environments, or transferring without network.


66. Scenario: CI jobs are failing due to incorrect .gitmodules entries. What’s the fix?

Answer:

  • Update .gitmodules with correct URLs.

    Run:

      git submodule sync
      git submodule update --init --recursive
    

67. How do you compare two branches and list only the files that differ?

Answer:

git diff --name-only branch1..branch2

68. How can you squash the last N commits into one without changing the commit message?

Answer:

git reset --soft HEAD~N
git commit --amend

69. Scenario: You want to enforce specific labels before merging a PR. How do you do that?

Answer:

  • Use GitHub Actions or Probot apps.

  • Define a job that fails if required labels are missing:

      if: contains(github.event.pull_request.labels.*.name, 'approved')
    

70. What’s the best way to mirror a GitHub repository to a self-hosted Git server continuously?

Answer:

  • Use GitHub webhooks to trigger mirroring scripts.

  • Optionally, use CI tools (GitHub Actions) to automate push to the mirror on every push event.


71. Scenario: A collaborator complains that their PR was merged without review. How do you prevent this?

Answer:

  • Enable “Require pull request reviews before merging” under GitHub Branch Protection Rules.

72. What does the --no-ff flag do in a git merge?

Answer:

  • Forces creation of a merge commit even when a fast-forward is possible, preserving branch history.

73. Scenario: You're investigating why GitHub Action secrets aren't accessible in forked PRs. Why?

Answer:

  • Secrets are not exposed to workflows triggered from forks due to security reasons.

74. How do you reapply a commit that was reverted earlier?

Answer:

git cherry-pick <reverted-commit-hash>

75. What’s the purpose of git rerere?

Answer:

  • Stands for “reuse recorded resolution”.

  • Automatically re-applies previous merge conflict resolutions.


76. Scenario: Git clone takes too long due to large history. What are your options?

Answer:

  • Use:

      git clone --depth 1
    

    Clean up history with filter-repo or use Git LFS.


77. What is a rebase interactive (git rebase -i) used for?

Answer:

  • Used to edit, squash, reorder, or remove commits.

  • Ideal for cleaning up feature branches before merging.


78. Scenario: A teammate wants to contribute but doesn’t want to fork the repo. How do you securely give them access?

Answer:

  • Add them as a collaborator with limited permission (Write or Triage).

  • Use protected branches and required reviews for safety.


79. What is the difference between git diff and git status?

Answer:

  • git diff: shows line-by-line changes.

  • git status: shows staged, unstaged, and untracked file statuses.


80. Scenario: CI pipeline is always rebuilding everything, even on small changes. How do you optimize this?

Answer:

  • Use caching (e.g., GitHub Actions cache).

  • Use checksum comparison on files.

  • Split monolith workflows using path-based triggers.

Git & GitHub Interview Questions (81–100)

Level: Senior DevOps | Focus: Failures, Internals, GitHub Enterprise, GitOps Security, Team Collaboration


81. Scenario: A teammate accidentally removed a Git tag used for production deployment. How do you recover it?

Answer:

  • Check with:

      git reflog show --tags
    

    Recreate:

      git tag <tag> <commit>
      git push origin <tag>
    

82. What is the .gitattributes file used for?

Answer:

  • Controls how Git treats files (e.g., line endings, merge strategy).

  • Used for Git LFS, custom diff, and filters.


83. How do you automatically delete a feature branch after PR is merged in GitHub?

Answer:

  • Enable "Automatically delete head branches" in repo settings under General → Pull Requests.

84. Scenario: Two devs push the same file with different changes at the same time. GitHub merges both. Why no conflict?

Answer:

  • If changes are in different lines, Git can automatically merge.

  • Conflicts only arise with overlapping line changes.


85. What does git rev-parse HEAD return?

Answer:

  • The full SHA-1 hash of the latest commit (HEAD).

86. What is the difference between git clone --mirror and git clone --bare?

Answer:

  • --bare: no working directory, used on servers.

  • --mirror: includes remote-tracking branches and config, good for repo backup.


87. Scenario: CI/CD fails due to GitHub rate-limiting. How can this be mitigated?

Answer:

  • Use a GitHub token with higher quota, or cache dependencies.

  • Switch to SSH instead of HTTPS.

  • Use GitHub Enterprise for higher limits.


88. What is a fast-forward merge and when is it preferred?

Answer:

  • A merge where the target branch is directly updated without a new merge commit.

  • Preferred for linear history when no divergent commits exist.


89. Scenario: You're setting up a monorepo with multiple CI workflows. How do you optimize?

Answer:

  • Use path-based triggers in GitHub Actions:

      on:
        push:
          paths:
            - 'service-a/**'
    

90. How do you list all commits that modified a specific file?

Answer:

git log --follow -- path/to/file

91. What is a shallow submodule clone?

Answer:

  • Cloning submodules with --depth=1 to reduce fetch time:

      git submodule update --init --depth 1
    

92. Scenario: A GitHub Actions workflow is stuck. How do you debug it?

Answer:

  • Check Actions logs.

  • Enable ACTIONS_STEP_DEBUG and ACTIONS_RUNNER_DEBUG secrets.

  • Add echo/printenv commands for inspection.


93. What is the difference between fork and clone in GitHub context?

Answer:

  • Clone: copies a repo locally.

  • Fork: copies a repo to your GitHub account with history, enabling pull requests back to original.


94. What are monorepos and what Git challenges do they pose?

Answer:

  • Monorepos contain multiple projects in one Git repo.

  • Challenges: long CI times, complex history, merge conflicts, partial builds.


95. Scenario: GitHub Actions workflow should only run on PR to main, but it's running on all branches. Why?

Answer:

  • Misconfigured trigger:

      on:
        pull_request:
          branches: [main]
    

96. What is Git’s object model composed of?

Answer:

  • Four main objects: blob, tree, commit, and tag.

  • Stored in .git/objects.


97. How does Git handle line endings across OS (Windows vs Linux)?

Answer:

  • With .gitattributes and core.autocrlf:

  • true: converts LF → CRLF on checkout.

  • input: converts CRLF → LF on commit.


98. Scenario: You accidentally committed large secrets. You removed them and force pushed. Is that enough?

Answer:

  • Not enough. Git history still contains them.

  • Use git filter-repo or BFG Repo Cleaner.

  • Revoke secrets immediately.


99. How do you enable automatic branch creation in GitHub when pushing to a new remote branch?

Answer:

git push -u origin new-branch

100. Scenario: You need to prove audit integrity of your repo. How does Git support this?

Answer:

  • Git’s history is cryptographically secure (SHA-1).

  • Use signed commits and tags.

  • GitHub supports GPG and SSH verification badges.


Please add your inputs on questions and answers

More from this blog

thiru's blog

43 posts