---Advertisement---

Git Commands Interview Question and Answers (Level-2)

By Manisha

Published On:

---Advertisement---

14. What are Git Merge Strategies? How do you handle merge conflicts?

Merge strategies are methods Git uses to combine changes from different branches. They’re crucial during feature integration or collaborative development.

🔹 Common Merge Strategies:

  • git merge –strategy=ours <branch_name>
    • Uses changes from the current branch.
    • Ignores changes from the branch being merged.
    • Useful in hotfixes where the current code must override others.
  • git merge –strategy=theirs <branch_name>
    • Opposite of ours; prioritizes incoming branch changes.
    • Common in long-lived feature branches or legacy merges.
  • git merge –abort
    • Used to cancel an in-progress merge if conflicts are hard to resolve.
    • Restores working directory to the pre-merge state.

Use-case Example:

You’re merging a development branch into main. You notice lots of conflicts due to recent refactors. If you realize your branch is more updated, you may use –strategy=ours to resolve all conflicts favoring your code.


15. What is git bisect and how is it useful for debugging?

git bisect is a binary search tool to find the commit that introduced a bug.

🔹 Command Breakdown:

  • git bisect start: Initializes bisect session.
  • git bisect bad: Marks current commit as “buggy”.
  • git bisect good <commit>: Marks a known-good commit.
  • git bisect log: Shows progress log of the bisect process.
  • git bisect reset: Exits bisect mode and returns to normal state.

Example Workflow:

bash

git bisect start

git bisect bad

git bisect good abc123

Git will now automatically checkout midpoint commits and you test at each stage.

Useful in CI pipelines or debugging regressions introduced in large codebases.


16. How are Git Hooks used for automation?

Git hooks are custom scripts that Git triggers at certain lifecycle events.

🔹 Types of Hooks:

  • pre-commit: Run before a commit. Enforce code standards using linters or test runners.
  • commit-msg: Validates commit message format (e.g., Jira ID, semantic versioning).
  • post-commit: Trigger post-commit tasks like notifying a team via Slack.
  • pre-push: Validate code before pushing. Run tests or security scans.
  • post-merge: Clean up files or recompile after a merge.

Real-World Example:

In DevOps pipelines, pre-push is used to ensure no broken test cases are pushed to the staging branch.


17. How do you clean up untracked files and optimize Git repositories?

🔹 Cleanup Commands:

  • git clean -n: Dry run – shows files that would be deleted.
  • git clean -f: Force deletes untracked files.
  • git clean -fd: Deletes both untracked files and folders.
  • git gc: Garbage collection to compress and optimize repo.
  • git prune: Deletes unreachable Git objects (use with caution).

Best Practice:

Run git clean -n before using -f or -fd to avoid accidental deletion.


18. What is git archive and when should you use it?

git archive is used to export a snapshot of your repository without .git history.

🔹 Usage:

  • git archive –format=tar –output=repo.tar main
  • git archive –format=zip –output=repo.zip feature-branch

Use Cases:

  • Packaging code for deployment or delivery.
  • Sharing code snapshots with external stakeholders.

19. What is git blame and how does it help in tracking file changes?

🔹 Purpose:

git blame helps track who changed each line of a file, when, and in which commit.

  • git blame <file>: Shows line-by-line commit history.
  • git annotate <file>: Alias to blame.

Example:

Helpful when debugging to trace back which developer introduced a specific line of code and why.


20. What is git reflog and how is it different from git log?

git reflog tracks movements of HEAD, while git log shows only commit history.

🔹 Key Commands:

  • git reflog: Show all recent HEAD changes (even discarded commits).
  • git reflog show: Detailed view per reference.
  • git reflog delete <ref>: Deletes a specific reflog entry.
  • git reflog expire –expire=now –all: Prunes old entries.

Use-case:

After an accidental git reset –hard, reflog helps recover lost commits.


21. How is Git used in CI/CD pipelines?

CI/CD integrates Git to ensure automated building, testing, and deployment.

🔹 Useful Commands:

  • git fetch –all: Fetch all remotes – ensures you’re working on updated code.
  • git merge origin/main: Merge remote changes before build.
  • git rebase origin/main: Keeps linear history before push.
  • git push –force-with-lease: Safe force-push with collaboration protection.
  • git tag v1.0.0: Tagging releases for deployment.

Example:

When merging feature branches into main, Git triggers CI to test and deploy code automatically.


22. How does Git help with Infrastructure as Code (IaC)?

Git is commonly used to manage IaC tools like Terraform, Ansible, or Helm.

🔹 Typical Commands:

  • git clone <repo_url>: Get IaC code locally.
  • git checkout dev-env: Switch to a specific environment.
  • git pull origin staging: Update IaC before deployment.
  • git submodule: Manage shared IaC components across multiple repos.

23. How are Git Hooks used for DevOps automation?

DevOps leverages Git hooks to automate builds, tests, deployments, and validation.

🔹 Examples:

  • pre-commit: Run static code analysis before commit.
  • post-merge: Auto-deploy to test environment.
  • commit-msg: Validate ticket number or versioning format.
  • pre-push: Run integration tests before pushing.

Automates quality checks and avoids manual intervention in pipelines.


24. How is Git used with Feature Flags in DevOps?

Feature flags control the release of features without merging code prematurely.

🔹 Git Workflow:

  • git branch feature-login
  • git checkout -b feature-login
  • git merge feature-login: Merge code behind a flag (off in production)

Enables safe deployments of incomplete features by toggling flags.


25. How is Git used in multi-environment deployments?

Each environment (dev/staging/prod) usually has a dedicated branch.

🔹 Commands:

  • git checkout staging: Deploy staging version.
  • git pull origin staging: Get latest staging code.
  • git merge –no-ff feature-branch: Keeps merge history for auditing.
  • git tag v2.3.0: Create release tag for production.

Helps in controlled deployments and easy rollback.


26. How is Git integrated into deployment automation?

Git triggers automated deployments via webhooks or CI/CD tools like Jenkins/GitHub Actions.

🔹 Deployment Triggers:

  • git push origin main: Starts staging/production deployment.
  • git tag prod-1.0.0: Triggers production release pipeline.
  • git reset –hard <commit>: Roll back to stable commit before re-deploy.

Ensures consistent and repeatable deployments across all stages.

👉The Next 13 Questions-3: GIT COMMANDS

---Advertisement---

Leave a Comment