1. Git Fundamentals
1. What is Git?
Git is a distributed version control system used to track changes in source code. It helps developers maintain history and collaborate safely.
git --version
2. Why do we use Git?
Git is used for version tracking, collaboration, branching, merging, and rollback. It allows multiple developers to work on the same project safely.
3. What is Version Control?
Version Control is a system that records changes to files over time. It lets us track who changed what, compare versions, and restore older versions.
4. What is a Distributed Version Control System?
In a DVCS, every developer has a complete local copy of the repository and its history. Developers can commit and view history even without connecting to a central server.
5. Centralized vs Distributed Version Control
| Centralized | Distributed |
|---|---|
| Central server contains main history | Every developer has full history |
| Server dependency is higher | Local Git operations work offline |
| Example: SVN | Example: Git |
6. What is a Git repository?
A Git repository is a project directory whose files and version history are tracked by Git. Its Git metadata and history are stored in the .git directory.
git init
7. Local repository vs remote repository
| Local Repository | Remote Repository |
|---|---|
| Exists on developer's machine | Hosted on a remote server |
| Used for local commits | Used for sharing/collaboration |
| Example: local Git repo | Example: GitHub/Azure Repos |
8. Working Directory, Staging Area, and Local Repository
Working Directory contains current files, Staging Area holds changes selected for the next commit, and Local Repository stores committed history.
Working Directory → git add → Staging Area → git commit → Local Repository
9. What is a commit?
A commit is a saved snapshot of staged changes in the Git history. Each commit contains information such as author, message, timestamp, and a unique hash.
git commit -m "Add login feature"
10. What is a commit hash?
A commit hash is the unique identifier Git assigns to a commit. It is commonly used to identify a specific version when viewing, reverting, or cherry-picking commits.
git log --oneline
11. What is HEAD in Git?
HEAD is a reference to the commit currently checked out, normally through the current branch. It usually represents your current position in Git history.
git log HEAD
12. What is .gitignore?
.gitignore tells Git which untracked files or folders should not be tracked, such as build output, temporary files, or local configuration.
bin/
obj/
*.log
13. What is the .git folder?
The .git folder contains the repository's commits, branches, references, configuration, and metadata. Removing it removes Git tracking/history from that working copy.
14. What is the basic Git workflow?
The common workflow is modify → stage → commit → push. Changes are first made locally, staged, committed to the local repository, and then pushed remotely.
git add . → git commit -m "message" → git push
2. Basic Git Commands
1. What does git init do?
git init initializes a directory as a new local Git repository. It creates the hidden .git directory.
git init
2. What does git clone do?
git clone creates a local copy of an existing repository, including its Git history. It normally configures the source repository as origin.
git clone https://github.com/user/project.git
3. What does git status do?
git status shows the current state of the working directory and staging area, including modified, staged, and untracked files.
git status
4. What does git add do?
git add moves selected changes from the working directory to the staging area, preparing them for the next commit.
git add Program.cs
5. What does git commit do?
git commit saves the staged changes as a new snapshot in the local repository.
git commit -m "Add authentication"
6. What does git push do?
git push sends local commits to a remote repository. It updates the corresponding remote branch.
git push origin main
7. What does git pull do?
git pull retrieves remote changes and integrates them into the current branch. By default this commonly means fetch followed by merge, depending on configuration.
git pull origin main
8. What does git fetch do?
git fetch downloads commits and references from the remote repository without integrating them into your current branch.
git fetch origin
9. What does git log do?
git log displays the repository's commit history, including commit hashes, authors, dates, and messages.
git log --oneline
10. What does git diff do?
git diff displays differences between Git states, commonly showing unstaged changes in the working directory.
git diff
11. What does git remote do?
git remote manages references to remote repositories associated with the local repository.
git remote -v
12. What does git checkout do?
git checkout is an older multi-purpose command used to switch branches, restore files, or check out commits.
git checkout main
13. What does git switch do?
git switch is specifically designed to switch between branches and can also create a new branch.
git switch feature/login
14. What does git restore do?
git restore is used to discard working-tree changes or unstage files. It provides a clearer alternative to some older checkout usages.
git restore Program.cs
15. git add . vs git add <file>
git add . stages changes under the current directory, while git add <file> stages only the specified file.
git add . # Multiple changes
git add Program.cs # Specific file
3. Clone, Fetch, Pull & Push
1. What is git clone?
git clone downloads an existing repository and creates a local working copy containing files, branches/references, and repository history.
git clone https://github.com/user/project.git
2. What happens internally when you clone?
Git creates a local repository, downloads the remote repository's objects/references, creates remote-tracking references, checks out the default branch, and normally adds origin.
3. What is git fetch?
git fetch downloads the latest remote commits and updates remote-tracking branches without changing your current working branch.
git fetch origin
4. What is git pull?
git pull fetches remote changes and then integrates them into the current branch using merge or rebase depending on configuration/options.
git pull origin main
5. git fetch vs git pull
fetch |
pull |
|---|---|
| Downloads changes | Downloads + integrates changes |
| Current branch unchanged | Current branch may change |
6. What is git push?
git push uploads your local commits to a remote repository, usually updating a corresponding remote branch.
git push origin feature/login
7. What happens when you execute git pull?
Git first fetches changes from the remote and then integrates the appropriate remote-tracking branch into your current branch, usually by merge or rebase.
8. Why can git push be rejected?
A push can be rejected when the remote branch contains commits your local branch does not have, or because of branch protection, permissions, or policy checks.
9. What if your local branch is behind remote?
First retrieve and integrate the latest remote changes, resolve conflicts if necessary, and then push again.
git pull --rebase origin main
git push origin main
10. What is origin?
origin is the conventional default name Git gives to the remote repository when you clone it. It is only a remote name and can be renamed.
git remote -v
11. What is an upstream branch?
An upstream/tracking branch is the remote branch associated with a local branch. It allows commands such as git pull and git push to know the default remote/branch.
git push -u origin feature/login
4. Git Branching
1. What is a Git branch, and why use it?
A branch is a movable reference to a line of development. It lets developers work on features, fixes, or experiments independently without directly changing main.
git switch -c feature/login
2. How do you create, switch, and delete a branch?
Use git branch/git switch to create and switch branches, and git branch -d to safely delete a merged local branch.
git branch feature/login
git switch feature/login
git branch -d feature/login
3. Main, feature, release, and hotfix branches
main represents stable code; feature develops functionality; release prepares a release; hotfix handles urgent Production fixes. Their use depends on the team's branching strategy.
4. Local branch vs remote branch
| Local Branch | Remote-tracking Branch |
|---|---|
| Your local development branch | Local reference representing remote state |
Example: main |
Example: origin/main |
5. What is a tracking/upstream branch?
A local branch can track an upstream branch, establishing its default remote counterpart for push/pull operations.
git push -u origin feature/login
6. How do you update a feature branch from main?
Fetch/update main, switch to the feature branch, then merge or rebase the latest main into it.
git switch feature/login
git merge main
5. Merge & Merge Conflicts
1. What is Git merge?
Merge combines changes from one branch into another. Switch to the target branch and merge the source branch.
git switch main
git merge feature/login
2. Fast-forward vs three-way merge
| Fast-forward | Three-way |
|---|---|
| Target has no divergent commits | Branches have diverged |
| Branch pointer can simply move | Git combines two tips using common ancestor |
3. What is a merge conflict?
A merge conflict occurs when Git cannot automatically reconcile competing changes, often to the same lines. Manually choose/combine the correct code, stage it, and complete the merge.
git add Program.cs
git commit
4. What after resolving a merge conflict?
Remove the conflict markers, verify/test the code, stage resolved files, and complete the merge commit if Git requires one.
git add .
git commit
5. How can teams reduce merge conflicts?
Use small branches, frequent synchronization, small focused commits, clear ownership, and frequent PRs. Avoid keeping feature branches isolated for long periods.
6. Merge vs Rebase
1. What is Git rebase?
Rebase moves/replays your branch commits onto another base commit. It is commonly used to create a cleaner linear history.
git rebase main
2. Merge vs rebase
| Merge | Rebase |
|---|---|
| Combines histories | Replays commits on a new base |
| Preserves existing history | Rewrites commit history |
3. When should you use merge vs rebase?
Use merge when preserving shared history is important. Use rebase mainly for cleaning/updating your own local feature branch before sharing or merging.
4. Why avoid rebasing shared branches?
Rebase rewrites commit hashes/history. Rebasing commits already used by other developers can create duplicated history and synchronization problems.
5. How do you handle rebase conflicts?
Resolve the conflicting files, stage them, and continue the rebase. Repeat until all commits have been replayed.
git add .
git rebase --continue
7. Undoing Changes
1. How do you discard and unstage changes?
Use git restore <file> to discard unstaged working-tree changes and --staged to remove a file from the staging area while keeping its working copy.
git restore Program.cs
git restore --staged Program.cs
2. git reset vs git revert
reset |
revert |
|---|---|
| Moves branch/rewrites local history | Creates a new commit that reverses changes |
| Best suited to unpublished history | Safer for shared history |
3. --soft vs --mixed vs --hard
Soft: keeps changes staged. Mixed: keeps changes unstaged. Hard: discards tracked working-tree and index changes associated with the reset.
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
4. Undo last commit before push
If the commit is not shared, reset can move HEAD back. Use --soft when you want to keep the changes staged.
git reset --soft HEAD~1
5. Undo an already pushed shared commit
Use git revert because it creates a new reversing commit without rewriting the shared branch's existing history.
git revert <commit-hash>
8. Stash, Cherry-Pick & Tags
1. What is git stash? apply vs pop?
Stash temporarily saves uncommitted changes so you can switch tasks. apply restores while keeping the stash; pop restores and removes it if successfully applied.
git stash
git stash pop
2. What is git cherry-pick?
Cherry-pick applies the changes introduced by a specific commit onto your current branch. Use it when you need selected commits rather than an entire branch.
git cherry-pick a1b2c3d
3. What is a Git tag?
A tag is a named reference commonly used to mark important commits such as application releases.
git tag -a v1.0.0 -m "Release v1.0.0"
4. Branch vs tag
| Branch | Tag |
|---|---|
| Moves as new commits are added | Normally remains fixed |
| Used for ongoing development | Used to mark releases/milestones |
9. GitHub Fundamentals
1. What is GitHub? Git vs GitHub
Git is the distributed version control system. GitHub is a hosted platform built around Git repositories that adds collaboration features such as PRs, Issues, Actions, and repository management.
2. Local Git repository vs GitHub repository
A local repository exists on your machine for local development and commits. A GitHub repository is a remote repository used for sharing, backup, collaboration, and automation.
3. Fork vs clone
| Fork | Clone |
|---|---|
| Creates your server-side copy of a repository on GitHub | Creates a local copy on your machine |
| Common in external/open-source contribution | Used for local development |
4. What are GitHub Issues and Releases?
Issues track bugs, tasks, and feature requests. Releases package/publish specific versions of a project, usually associated with Git tags.
5. What is GitHub Actions?
GitHub Actions is GitHub's workflow automation platform. It can run CI/CD workflows such as build, test, and deployment when events like pushes or PRs occur.
on: [push, pull_request]
6. How do you authenticate GitHub using PAT or SSH?
With HTTPS, a Personal Access Token (PAT) can authenticate Git operations; with SSH, an SSH key pair authenticates securely without entering a token for each operation.
git clone git@github.com:user/project.git
10. Pull Requests & Code Review
1. What is a Pull Request and its workflow?
A Pull Request (PR) requests that changes from one branch be reviewed and merged into another. Typical flow: push → PR → review/checks → approval → merge.
feature branch → PR → Review → Checks → Merge → main
2. What is code review?
Code review is examination of code changes by other developers before merge. It helps identify bugs and improve quality, maintainability, consistency, and security.
3. How do you resolve a PR merge conflict?
Update the PR branch with the target branch, resolve conflicts locally or in the supported UI, test the result, commit it, and push the updated branch.
git switch feature/login
git merge main
4. What are required reviewers and status checks?
Required reviewers enforce approval before merging. Required status checks require configured CI validations such as build/tests to pass before the PR can be merged.
5. What is a protected branch?
A protected branch applies rules to important branches such as main. Rules can require PRs, approvals, successful checks, and restrict direct pushes.
6. Merge commit vs squash merge vs rebase merge
| Method | Result |
|---|---|
| Merge commit | Preserves branch commits and adds merge commit |
| Squash merge | Combines PR commits into one commit |
| Rebase merge | Replays commits for linear history |
11. Git Branching Strategies
1. What is a Git branching strategy?
A branching strategy defines how a team creates, uses, integrates, and releases branches. It provides a consistent development and release workflow.
2. What is GitFlow?
GitFlow is a branching model traditionally using main, develop, feature, release, and hotfix branches. It supports structured release cycles.
main ← hotfix/release ← develop ← feature
3. What is trunk-based development?
Developers integrate small changes frequently into a shared trunk/main branch, usually using very short-lived branches and strong automated testing.
4. What is GitHub Flow?
GitHub Flow is a lightweight workflow: create a branch from main, commit/push, open a PR, review/test, and merge back to main.
main → feature → PR → main
5. GitFlow vs GitHub Flow vs trunk-based
| Strategy | Key idea |
|---|---|
| GitFlow | Multiple long-lived branch types and structured releases |
| GitHub Flow | Short feature branches + PRs into main |
| Trunk-based | Very frequent integration into trunk/main |
6. How would you handle a Production hotfix?
Create a hotfix branch from the Production branch/tag, fix and test the issue, raise a PR, merge/deploy it, then ensure the fix reaches other active development branches as required.
git switch -c hotfix/payment main
12. Git Security & Best Practices
1. Why should secrets never be committed?
Git history can preserve passwords, API keys, tokens, and connection strings even after files are changed or deleted. Exposed credentials can allow unauthorized access.
2. What if a secret is committed and pushed?
Immediately revoke/rotate the secret, then remove it from current code and, where necessary, rewrite repository history using an appropriate history-cleaning tool. Coordinate before rewriting shared history.
3. Does .gitignore remove files from Git history?
No. .gitignore primarily prevents matching untracked files from being added. It does not remove an already tracked file or erase its previous history.
git rm --cached appsettings.Secret.json
4. How do you protect main?
Configure branch protection/rules to require Pull Requests, approvals, CI checks, and restrict direct pushes or force pushes as appropriate.
5. Important Git team best practices
Use small commits, meaningful messages, feature branches, PR/code reviews, frequent synchronization, protected branches, CI checks, and never commit secrets.
13. Important Real-Time Scenarios
1. Push rejected because remote has newer changes
Fetch/integrate the remote changes first, resolve any conflicts, test, and then push your commits again.
git pull --rebase
git push
2. Two developers changed the same lines
Git may produce a merge conflict. Review both changes, manually create the correct final version, remove conflict markers, stage it, test, and complete the merge/rebase.
git add .
git commit
3. Incorrect commit but not pushed
Because the commit has not been shared, you can safely use git reset. --soft removes the commit while keeping its changes staged.
git reset --soft HEAD~1
4. Incorrect commit already pushed
For a shared branch, use git revert to create a new commit that reverses the incorrect commit without rewriting shared history.
git revert <commit-hash>
5. Accidentally pushed a password
Immediately revoke/rotate the password, remove it from the code, and clean repository history if required. Treat the exposed password as compromised even after deletion.
6. Feature branch is behind main
Update local main, switch back to the feature branch, then merge or rebase main into it according to team policy.
git switch feature/login
git rebase main
7. Urgent Production issue while working on feature
Stash or commit your current feature work, create a hotfix branch from the Production baseline, fix/test/deploy it, then return to your feature work.
git stash
git switch -c hotfix/payment main
8. Need one specific commit from another branch
Use git cherry-pick to apply the changes introduced by that particular commit to your current branch.
git cherry-pick <commit-hash>
14. Git/GitHub → Azure DevOps
1. GitHub vs Azure Repos
Both host Git repositories and support collaboration. GitHub is a broader developer platform, while Azure Repos is Microsoft's repository service integrated with Azure DevOps services.
2. GitHub branch protection vs Azure Repos branch policies
Both protect important branches and enforce controls around merging. GitHub uses rulesets/branch protection, while Azure Repos uses branch policies such as reviewers and build validation.
3. How does a commit or PR trigger CI?
A CI system monitors configured repository events/triggers. A push or PR event can start a pipeline that checks out the code and runs build, tests, and other validations.
trigger:
- main
4. Can Azure Pipelines build GitHub source code?
Yes. Azure Pipelines can connect to a GitHub repository and run CI/CD pipelines when configured GitHub events occur.
GitHub Repository → Azure Pipeline → Build → Test → Deploy
5. Complete Developer-to-Deployment flow
A developer creates a branch, commits and pushes code, opens a PR, receives review/CI validation, and merges it. The merge can trigger CI and, when configured, a deployment process.
Developer → Branch → Commit → Push → PR → Review → Merge → CI Build → Deployment