What is Git?
Git is a distributed version control system used to track changes in files, especially source code. Each developer can have a complete local repository containing project history, branches, and commits.
Git & GitHub interview questions 1-25: Git Fundamentals, Repository, Working Tree, Staging, Commits & Branch Basics.
Open a question for the full answer, code and interview guidance.
Git is a distributed version control system used to track changes in files, especially source code. Each developer can have a complete local repository containing project history, branches, and commits.
GitHub is a cloud-based platform for hosting Git repositories and collaborating on software projects. It adds services around Git such as pull requests, issue tracking, code review, repository permissions, Actions workflows, releases, and project management.
Git is the version control system that records project history and runs on a developer's computer. GitHub is an online hosting and collaboration platform for Git repositories.
A Git repository is a project directory whose history and version-control metadata are managed by Git. A normal local repository contains the working files and a hidden .git directory that stores objects, references, configuration, and other Git metadata.
The working directory, also called the working tree, is the checked-out set of project files that you currently view and edit. Changes made here are not automatically included in the next commit.
The staging area, also called the index, is the intermediate snapshot that defines what will go into the next commit. git add copies selected changes into this staging area.
A commit is a recorded snapshot of the staged project state together with metadata such as author information, timestamp, message, and a reference to parent commit history. Git identifies commits using object IDs.
A common Git workflow is: modify files in the working directory, inspect changes, stage the intended changes, commit them locally, and then push commits to a remote repository when collaboration or backup is required.
git init creates a new Git repository in the current directory, or in a specified directory. It creates the .git metadata directory that Git uses to store repository history and configuration.
git clone creates a local copy of an existing repository. It normally downloads the repository history, creates a working tree, and configures the source repository as a remote named origin.
git status reports the current state of the working tree and staging area. It shows information such as the current branch, staged changes, unstaged changes, untracked files, and whether the branch is ahead of or behind its upstream.
git add stages file content for the next commit. It can stage one file, selected paths, or a broader set of changes.
Both commands often stage a broad set of changes, but their path scope can matter depending on where the command is run and the Git version. git add -A explicitly stages additions, modifications, and deletions across the repository, while git add . uses the current pathspec and stages relevant changes under the current directory.
git commit creates a new commit from the content currently in the staging area. The commit becomes part of the current branch history.
A good commit message is concise, specific, and describes the purpose of the change. The subject should help another developer understand the history without opening every diff.
git log displays commit history. By default it shows information such as commit IDs, authors, dates, and messages, while options can produce compact graphs or filter the history.
git diff shows differences between Git states. Without arguments, it commonly shows unstaged changes in the working tree relative to the index. With --staged, it shows staged changes relative to the current commit.
An untracked file exists in the working tree but is not currently tracked by Git. Git reports it in git status until it is staged and committed or intentionally ignored.
A .gitignore file defines patterns for intentionally untracked files and directories that Git should generally ignore. Typical examples include build outputs, dependency caches, IDE-specific files, logs, and local configuration that should not enter version control.
Adding a tracked file to .gitignore is not enough because Git already knows about it. git rm --cached removes the path from the index while leaving the working copy in place; the removal is then committed.
HEAD is Git's reference to the currently checked-out position. In normal branch-based work, HEAD points to the current branch, which in turn points to the latest commit on that branch.
A branch is a movable reference to a commit and represents a line of development. Branches allow developers to work on features, fixes, or experiments without immediately changing another branch such as main.
git switch can be used to move between branches, and git switch -c creates a new branch and switches to it. Older workflows commonly use git checkout for both file and branch operations.
git branch primarily creates, lists, renames, or deletes branch references, while git switch changes the currently checked-out branch and can also create a branch with -c.
A detached HEAD state occurs when HEAD points directly to a specific commit instead of to a branch reference. You can inspect or build the project in this state, and you can even create commits, but those commits are not automatically attached to a named branch.