01 Git & GitHub Basics - gannurohith/devops-interview-wiki GitHub Wiki
-
What is Git? Git is a distributed version control system that tracks changes in source code during software development.
-
What is GitHub? GitHub is a cloud-based hosting platform for Git repositories with collaboration and CI/CD features.
-
How do you check the current status of your Git repo?
git status
– shows modified, staged, and untracked files. -
How do you initialize a Git repository?
git init
– initializes a new Git repository locally. -
How do you clone a repository?
git clone <repo_url>
– copies a remote repository locally. -
What is the difference between
git pull
andgit fetch
?-
git fetch
downloads changes but doesn't apply them. -
git pull
fetches and merges changes into your branch.
-
-
How do you stage files in Git?
git add <filename>
– adds file to staging area. -
How do you commit changes in Git?
git commit -m "message"
– records changes in the local repo. -
How do you push commits to a remote repository?
git push origin <branch>
-
How do you check commit history?
git log
– shows list of commits. -
What is a branch in Git? A branch is an independent line of development in a repository.
-
How do you create and switch to a new branch?
git checkout -b <branch_name>
-
How do you merge one branch into another?
- Checkout target branch:
git checkout main
- Merge:
git merge <feature_branch>
-
What are merge conflicts and how do you resolve them? Conflicts occur when two branches modify the same lines. Resolve manually and then commit.
-
How do you discard uncommitted changes?
git checkout -- <file>
– restores file from last commit. -
How do you remove a file from Git but keep it locally?
git rm --cached <file>
-
What is
.gitignore
used for? Lists files/folders Git should ignore. -
How do you revert a commit?
git revert <commit_hash>
– creates a new commit that undoes the specified one. -
What is a pull request in GitHub? A request to merge changes from one branch into another, with review and discussion.
-
What is Git stash? Temporarily shelves uncommitted changes:
git stash
, restore withgit stash pop
-
How do you tag a release in Git?
git tag v1.0
thengit push origin v1.0
-
How do you see who made which changes in a file?
git blame <file>
– shows line-by-line authorship. -
How do you squash commits in Git? Use
git rebase -i HEAD~n
to combine commits. -
How do you undo the last commit but keep the changes?
git reset --soft HEAD~1
-
What is the difference between
revert
,reset
, andcheckout
?
-
revert
: create new commit that undoes a commit. -
reset
: move HEAD and optionally update working tree. -
checkout
: switch branches or restore files.
-
What is a fork in GitHub? A copy of a repository under your account to freely experiment with changes.
-
What is a Git remote? A reference to a remote repository, e.g., origin.
-
How do you list remote repositories?
git remote -v
-
How do you resolve a detached HEAD state? Create a branch from current state:
git checkout -b <new_branch>
-
How do you view the differences between commits?
git diff <commit1> <commit2>
-
Explain the difference between
git fetch
,git pull
, andgit 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
. -
You need to rollback a faulty commit already pushed to
main
—how? Answer: Usegit revert <commit>
to create a new commit that undoes changes without rewriting history. Avoidgit reset
on shared branches. -
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 intomain
with CI checks. -
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. -
How can you squash multiple commits before merging? Answer:
git rebase -i HEAD~n
, change pick tosquash
, edit commit message, then force-push (git push -f
) if required. -
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
. -
Explain
git rebase
vsgit 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. -
How do you restore a deleted local branch after switching? Answer: Use
git reflog
to find the last commit hash, then rungit checkout -b <branch> <commit>
. -
Demonstrate a cherry-pick workflow for selective bugfix. Answer:
git checkout hotfix
, thengit cherry-pick <SHA>
, resolve conflicts if any, test, push. -
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.
-
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.
-
How to configure GitHub branch protection? Answer: Go to Settings > Branches > Add rule. Require PR reviews, status checks, signed commits, and restrict force pushes.
-
Explain
git bisect
—how find regression? Answer: Rungit bisect start
, mark good and bad commits, and Git will binary search for the first bad commit. -
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.
-
How to work around file permission changes causing diffs? Answer: Set
core.fileMode=false
in Git config. This avoids flagging permission changes as diffs. -
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.
-
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. -
Describe common GitHub Actions you’d implement across repos. Answer: CI build/test, linting, security scanning, release tagging, deployment to environments, Slack notifications.
-
Your push was rejected—remote has changes. What do you do? Answer: Run
git pull --rebase
, resolve any conflicts, then push. -
You want to enforce commit signing—how? Answer: Generate GPG or SSH key, configure
git config --global commit.gpgsign true
, enforce via GitHub settings. -
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.
-
A branch is accidentally force-pushed—how recover? Answer: Use
git reflog
or GitHub UI to find the last known commit and restore it. -
Demonstrate resolving upstream conflicts across forks. Answer: Add upstream remote, fetch upstream, checkout feature branch,
git rebase upstream/main
, resolve conflicts, push. -
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. -
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.