Top Git Interview Questions and Answers

What is Git?

Git is a distributed version control system (DVCS) used for tracking changes in source code during software development. It's open-source, enabling collaboration among developers by managing different versions of code efficiently. Git allows multiple developers to work simultaneously on a project, merging their changes without overwriting each other's work.

Git's Programming Language

Git is written in C, a language chosen for its performance and efficiency.

Git Repositories

A Git repository is a directory containing all the files and metadata related to a project's version history. It's identified by the presence of a .git subdirectory.

Bare Git Repositories

A bare repository in Git contains only the version history; it doesn't include a working directory. It's primarily used as a central repository for collaboration.

Git Stash

The git stash command saves your uncommitted changes temporarily, allowing you to switch branches or perform other tasks without losing your work. You can then restore your changes later.

Git Stash Drop

The git stash drop command removes a stashed changeset. By default, it removes the most recent stash, but you can specify a changeset to be dropped using the stash name.

Advantages of Using Git

  • Version control; track changes over time.
  • Branching and merging; enables parallel development.
  • Collaboration; multiple developers can work together.
  • Distributed system; doesn't rely on a central server for all operations.
  • Efficient performance.

git push

The git push command uploads your local commits to a remote repository. This makes your changes available to other developers.

Branching in Git

Branching enables creating parallel versions of a project, allowing developers to work on new features or bug fixes without affecting the main codebase. Branches can be merged when ready.

git config

The git config command is used to configure various Git settings, such as user information, editor preferences, and repository behavior.

Index (Staging Area) in Git

The staging area (or index) is a temporary holding area for changes you're ready to commit. You add modified files to the staging area before creating a commit.

Conflicts in Git

Conflicts arise when merging branches with changes in the same parts of files. Git cannot automatically resolve them, requiring manual intervention by the developer to choose the correct changes.

git pull vs. git fetch

git pull git fetch
Fetches changes from a remote repository and merges them into the current branch. Fetches changes from a remote repository but doesn't merge them; saves changes locally. Requires a separate merge command.

Resolving Conflicts in Git

Resolving a conflict involves editing the affected files to combine the changes from different branches. Then use git add to stage the resolved files and git commit to create a new commit containing the merged changes.

git clone

The git clone command creates a local copy of a Git repository from a remote source.

git pull origin master

This command fetches changes from the master branch of the remote repository (origin) and merges them into your local master branch.

git commit -m "message"

The git commit command saves your changes to the local repository. The -m flag adds a commit message.

Why Git is Better than Subversion (SVN)

Git is a distributed version control system, meaning every developer has a full copy of the repository, unlike SVN which is centralized. This allows for offline work and faster operations. Git also offers more flexible branching and merging capabilities.

Commit Messages in Git

Commit messages describe the changes made in a commit. Clear and concise commit messages are essential for tracking changes and understanding the project's history.

Amending vs. Creating New Commits

It's generally better to create a new commit rather than amending an existing one, especially if the changes are substantial. Amending can make it harder to track changes and can lead to issues when collaborating.

Git Hooks

Git hooks are custom scripts that run automatically before or after Git events (e.g., commit, push). They are often used for code validation, testing, and other automation tasks.

Git vs. GitHub

Git GitHub
The version control system (the software). A web-based hosting service for Git repositories.

Reverting a Recently Pushed Commit

  1. Create a new commit that undoes the changes made in the bad commit (using git revert).
  2. Or, directly amend the bad commit (using git commit --amend) if the commit is recent and hasn't been shared yet with others.

Commit Object Contents

  • Snapshot of changes.
  • Pointer to parent commit(s).
  • Unique SHA-1 hash.

Git Branching Strategies

  • Feature Branching: Create branches for individual features.
  • Task Branching: Create branches for specific tasks.
  • Release Branching: Create branches for preparing releases.

Checking for Merged Branches

Use these Git commands:

  • git branch --merged: Lists branches merged into the current branch.
  • git branch --no-merged: Lists branches that have not been merged.

Fixing a Messed-Up Commit

Use git commit --amend to modify the last commit's message or contents. For more significant changes, create a new commit to revert or correct the previous one.

Git Repository Hosting Services

  • GitHub
  • GitLab
  • Bitbucket
  • Other services

Graphical Git Clients for Linux

  • Git Cola
  • SmartGit
  • Other clients

SubGit: A Subversion to Git Migration Tool

SubGit is a powerful tool that facilitates migrating from Subversion (SVN) to Git. It simplifies the transition process, allowing organizations to use Git's features while preserving their existing Subversion history and infrastructure. SubGit provides a smoother, more reliable migration compared to using git-svn.

Key Advantages of Using SubGit for Migration

  • Seamless Transition: Minimal disruption to existing workflows.
  • Preserves Subversion History: All your Subversion history is transferred to Git.
  • No Infrastructure Changes: You can continue using your existing Subversion infrastructure during and after migration.
  • Full Feature Support: Access all the features of both Git and Subversion.