01 Git & GitHub Basics - gannurohith/devops-interview-wiki GitHub Wiki

📁 01 - Git & GitHub Basics (Basic to Intermediate Q&A)

  1. What is Git? Git is a distributed version control system that tracks changes in source code during software development.

  2. What is GitHub? GitHub is a cloud-based hosting platform for Git repositories with collaboration and CI/CD features.

  3. How do you check the current status of your Git repo? git status – shows modified, staged, and untracked files.

  4. How do you initialize a Git repository? git init – initializes a new Git repository locally.

  5. How do you clone a repository? git clone <repo_url> – copies a remote repository locally.

  6. What is the difference between git pull and git fetch?

    • git fetch downloads changes but doesn't apply them.
    • git pull fetches and merges changes into your branch.
  7. How do you stage files in Git? git add <filename> – adds file to staging area.

  8. How do you commit changes in Git? git commit -m "message" – records changes in the local repo.

  9. How do you push commits to a remote repository? git push origin <branch>

  10. How do you check commit history? git log – shows list of commits.

  11. What is a branch in Git? A branch is an independent line of development in a repository.

  12. How do you create and switch to a new branch? git checkout -b <branch_name>

  13. How do you merge one branch into another?

  • Checkout target branch: git checkout main
  • Merge: git merge <feature_branch>
  1. What are merge conflicts and how do you resolve them? Conflicts occur when two branches modify the same lines. Resolve manually and then commit.

  2. How do you discard uncommitted changes? git checkout -- <file> – restores file from last commit.

  3. How do you remove a file from Git but keep it locally? git rm --cached <file>

  4. What is .gitignore used for? Lists files/folders Git should ignore.

  5. How do you revert a commit? git revert <commit_hash> – creates a new commit that undoes the specified one.

  6. What is a pull request in GitHub? A request to merge changes from one branch into another, with review and discussion.

  7. What is Git stash? Temporarily shelves uncommitted changes: git stash, restore with git stash pop

  8. How do you tag a release in Git? git tag v1.0 then git push origin v1.0

  9. How do you see who made which changes in a file? git blame <file> – shows line-by-line authorship.

  10. How do you squash commits in Git? Use git rebase -i HEAD~n to combine commits.

  11. How do you undo the last commit but keep the changes? git reset --soft HEAD~1

  12. What is the difference between revert, reset, and checkout?

  • revert: create new commit that undoes a commit.
  • reset: move HEAD and optionally update working tree.
  • checkout: switch branches or restore files.
  1. What is a fork in GitHub? A copy of a repository under your account to freely experiment with changes.

  2. What is a Git remote? A reference to a remote repository, e.g., origin.

  3. How do you list remote repositories? git remote -v

  4. How do you resolve a detached HEAD state? Create a branch from current state: git checkout -b <new_branch>

  5. How do you view the differences between commits? git diff <commit1> <commit2>


01. Git & GitHub Basics (Q&A)

  1. Explain the difference between git fetch, git pull, and git merge. Answer: git fetch retrieves new commits from a remote but doesn't integrate them; git merge integrates branches and creates a merge commit if needed; git pull = fetch + merge.

  2. You need to rollback a faulty commit already pushed to main—how? Answer: Use git revert <commit> to create a new commit that undoes changes without rewriting history. Avoid git reset on shared branches.

  3. Describe an effective branching strategy for a CI/CD pipeline used by 5+ teams. Answer: GitFlow or trunk-based development. GitFlow uses main, develop, and feature/hotfix branches; trunk-based favors short-lived branches merged frequently into main with CI checks.

  4. A PR shows a merge conflict—what steps do you take to resolve it? Answer: Pull the latest main, attempt to merge: git merge main. Conflicts will appear. Manually resolve conflicts, mark as resolved (git add <file>), commit, push.

  5. How can you squash multiple commits before merging? Answer: git rebase -i HEAD~n, change pick to squash, edit commit message, then force-push (git push -f) if required.

  6. Scenario: Large binary files in a repo—what’s the issue, fix? Answer: They bloat repository history. Use Git LFS to track binaries separately or remove history with git filter-repo.

  7. Explain git rebase vs git merge in a shared branch context. Answer: merge preserves history and is non-destructive. rebase rewrites commit history, so avoid it on shared branches unless everyone agrees.

  8. How do you restore a deleted local branch after switching? Answer: Use git reflog to find the last commit hash, then run git checkout -b <branch> <commit>.

  9. Demonstrate a cherry-pick workflow for selective bugfix. Answer: git checkout hotfix, then git cherry-pick <SHA>, resolve conflicts if any, test, push.

  10. Explain tags vs branches; when build vs release. Answer: Branches move forward; tags are static snapshots. Tags are used for marking releases; branches for development workflows.

  11. Your CI pipeline needs commit message linting—how integrate? Answer: Add a commit-msg hook or use tools like Husky or GitHub Actions with commitlint.

  12. How to configure GitHub branch protection? Answer: Go to Settings > Branches > Add rule. Require PR reviews, status checks, signed commits, and restrict force pushes.

  13. Explain git bisect—how find regression? Answer: Run git bisect start, mark good and bad commits, and Git will binary search for the first bad commit.

  14. Difference between SSH and HTTPS remotes? Which for DevOps? Answer: SSH uses key pairs, HTTPS uses credentials/tokens. SSH preferred in dev environments, HTTPS is often used in CI.

  15. How to work around file permission changes causing diffs? Answer: Set core.fileMode=false in Git config. This avoids flagging permission changes as diffs.

  16. Repo-sprawl across many microservices—how to manage? Answer: Use GitHub templates, mono-repo (if appropriate), submodules (rare), or automation to sync changes across repos.

  17. Explain fork, clone, and upstream—how to contribute to upstream repo? Answer: Fork to your namespace, clone locally, add original as upstream, fetch and rebase from upstream, then PR.

  18. Describe common GitHub Actions you’d implement across repos. Answer: CI build/test, linting, security scanning, release tagging, deployment to environments, Slack notifications.

  19. Your push was rejected—remote has changes. What do you do? Answer: Run git pull --rebase, resolve any conflicts, then push.

  20. You want to enforce commit signing—how? Answer: Generate GPG or SSH key, configure git config --global commit.gpgsign true, enforce via GitHub settings.

  21. Explain merge commit vs fast-forward vs rebase merge. Answer: Merge commits preserve full history. Fast-forward simply moves HEAD. Rebase merge rewrites history for a cleaner linear flow.

  22. A branch is accidentally force-pushed—how recover? Answer: Use git reflog or GitHub UI to find the last known commit and restore it.

  23. Demonstrate resolving upstream conflicts across forks. Answer: Add upstream remote, fetch upstream, checkout feature branch, git rebase upstream/main, resolve conflicts, push.

  24. How do you remove sensitive data from Git history? Answer: Use git filter-repo or BFG to delete secrets, rotate credentials, then force-push cleaned history.

  25. Real-world: Dev sees failing build on main after merge—how you respond? Answer: Revert faulty PR, communicate issue, patch bug on a separate branch, test, push fix. Add pre-merge checks to prevent future issues.

⚠️ **GitHub.com Fallback** ⚠️