Git Interview Questions & Answers

Git is a distributed version control system used to track changes in source code during software development.

GitHub is a cloud-based platform used to host Git repositories and collaborate on projects.

GitGitHub
Version control toolHosting platform
Works locallyWorks online
Tracks code changesEnables collaboration

A repository is a storage location that contains project files and version history.

Local RepositoryRemote Repository
Stored on local machineStored on server/GitHub
Used for local workUsed for team collaboration

A commit is a snapshot of changes saved to the repository.

Example:

git commit -m "Added login feature"
git pullgit fetch
Fetches and merges changesOnly downloads changes
Updates local branch automaticallyDoes not merge automatically
git mergegit rebase
Combines branches with merge commitRewrites commit history
Preserves historyCreates cleaner history

Branching allows developers to work on features independently without affecting the main code.

A merge conflict occurs when Git cannot automatically merge changes from different branches.

Steps:

  1. Open conflicted files
  2. Edit and resolve conflicts
  3. Save files
  4. Run : git add & git commit

The staging area is an intermediate area where changes are prepared before committing.

It adds changes to the staging area.

Example:

git add file.txt

It saves staged changes into the repository history.

It uploads local commits to the remote repository.

Example:

git push origin main

It copies an existing remote repository to the local machine.

Example:

git clone <repository-url>

It shows:

  • Modified files
  • Staged files
  • Untracked files

git branch feature

git branch -d feature

A pull request is a request to merge code changes into another branch

  • Fast performance
  • Distributed system
  • Easy branching
  • Strong version tracking
  • Collaboration support