Git
A distributed version control system that records changes to project files. It lets you inspect earlier versions and work on changes independently before combining them with other people's work.
What Git does
Imagine changing how an app handles sign-in while a teammate updates its settings page. With Git, each of you can record and test your work on a separate branch, then bring the changes together. The resulting history lets you see which commit introduced a change and compare the project before and after it. Teams use Git for source code, tests, documentation, and text-based configuration.
Git is distributed: a normal clone gives you a local repository with project history, not just the latest files. You can inspect that history, create branches, and commit without a network connection. GitHub and GitLab, by contrast, are hosting platforms that add features such as code review and automated checks. You can use Git without either one.
From an edit to a commit
Git distinguishes between three places:
- The working tree contains the files you see and edit.
- The staging area, also called the index, holds what you intend to record next.
git addstages a file as it exists at that moment, so later edits aren't automatically included. - The repository stores commits and references to them. A commit records a snapshot of the staged project state and, after the first commit, links to one or more parent commits. A branch is a name that points to a commit. When you commit on that branch, the name moves forward to the new commit.
Example: publish a documentation fix
In an existing clone with a remote named origin, start a branch from your current commit, edit the README, and inspect what you've staged before committing:
git switch -c docs/setup
# Edit README.md
git add README.md
git diff --staged
git commit -m "Clarify setup instructions"
git push -u origin docs/setup
The commit exists locally as soon as git commit succeeds. The push publishes the branch to origin; it doesn't merge it into the team's default branch. On a hosting platform, you can then open a pull request to ask for review.
Common mistakes and interview scenarios
An empty git diff doesn't necessarily mean there's nothing to commit: by default it shows unstaged changes to tracked files. Use git diff --staged to review the proposed commit and git status for an overview that also includes untracked files.
Interviewers often ask how you'd undo a bad commit that's already on a shared branch. git revert is usually the right choice because it makes a new commit that reverses the change without rewriting shared history. Another common scenario is a merge conflict. Explain how you'd decide what the final file should contain, check that it works, and stage the resolved file before completing the merge.