Skip to content

Git

← Back to all decks

140 cards — 🟢 40 easy | 🟡 83 medium | 🔴 3 hard

🟢 Easy (40)

1. What does git remote -v show?

Show answer `git remote -v` shows the fetch and push URLs for each configured remote repository. Most repos have one remote named 'origin', but you can add more (e.g., 'upstream' for forked repos). Example output: `origin https://github.com/user/repo.git (fetch)`.

2. What is a Git branch?

Show answer A branch is a lightweight movable pointer to a commit, representing an independent line of development. Creating a branch is nearly instantaneous because Git just creates a 41-byte file (the SHA reference). Common branches: main/master (production), feature/* (new work), hotfix/* (urgent fixes). Use `git branch -a` to list all.

3. What is the .git directory? What can you find there?

Show answer The `.git` folder contains all the information that is necessary for your project in version control and all the information about commits, remote repository address, etc. All of them are present in this folder. It also contains a log that stores your commit history so that you can roll back to history.

This info copied from [https://stackoverflow.com/questions/29217859/what-is-the-git-folder](https://stackoverflow.com/questions/29217859/what-is-the-git-folder)

4. What is the "HEAD" in Git?

Show answer HEAD is a reference (pointer) to the latest commit on the currently checked-out branch. It's stored in `.git/HEAD` and usually contains a symbolic reference like `ref: refs/heads/main`. Use `HEAD~1` for the parent commit, `HEAD~3` for three commits back. Detached HEAD means HEAD points directly to a commit, not a branch.

5. How do you check the status of your Git repository?

Show answer `git status` shows which files are modified, staged, or untracked in your working directory. Red means unstaged changes; green means staged and ready to commit. Use `git status -s` for a compact view: M=modified, A=added, ??=untracked. Add `-b` to also show the branch name.

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

Show answer `git fetch` downloads new commits and refs from the remote but does not modify your working directory. `git pull` does a fetch followed by a merge (or rebase with `--rebase`).
Best practice: use `git fetch` then inspect with `git log origin/main..main` before merging to avoid surprises.

7. What is "origin" in Git?

Show answer 'origin' is the default alias Git assigns to the remote repository you cloned from. It's just a shorthand — you can rename it with `git remote rename origin upstream`. View all remotes with `git remote -v`. You can add additional remotes: `git remote add upstream ` for fork workflows.

8. How does git diff function?

Show answer `git diff` shows line-by-line differences between your working directory and the staging area. Use `git diff --staged` (or `--cached`) to compare staged changes against the last commit. `git diff branch1..branch2` compares two branches. Add `--stat` for a summary of changed files and line counts.

9. How do you restore a file to its state in the last commit?

Show answer `git restore ` discards unstaged changes, reverting the file to the last committed version. Use `git restore --staged ` to unstage without discarding changes.
Gotcha: `git restore` permanently discards your working directory changes — there is no undo. Consider `git stash` first if unsure.

10. What is git clone used for?

Show answer `git clone ` copies a remote repository to your local machine, including all branches, tags, and commit history.
Example: `git clone https://github.com/user/repo.git`. Use `--depth 1` for a shallow clone (latest commit only) to save time on large repos. Add `--branch ` to clone a specific branch.

11. What is Git and what is it used for?

Show answer Git is a distributed version control system used to track changes in source code during software development, enabling multiple developers to collaborate.

12. What does git commit -m "message" do?

Show answer `git commit -m "message"` creates a new commit containing all staged changes with the given message. The commit records a snapshot of the staging area, author, timestamp, and parent commit SHA.
Best practice: write imperative messages like 'Add login validation' not 'Added login validation'. Use `-m` for one-liners only.

13. What command shows the commit history?

Show answer `git log`. Useful flags: `--oneline` for compact output, `--graph` for branch topology, `-n 5` for last 5 commits, `--author=name` to filter by author.

14. What is a "commit hash"?

Show answer A commit hash is a unique 40-character SHA-1 identifier computed from the commit's content, parent, author, and timestamp. You can reference commits by their first 7-8 characters (e.g., `git show abc1234`).
Gotcha: SHA-1 is deterministic — identical content always produces the same hash, enabling integrity verification.

15. True or False? A branch is basically a simple pointer to a commit.

Show answer TRUE.

Git branches are:
- Just a pointer (40-byte SHA reference)
- Stored in .git/refs/heads/
- Very lightweight to create
- No copying of files

When you create branch:
- Git creates a file with commit SHA
- That's it - no data copied
- Example: .git/refs/heads/feature → abc123...

When you commit on branch:
- Branch pointer moves to new commit
- Previous commits stay linked

HEAD:
- Points to current branch
- Or directly to commit (detached HEAD)

Why this matters:
- Branching is cheap
- Create many branches freely
- Merging is about commit history, not files

16. What is the "staging area" or "index"?

Show answer The staging area (also called the index) is a temporary holding zone where changes are assembled before committing. `git add` moves changes into the staging area; `git commit` records the staged snapshot. This two-step process lets you craft precise commits by staging only related changes, even from the same file using `git add -p`.

17. What is the purpose of a .gitignore file?

Show answer It specifies intentionally untracked files that Git should ignore (not show as untracked or stage/commit), such as build artifacts or secrets.

18. How do you check the status of your current repository?

Show answer `git status` — shows the current state of the working tree and staging area: which files are modified, staged for commit, untracked, or ignored. It also indicates the current branch and its relationship to the upstream remote.

19. What is a "repository"?

Show answer A repository is a data structure that stores project files along with their complete change history, branches, and tags. Git repositories include a `.git` directory containing the object database (blobs, trees, commits), refs, and config. Repositories can be local (on disk) or remote (hosted on GitHub, GitLab, etc.).

20. What is a "conflict"?

Show answer A conflict occurs when two branches modify the same line in a file and Git cannot automatically merge them. Git marks conflicts with `<<<<<<<`, `=======`, and `>>>>>>>` markers in the file. Resolve by editing the file, removing markers, then `git add` and `git commit`. Use `git mergetool` for a visual diff tool.

21. How do you edit the last commit message?

Show answer `git commit --amend` replaces the last commit with a new one, letting you fix the message or add forgotten changes. Stage any additional files first with `git add`, then run `git commit --amend`.
Gotcha: amending rewrites the commit hash — never amend commits already pushed to a shared branch without force-pushing.

22. How do you create a new branch?

Show answer `git branch ` creates a new branch pointing to the current commit but does not switch to it. Use `git checkout -b ` or `git switch -c ` to create and switch in one step.
Gotcha: the new branch shares history with its parent — divergence only happens when you make new commits.

23. What does git clone do?

Show answer `git clone ` creates a local copy of a remote repository including all commits, branches, and tags. It automatically sets up 'origin' as the remote and checks out the default branch. Use `--depth 1` for shallow clones (faster, less disk) or `--bare` for server-side repositories without a working tree.

24. True or False? When you git checkout some_branch, git updates HEAD to point to that branch.

Show answer TRUE.

What checkout does:
1. Updates HEAD to point to branch
2. Updates working directory to match
3. Updates index (staging area)

HEAD file:
- .git/HEAD contains: ref: refs/heads/some_branch
- This is how Git knows current branch

Example:
Before: HEAD → main
git checkout feature
After: HEAD → feature

Detached HEAD:
- git checkout
- HEAD points directly to commit, not branch
- .git/HEAD contains SHA, not ref

Modern alternative:
- git switch some_branch (for switching)
- git restore (for file operations)

25. What does git init do?

Show answer `git init` creates a new `.git` directory in the current folder, initializing an empty Git repository. This sets up the object database, refs, HEAD, and config files. Use `git init --bare` for server-side repositories that receive pushes but have no working tree. The repo starts with no commits.

26. What does git blame do?

Show answer `git blame ` annotates each line with the commit hash, author, and date of its last modification. Useful for tracing who introduced a specific change and why. Use `git blame -L 10,20 ` to limit to specific lines. Add `-w` to ignore whitespace changes that obscure the real author.

27. How do you delete a local branch?

Show answer `git branch -d ` deletes a local branch that has been fully merged. Use `-D` (capital D) to force-delete an unmerged branch and discard its unique commits.
Gotcha: `-d` is safe — Git refuses if the branch has unmerged work. `-D` is destructive and skips that check.

28. What is a merge in version control and how does it work?

Show answer A merge combines changes from one branch into another, creating a merge commit with two parent commits. Fast-forward merges happen when the target branch has no new commits — Git simply moves the pointer forward. Use `--no-ff` to always create a merge commit for clearer history. Conflicts require manual resolution before completing.

29. What is the difference between Git and GitHub?

Show answer Git is a distributed version control system (the tool) that runs locally. GitHub is a cloud hosting platform for Git repositories that adds collaboration features: pull requests, issues, CI/CD (Actions), code review, and access control. Alternatives to GitHub include GitLab, Bitbucket, and Gitea.

30. How do you temporarily save uncommitted changes?

Show answer `git stash`. It pushes uncommitted changes onto a stack. `git stash pop` restores them. `git stash list` shows all stashes. Useful when switching branches mid-work.

31. How do you create a new Git repository?

Show answer `git init` in a project directory creates a new Git repository by setting up the `.git` directory structure. Then add files with `git add .` and create the first commit with `git commit -m 'Initial commit'`. To connect to a remote: `git remote add origin ` then `git push -u origin main`.

32. How do you create and switch to a new branch in Git?

Show answer `git checkout -b ` creates a new branch and switches to it in one command. Modern equivalent: `git switch -c `. The new branch starts from your current HEAD. To branch from a specific commit: `git checkout -b `. Use descriptive names like `feature/add-login` or `fix/null-pointer`.

33. What does git push do?

Show answer `git push` uploads your local commits to a remote repository, making them available to others. First push requires `-u` flag: `git push -u origin main` sets the upstream tracking branch.
Gotcha: push is rejected if the remote has commits you don't have — `git pull --rebase` first, then push again.

34. How do you stage changes for commit in Git?

Show answer Using git add (or git add . to add all changes), which moves changes into the staging area (index) to be committed.

35. What is a "Pull Request"?

Show answer A Pull Request (PR) is a request to merge code from one branch into another, enabling code review before merging. PRs show the diff, allow inline comments, run CI checks, and require approvals. Despite the name, a PR is a 'push' of your changes requesting review — GitHub, GitLab (Merge Request), and Bitbucket all support them.

36. How do you record changes to the repository?

Show answer `git commit -m "message"` records staged changes as a new commit in the repository history. Each commit stores a snapshot, parent reference, author, and timestamp.
Best practice: stage specific files with `git add ` rather than `git add .` to keep commits focused. Write imperative messages: 'Add feature' not 'Added feature'.

37. What is the difference between git reset and git revert?

Show answer

`git revert` creates a new commit which undoes the changes from last commit.

`git reset` depends on the usage, can modify the index or change the commit which the branch head
is currently pointing at.

38. How do you switch between branches?

Show answer `git checkout ` or the modern `git switch ` changes your working directory to the specified branch. Git updates files to match the branch's latest commit.
Gotcha: uncommitted changes may be carried over or may cause a conflict — commit or stash first. Use `git switch -` to toggle back to the previous branch.

39. What are the four object types in Git's content-addressed database?

Show answer Git's content-addressed database has four object types: blob (stores file contents), tree (directory listing mapping names to blob/tree SHAs), commit (snapshot referencing a tree, parent commits, author, and message), and tag (named pointer to a commit with optional metadata and GPG signature). All are identified by their SHA-1 hash.

40. How do you stop tracking a file that was accidentally committed?

Show answer git rm --cached removes it from the index (staging area) without deleting the working copy. Then add the path to .gitignore to prevent re-adding.

🟡 Medium (83)

1. How can you see which changes have done before committing them?

Show answer `git diff` shows unstaged changes. `git diff --staged` (or `--cached`) shows what's staged for the next commit. Both let you review before committing.

2. If git status has to run diff on all the files in the HEAD commit to those in staging area/index and another one on staging area/index and working directory, how is it fairly fast?

Show answer One reason is about the structure of the index, commits, etc.

- Every file in a commit is stored in tree object
- The index is then a flattened structure of tree objects
- All files in the index have pre-computed hashes
- The diff operation then, is comparing the hashes

Another reason is caching

- Index caches information on working directory
- When Git has the information for certain file cached, there is no need to look at the working directory file

3. Explain the purpose of the .gitignore file.

Show answer The .gitignore file tells Git which files and directories it should not track or include in version control. It's useful for excluding build artifacts, log files, and other things that shouldn't be in the repository.

4. Explain the Git three-stage workflow (working directory, staging area, and repository)

Show answer Git has three stages for tracking changes:

1. **Working Directory**: Your local filesystem where you edit files. Changes here are 'modified' but not yet tracked
2. **Staging Area (Index)**: A holding area where you selectively prepare changes for commit using `git add`. Lets you group related changes into logical commits
3. **Repository (.git)**: The permanent record. `git commit` saves staged changes as a new snapshot with a message

Flow: Edit files -> `git add` (stage) -> `git commit` (save to history). This three-stage design lets you craft clean, intentional commits rather than saving everything at once.

5. How do you know if a certain directory is a git repository?

Show answer Check if there is a `.git` directory in the project root. Run `ls -la .git` or `git rev-parse --is-inside-work-tree` which returns 'true' if you're inside a git repo.
Gotcha: bare repositories (used as central hubs) have no working tree — use `git rev-parse --is-bare-repository` instead.

6. You have files in your repository you don't want Git to ever track them. What should you be doing to avoid ever tracking them?

Show answer Add file patterns to `.gitignore` to prevent Git from tracking them. Example patterns: `*.log`, `node_modules/`, `.env`. Files already tracked are not affected — use `git rm --cached ` to untrack them first.
Gotcha: `.gitignore` only works for untracked files, not files already in the index.

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

Show answer git fetch downloads new commits from a remote repository to your local repo (but doesn't merge them), whereas git pull fetches and immediately merges (or rebases) them into your current branch.

8. What is a "Git bundle"?

Show answer A Git bundle is a single file containing repository data (commits, branches, tags) that can be transferred offline. Create with `git bundle create repo.bundle --all`. Useful for sneakernet transfers or air-gapped environments. Import with `git clone repo.bundle` or `git fetch repo.bundle`.

9. Which one is faster? git diff-index HEAD or git diff HEAD

Show answer `git diff-index` is faster but to be fair, it's because it does less. `git diff index` won't look at the content,
only metadata like timestamps.

10. What is git tag -a used for?

Show answer `git tag -a v1.0 -m 'Release 1.0'` creates an annotated tag with a message, tagger name, date, and optional GPG signature. Unlike lightweight tags (`git tag v1.0`), annotated tags are full Git objects stored in the database. Use annotated tags for releases; they show up in `git describe` and can be verified.

11. In what situations are you using git rebase?

Show answer Suppose a team is working on a `feature` branch that is coming from the `main` branch of the repo. At a point, where the feature development is done, and finally we wish to merge the feature branch into the main branch without keeping the history of the commits made in the feature branch, a `git rebase` will be helpful.

12. What is a merge conflict in Git?

Show answer A situation where two commits (usually from different branches) have changed the same lines in a file, requiring manual resolution before merging.

13. You've created new files in your repository. How to make sure Git tracks them?

Show answer `git add FILES` stages files for the next commit. Use `git add .` for all changes in the current directory, or `git add -p` to interactively stage individual hunks within files.
Gotcha: `git add` only snapshots the file at that moment — further edits require another `git add`.

14. What is git reset (in general)?

Show answer It moves the current branch pointer to a specified commit. For example, git reset --hard HEAD~1 will move HEAD back one commit and discard that commit's changes. (Different options: --soft, --mixed, --hard determine what happens to index and working tree).

15. You have two branches - main and devel. How do you merge devel into main?

Show answer `git checkout main && git merge devel && git push origin main`. This creates a merge commit combining devel's changes into main. If there are conflicts, Git marks them in the files with `<<<<<<<`/`=======`/`>>>>>>>` markers — resolve, then `git add` and `git commit`. Use `--no-ff` to always create a merge commit for clearer history.

16. What is a Git rebase, and when might you use it?

Show answer Git rebase is like rewriting the history of your branch. It allows you to move your changes on top of another branch's changes, creating a cleaner and more straightforward history. It's used when you want a linear history without many merge commits.

17. What is a Git repository?

Show answer A Git repository is like a special folder that stores all the files, their history, and the changes made to them. It's where Git keeps track of your project.

18. What is a Git pull request?

Show answer A pull request is a way to suggest changes from one branch (e.g., a feature branch) to another (e.g., the main branch). It's a request for someone to review and approve your changes.

19. What is a Git commit message, and why is it important?

Show answer A Git commit message is a short description of the changes you made in a commit. It's essential because it helps you and your collaborators understand the purpose of the change, making it easier to review and track changes over time.

20. What is a branch in Git?

Show answer A branch is like a separate path for your project's development. You can work on new features or fixes in a branch without affecting the main project until you decide to merge the changes.

21. Explain the difference between Git pull and Git fetch.

Show answer Git pull is like getting updates from a remote repository and immediately merging them into your local branch. Git fetch is like fetching updates from a remote repository, but it doesn't automatically merge them. You can decide later when to merge the fetched changes.

22. True or False? To remove a file from git but not from the filesystem, one should use git rm

Show answer The question is misleading. `git rm` removes from both git and filesystem. To remove from git tracking but keep the file locally, use `git rm --cached `

23. What is the purpose of git bisect?

Show answer `git bisect` uses binary search to efficiently find which commit introduced a bug. Start with `git bisect start`, then `git bisect bad` (current) and `git bisect good `. Git checks out the midpoint; you test and mark good/bad until the culprit is found. Automate with `git bisect run `.

24. How does git revert differ from git reset?

Show answer `git revert ` creates a new commit that undoes the specified commit's changes — safe for shared branches. `git reset ` moves HEAD backward, potentially discarding commits.
Key difference: revert preserves history (safe to push); reset rewrites history (dangerous on shared branches, requires force push).

25. You would like to move forth commit to the top. How would you achieve that?

Show answer Use `git rebase -onto` or interactive rebase to reorder commits.
Example: `git rebase -i HEAD~4` opens an editor where you can reorder lines to change commit order.
Gotcha: reordering rewrites commit hashes, so only do this on unpushed commits. On shared branches, prefer cherry-pick instead.

26. What does git revert do?

Show answer It creates a new commit that undoes the changes introduced by the specified commit, without rewriting history (useful for "undoing" a commit in a safe way).

27. Explain the following: git directory, working directory and staging area

Show answer This answer taken from [git-scm.com](https://git-scm.com/book/en/v1/Getting-Started-Git-Basics#_the_three_states)

"The Git directory is where Git stores the meta-data and object database for your project. This is the most important
part of Git, and it is what is copied when you clone a repository from another computer.

The working directory is a single checkout of one version of the project. These files are pulled out of the compressed
database in the Git directory and placed on disk for you to use or modify.

The staging area is a simple file, generally contained in your Git directory, that stores information about what will go
into your next commit. It's sometimes referred to as the index, but it's becoming standard to refer to it as the staging
area."

28. How do you remove a remote branch?

Show answer Delete a remote branch with `git push origin --delete ` or the older syntax `git push origin :`. This removes the branch on the remote but not locally — run `git branch -d ` to clean up the local copy. Use `git fetch --prune` to remove stale remote-tracking references.

29. How do you undo the last Git commit?

Show answer To undo the last commit, you can use the command git reset HEAD~1. This moves the branch pointer back to the previous commit, effectively removing the last commit. Be cautious when doing this because it discards changes.

30. How to list the current git references in a given repository?

Show answer `git show-ref` or `find .git/refs/` lists all references (branches, tags, remotes) in the repository. `git show-ref` is the preferred porcelain command as it works on all platforms. References are just files containing SHA-1 hashes — branches under `refs/heads/`, tags under `refs/tags/`, remotes under `refs/remotes/`.

31. What is a Git stash, and why would you use it?

Show answer A Git stash is like a temporary storage for changes you're not ready to commit. You can "stash" your work, switch to another branch, and later "pop" the stash to continue where you left off.

32. What is a "detached HEAD"?

Show answer A detached HEAD occurs when HEAD points directly to a commit SHA instead of a branch name. This happens after `git checkout ` or checking out a tag.
Gotcha: commits made in detached HEAD state are not on any branch and can be garbage-collected. To keep them, create a branch: `git checkout -b new-branch`.

33. What is git stash used for?

Show answer It temporarily shelves (saves) your uncommitted changes so you can work on something else, and you can later apply them with git stash pop.

34. What is the difference between Git commit and Git push?

Show answer When you "commit," you're saving your changes to your local repository. When you "push," you're sending those changes to a remote repository, making them available to others.

35. How to check if a file is tracked and if not, then track it?

Show answer There are different ways to check whether a file is tracked or not:
- `git ls-files ` -> exit code of 0 means it's tracked
- `git blame `
...

36. How can you revert a Git commit after it has been pushed to a remote repository?

Show answer To revert a commit that has already been pushed to a remote repository, you can use the git revert command. It creates a new commit that undoes the changes made by the original commit without removing it from the history.

37. How do you discard local commits?

Show answer `git reset HEAD~1` removes the last commit but keeps changes in your working directory (mixed mode). `git reset --hard HEAD~1` discards both the commit and all changes permanently.
Gotcha: `--hard` is destructive and cannot be undone easily. If you already pushed, use `git revert` instead to avoid rewriting shared history.

38. What are the three modes of git reset?

Show answer The three modes of `git reset` are: `--soft` (moves HEAD, keeps changes staged), `--mixed` (moves HEAD, unstages changes but keeps them in working directory — this is the default), and `--hard` (moves HEAD, discards all changes permanently). Mnemonic: Soft=Safe(staged), Mixed=Middle(unstaged), Hard=Hazardous(gone).

39. Explain what the file gitignore is used for

Show answer The purpose of `gitignore` files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached.

40. What is the difference between git rebase and git merge?

Show answer Both integrate changes from one branch into another, but they create
different history structures.

Merge:
- Creates a new "merge commit" with two parents
- Preserves complete history and branch structure
- Non-destructive: doesn't change existing commits
- Shows when branches diverged and converged

```\n A---B---C feature\n / \\nD---E---F---G---M main (M is merge commit)\n```

41. What is Git branching strategy, and why is it important?

Show answer A Git branching strategy is a set of rules for creating, naming, and managing branches. It's essential for organized and collaborative development, making it clear which branches are for new features, bug fixes, or releases.

42. What does git diff show and what are its common options?

Show answer `git diff` compares working directory vs staging area by default. Key options: `--staged` compares staging vs last commit, `--stat` shows file-level summary, `git diff branch1..branch2` compares branches, and `git diff HEAD~3..HEAD` shows last 3 commits. Add `--name-only` to list changed files without showing the actual diff content.

43. What git status does?

Show answer `git status` helps you to understand the tracking status of files in your repository. Focusing on working directory and staging area - you can learn which changes were made in the working directory, which changes are in the staging area and in general, whether files are being tracked or not.

44. What is Git branching and merging, and how do they work?

Show answer Git branching is like creating different paths for your project's development. You can make changes separately in each branch. Merging is combining the changes from one branch back into another, creating a unified project with the new features or fixes.

45. What is Git blame (or annotate), and how can it be helpful?

Show answer Git blame is like a detective tool for finding out who made changes to a specific line in a file and when. It's useful for tracking down the origin of changes, understanding the context, and identifying who to ask questions if needed.

46. How do you delete a remote branch?

Show answer `git push origin --delete ` removes a branch from the remote repository. The local branch still exists — delete it separately with `git branch -d `.
Gotcha: other team members' stale remote-tracking refs remain until they run `git fetch --prune` or `git remote prune origin`.

47. A development team in your organization is using a monorepo and it's became quite big, including hundred thousands of files. They say running many git operations is taking a lot of time to run (like git status for example). Why does that happen and what can you do in order to help them?

Show answer Many Git operations relate to filesystem state. `git status` compares HEAD vs staging area vs working directory. Key state-related commands:

- `git status`: Shows modified, staged, and untracked files
- `git stash`: Temporarily shelves working directory changes
- `git checkout`: Switches branches or restores files
- `git reset`: Moves HEAD and optionally updates staging/working directory
- `git clean`: Removes untracked files

Understanding the three trees (HEAD, index, working directory) is essential for mastering these commands.

48. What is a Git remote?

Show answer A Git remote is a reference to a repository hosted on a server. You use it to interact with a repository on platforms like GitHub or GitLab. It allows you to fetch and push changes to and from the remote repository.

49. How to resolve git merge conflicts?

Show answer
First, you open the files which are in conflict and identify what are the conflicts.
Next, based on what is accepted in your company or team, you either discuss with your
colleagues on the conflicts or resolve them by yourself
After resolving the conflicts, you add the files with `git add `
Finally, you run `git rebase --continue`

50. Describe shortly what happens behind the scenes when you run git branch

Show answer Git runs update-ref to add the SHA-1 of the last commit of the branch you're on into the new branch you would like to create

51. What is a "bare repository"?

Show answer A bare repository has no working directory — it contains only the `.git` internals (objects, refs, HEAD). Created with `git init --bare`, it is used as a central hub that developers push to and pull from. You cannot edit files or run `git status` in a bare repo. GitHub/GitLab store all repos as bare repositories.

52. What are some Git anti-patterns? Things that you shouldn't do

Show answer Common Git anti-patterns: committing too infrequently (huge diffs that are hard to review), committing secrets or credentials, using `git add .` without reviewing changes, force-pushing to shared branches, and poor commit messages like 'fix' or 'update'. Also avoid storing large binaries — use Git LFS instead.

53. How do you revert a specific file to previous commit?

Show answer `git checkout HEAD~1 -- /path/to/file` restores a specific file to its state in the previous commit. Replace `HEAD~1` with any commit hash or ref. The file is placed in both the working directory and staging area. Modern alternative: `git restore --source=HEAD~1 /path/to/file`.
Gotcha: this overwrites your current file without warning.

54. What is a Git tag, and why is it used?

Show answer A Git tag is like a label for a specific commit, often used to mark significant milestones or releases. It helps you refer to a particular version of your project without having to remember long commit hashes. is a reference point or marker that is used to label a specific commit in a Git repository. Tags are typically used to mark important or significant points in a project's history, such as releases, milestones, or specific versions of the software. They provide a human-readable and permanent way to reference and identify a particular commit.

55. Describe how git status works

Show answer `git status` internally runs `git diff` twice: first comparing HEAD to the staging area (showing staged changes), then comparing the staging area to the working directory (showing unstaged changes). Files not in either diff but present on disk appear as untracked. Use `-s` for short format or `-b` to include branch info.

56. How do you resolve a Git merge conflict?

Show answer To resolve a merge conflict, you need to review the conflicting changes in the affected files and decide which ones to keep. Once resolved, you save the changes, mark the file as resolved, and complete the merge.

57. How do you discard local file changes? (before commit)

Show answer `git checkout -- ` discards unstaged changes in the working directory, reverting to the staged version. Modern equivalent: `git restore `.
Gotcha: this is permanent — the discarded changes cannot be recovered since they were never committed. Consider `git stash` if unsure.

58. What unstaged means in regards to Git?

Show answer A file that is in the working directory but is not in the HEAD nor in the staging area is referred to as "unstaged".

59. How to squash last two commits?

Show answer Use interactive rebase to combine commits:

Method 1 - Interactive rebase:
git rebase -i HEAD~2

In editor, change second commit from 'pick' to 'squash':
pick abc123 First commit
squash def456 Second commit

Save and edit combined commit message.

Method 2 - Soft reset:
git reset --soft HEAD~2
git commit -m "Combined commit message"

Method 3 - fixup (auto-discard message):
git rebase -i HEAD~2
pick abc123 First commit
fixup def456 Second commit

Note:
- Don't squash already-pushed commits (rewrites history)
- Use --force-with-lease if must push after squash
- Only squash local/unpushed commits

60. Explain Git cherry-pick and when it's useful.

Show answer Git cherry-pick is like picking a specific commit from one branch and applying it to another branch. It's handy when you want to include a particular change from one branch into another without merging the entire branch.

61. You have two branches - main and devel. How do you make sure devel is in sync with main?

Show answer To sync devel with main: `git checkout main && git pull && git checkout devel && git merge main`. This brings main's latest changes into devel via a merge commit. Alternative: `git checkout devel && git rebase main` for a linear history, but only if devel is not shared. Always pull main first to get the latest remote changes.

62. What does git rebase do?

Show answer `git rebase` reapplies your branch's commits on top of another base commit, creating a clean linear history.
Example: `git checkout feature && git rebase main` replays feature commits after main's HEAD.
Gotcha: rebase rewrites commit hashes — never rebase commits already pushed to a shared branch unless you coordinate with the team.

63. What is "squashing" in a merge?

Show answer Squashing combines multiple commits into a single commit during a merge, simplifying history. Use `git merge --squash ` to stage all changes from the branch as one commit. Common in PR workflows: a feature branch with 20 WIP commits becomes one clean commit on main.
Gotcha: the original commits are lost from the main branch history.

64. What merge strategies are you familiar with?

Show answer Mentioning two or three should be enough and it's probably good to mention that 'recursive' is the default one.

recursive
resolve
ours
theirs

This page explains it the best: https://git-scm.com/docs/merge-strategies

65. What command removes untracked files from the working directory?

Show answer `git clean`. Use `-n` for dry run, `-f` to force delete, `-fd` to include directories. Add `-x` to also remove gitignored files. Always dry-run first.

66. What is Git, and how does it differ from other version control systems?

Show answer Git is a version control system that helps you keep track of changes in your project's files. Unlike other systems, Git stores snapshots of your project instead of just the differences between versions.

67. What is git cherry-pick?

Show answer `git cherry-pick ` applies a specific commit's changes to your current branch as a new commit. Useful for applying a hotfix from one branch to another without merging the entire branch.
Gotcha: cherry-pick duplicates the commit (new hash), so the same change exists in two places — use sparingly to avoid confusion.

68. Explain Git octopus merge

Show answer Probably good to mention that it's:

- It's good for cases of merging more than one branch (and also the default of such use cases)
- It's primarily meant for bundling topic branches together

This is a great article about Octopus merge: http://www.freblogg.com/2016/12/git-octopus-merge.html

69. By which other Git commands does git diff used?

Show answer The diff mechanism used by `git status` to perform a comparison and let the user know which files are being tracked

70. What's is the branch strategy (flow) you know?

Show answer Major branching strategies: Git Flow (develop/release/hotfix branches — complex, suited for versioned releases), GitHub Flow (feature branches off main, deploy on merge — simpler), Trunk-Based Development (short-lived branches, continuous integration to main — fastest), and GitLab Flow (environment branches like staging/production). Choose based on release cadence and team size.

71. How do you merge one branch into another?

Show answer Check out the target branch (e.g., main) and run git merge to integrate changes from the other branch into the current one.

72. When you run git branch how does Git know the SHA-1 of the last commit?

Show answer Git reads the HEAD file at `.git/HEAD`, which typically contains `ref: refs/heads/main`. It then reads the SHA-1 from that branch ref file (e.g., `.git/refs/heads/main`). This indirection is what makes branch switching lightweight — only the HEAD pointer changes, not the commit data.

73. Are you familiar with gitattributes? When would you use it?

Show answer gitattributes allow you to define attributes per pathname or path pattern.

You can use it for example to control endlines in files. In Windows and Unix based systems, you have different characters for new lines (\r
and
accordingly). So using gitattributes we can align it for both Windows and Unix with `* text=auto` in .gitattributes for anyone working with git. This is way, if you use the Git project in Windows you'll get \r
and if you are using Unix or Linux, you'll get
.

74. What is git reflog and how does it help recover lost commits?

Show answer `git reflog` records every change to HEAD in the local repository, including commits, resets, rebases, and checkouts. It is your safety net for recovering lost commits after a bad `git reset --hard` or accidental branch deletion.
Example: `git reflog` then `git checkout `. Entries expire after 90 days by default.

75. Does Git store commits as diffs or snapshots?

Show answer Git stores each commit as a snapshot of the full tree of blobs and trees. Diffs are computed on demand for display. Pack files use delta compression for storage efficiency, but the logical model is snapshots.

76. How do you inspect the raw contents of a Git object?

Show answer git cat-file -p prints the object's contents in a human-readable form. Works for blobs, trees, commits, and tags. This is a plumbing command useful for understanding Git internals.

77. Why can "up to date with origin/main" be misleading?

Show answer origin/main is a local cached ref updated only on fetch. If you have not fetched recently, your origin/main may be stale. Always git fetch before assuming remote state.

78. What is the difference between a fast-forward merge and a true merge?

Show answer A fast-forward merge simply moves the branch pointer forward (no divergence existed). A true merge creates a merge commit with two parents, combining divergent histories. Fast-forward leaves a linear history.

79. What moves with each mode of git reset?

Show answer --soft: moves branch ref only (index and working tree unchanged).
--mixed (default): moves branch ref and resets index (working tree unchanged).
--hard: moves branch ref, resets index, and resets working tree. Data loss risk.

80. How do you move a commit from the wrong branch to the correct one?

Show answer 1. Note the commit SHA.
2. Switch to the correct branch.
3. git cherry-pick .
4. Switch back to the wrong branch.
5. git reset --hard HEAD~1 to remove it.
Only safe for unpushed commits.

81. What should you do when a rebase goes badly wrong?

Show answer Run git rebase --abort to return to the pre-rebase state. If you already completed the rebase, use git reflog to find the pre-rebase commit and git reset --hard to that SHA.

82. What is the first rule of Git disaster recovery?

Show answer Stop typing random commands. Name the current state with a branch if needed, inspect the graph and reflog, and prefer reversible steps. Distinguish local-only from already-pushed history before acting.

83. How do you save work done in a detached HEAD state?

Show answer Run git switch -c (or git checkout -b ) to create a new branch pointing to the current commit. Without naming it, the commits become unreachable when you switch away.

🔴 Hard (3)

1. How do you split one commit into two smaller commits?

Show answer git reset HEAD~1 (undoes the commit, keeps changes in working tree).
Then selectively stage with git add -p and commit the first group.
Then git add and commit the remainder.

2. Why might git diff show nothing even though you just changed a file?

Show answer Because the changes were already staged with git add. git diff shows working tree vs index. Use git diff --staged to see index vs HEAD. Understanding the three-state model (working tree, index, HEAD) is essential.

3. What does git add -p allow you to do and why is it valuable?

Show answer It lets you interactively stage individual hunks within a file rather than the entire file. This enables creating focused, logical commits from mixed working-tree changes.