---Advertisement---

Git Commands Interview Question and Answers (Level-3)

By Manisha

Published On:

---Advertisement---

27. What is GitOps and how does Git work in a GitOps Workflow?

Answer:

GitOps is a DevOps practice where Git repositories act as the single source of truth for infrastructure and application configuration.

Key Git Commands in GitOps:

  • git push: Triggers CI/CD automation (e.g., ArgoCD, Flux) to deploy updated configurations to clusters.
  • git clone <repo_url>: Developers clone GitOps repositories to inspect or modify configurations.
  • Changes pushed to Git initiate deployment pipelines automatically, ensuring declarative infrastructure and rollback via Git history.

28. How do you use advanced Git commands to manage CI/CD pipelines?

Answer:

Git is tightly integrated into CI/CD systems to trigger builds and deployments.

Useful Git Commands:

  • git push –tags: Triggers pipelines based on tags (e.g., deploy v2.0.0 tag to production).
  • git fetch –tags: Updates all tags in local, helpful during version-based deployment.
  • git log –oneline –graph: Visually represents commit history in CI logs.
  • git diff <commit1> <commit2>: Used in CI to conditionally trigger builds or tests based on specific file changes.

29. How do you integrate Git with Docker workflows?

Answer:

Git works with Docker to version control app code and Dockerfiles for building images.

Commands Used:

  • git clone <repo_url>: Clones code + Dockerfile for image builds.
  • git checkout <branch>: Switch branches to test different Docker versions.
  • git pull origin <branch>: Pull latest updates before rebuilding images.
  • git tag <v1.0.0>: Tags Git commits to version Docker images accordingly (e.g., docker build -t app:v1.0.0 .).

30. How is Git used with secrets management in DevOps?

Answer:

Git should never store secrets. DevOps uses .gitignore and cleanup commands to prevent leakage.

Key Commands:

  • git rm –cached <file>: Removes secrets from index without deleting locally.
  • git commit –amend: Fixes the last commit (e.g., to remove secret files).
  • git rebase -i <commit>: Allows removing or editing commits with secrets.
  • Bonus: Use tools like git-secrets, SOPS, or Vault for secure secrets handling.

31. What is Git Interactive Rebase and how do you use it to manage commit history?

Answer:

Interactive rebase is used to clean up messy histories before merging.

Key Commands:

  • git rebase -i <commit>: Squash, reorder, or delete commits.
  • git rebase –onto <new_base>: Change base of a branch.
  • git rebase –skip: Skip a problematic commit.
  • git rebase –continue: Continue rebase after resolving conflicts.

This helps maintain a clean and linear history for production deployments.


32. How do you resolve Git merge conflicts efficiently?

Answer:

Merge conflicts occur when changes clash across branches.

Conflict Management:

  • git mergetool: Opens GUI/CLI tool for resolving conflicts.
  • git merge –abort: Cancel merge and return to original state.
  • git diff <branch1> <branch2>: View differences before merging.
  • git status: See which files are conflicting and pending resolution.

33. What is Git commit signing and why is it important in DevOps?

Answer:

Signed commits ensure authenticity and accountability.

Signing Workflow:

  • git config –global user.signingkey <key_id>: Set your GPG key.
  • git commit –gpg-sign: Sign your commit.
  • git tag -s <tag>: Create a signed tag.
  • git log –show-signature: View signature status of commits.

This helps enforce secure and auditable pipelines.


34. How is Git used for Continuous Delivery (CD)?

Answer:

Git facilitates automated releases via tags and branch management.

Key Commands:

  • git push –tags: Triggers production releases via pipelines.
  • git push origin feature:staging: Push code to staging environments.
  • git branch -m <old> <new>: Rename branches for environment promotion.
  • git merge –no-ff: Ensures a merge commit, keeping feature branch history.

35. How can Git be used for rolling back deployments?

Answer:

Rollbacks are crucial when a release fails.

Recovery Commands:

  • git revert <commit>: Creates a new commit that undoes a previous one.
  • git reset –hard <commit>: Roll back to an exact commit (destructive).
  • git log –oneline: Find safe rollback points.
  • git checkout <commit>: Check out old versions to inspect or redeploy.

36. What is the role of Git submodules and subtrees in DevOps?

Answer:

They help manage dependencies or libraries within a Git repo.

Submodules:

  • git submodule add <repo_url>: Adds a dependency as a submodule.
  • git submodule init && git submodule update: Sync submodules.

Subtrees:

  • git subtree add –prefix=<dir> <repo> <branch>: Pulls another repo into a subdirectory.

Subtrees are simpler than submodules and work better with CI/CD.


37. How do teams collaborate using Git for code reviews and PRs?

Answer:

Git enables distributed team collaboration via feature branches and PRs.

Commands:

  • git push origin feature-xyz: Push branch for review.
  • git fetch origin feature-xyz: Fetch team member’s branch.
  • git pull origin feature-xyz: Test changes before merging.
  • git diff feature-xyz main: Compare branches before review.

38. How is Git used in containerized environments (e.g., Kubernetes, Docker)?

Answer:

Git repositories host all the code/configs for container builds.

Typical Usage:

  • git clone: Pull base code + Dockerfile/Helm charts.
  • git checkout dev: Work on development containers.
  • git pull: Sync latest configs before build/deploy.
  • git tag v1.2.0: Version containers via Git tags.
  • git submodule add: Pull reusable Helm templates or Docker build scripts.

39. How does Git integrate with CI/CD for build and deployment pipelines?

Answer:

CI/CD tools (e.g., Jenkins, GitLab CI) listen to Git events.

Workflow Commands:

  • git push: Triggers pipeline.
  • git tag v3.0.0: Tags a version for deployment.
  • git log –oneline: View commit IDs linked to builds.
  • git push –force-with-lease: Safer forced push for collaborative pipelines.

40. How do you use Git for managing documentation and versioned configs?

Answer:

DevOps teams version infrastructure and docs just like code.

Key Commands:

  • git add <doc_file>: Track documentation or YAMLs.
  • git commit -m “Update infra doc”: Commit config changes.
  • git diff: Track changes in docs, configs, or environment files.
  • Allows consistent documentation-driven development and auditing of infra.
---Advertisement---

Leave a Comment