Git Version Control Illustration

Sat Mar 08 - Written by: Samridha

Git QnA

Explore Git fundamentals, commands, version control concepts, and common Git interview questions for developers and testers.

Git Overview

What is Git?

Git is a distributed version control system that helps developers track and manage changes in their source code efficiently. It is widely used for collaboration in software projects.

Why Use Git?

  • Allows multiple developers to work simultaneously on a project.
  • Tracks changes and reverts to previous versions if needed.
  • Supports branching for feature development without affecting the main codebase.
  • Enables easy collaboration using remote repositories like GitHub, GitLab, or Bitbucket.

Key Git Concepts

  • Repository (Repo): A folder containing a project’s code and its version history.
  • Commit: A saved snapshot of code changes.
  • Branch: A separate line of development for features or fixes.
  • Merge: Combines changes from one branch into another.
  • Rebase: Moves commits from one branch to another to maintain a linear history.
  • Staging Area: Where changes are prepared before committing.
  • Remote Repository: A Git repo hosted on a cloud service.
  • Local Repository: A Git repo stored on your local system.

Setting Up Git

Configure Git

git config --global user.name "Samridha"
git config --global user.email "m@m.com"

# Git Commands Cheat Sheet

## Create a Repository
```bash
git init  # Initializes a new Git repository in the current directory

Clone a Repository

git clone <repo-url>  # Clones a repository from a remote source

Check Status of Changes

git status  # Shows the status of staged, unstaged, and untracked files

View Commit History

git log  # Displays commit history

Push Changes to Remote Repository

git push origin <branch_name>  # Pushes local changes to a remote branch

Pull Latest Changes from Remote Repository

git pull origin <branch_name>  # Fetches and merges updates from the remote branch

Working with Branches

Create and Switch to a New Branch

git branch <branch-name>  # Creates a new branch
git checkout <branch-name>  # Switches to the specified branch

Merge Branches

git checkout main  # Switch to the main branch
git merge <branch-name>  # Merges the specified branch into the main branch

Undoing Changes in a Branch

Revert a Commit

git revert <commit-id>  # Creates a new commit that reverts the specified commit

Reset a Commit (DANGEROUS)

git reset --hard <commit-id>  # Resets branch history to the specified commit

Saving and Restoring Uncommitted Work

Stash Changes

git stash  # Temporarily saves changes that are not ready for commit

Apply Stashed Changes

git stash pop  # Restores the most recent stashed changes

Rebase vs. Merge

Merge

  • Combines changes from another branch and creates a new commit.

Rebase

  • Moves the commits from one branch on top of another, making the history linear.
git checkout main  # Switch to main branch
git rebase feature-branch  # Apply feature branch commits on top of main branch

Git Overview

What is Git?

Git is a version control system used for tracking changes in source code. It helps developers collaborate efficiently and manage code history.

Why Use Git?

  • Tracks and manages changes to code.
  • Facilitates team collaboration in software development.
  • Supports branching for feature development.
  • Allows reverting to previous code versions if needed.

Common Git Terminology

  • git add - Adds the latest changes to staging before committing.
  • git commit - Stores a snapshot of the staged changes.
  • git push - Pushes local commits to the remote repository.
  • git branch - Creates a parallel version of the repository.
  • git clone - Creates a local copy of a remote repository.
  • repo (repository) - A project folder managed by Git.
  • git merge - Combines changes from one branch into another.
  • conflict - Occurs when two branches modify the same line of code.
  • git rebase - Moves commits from one branch to another to maintain a linear history.
  • remote repository - A Git repository hosted on a cloud service (e.g., GitHub).
  • local repository - A Git repository stored on your local system.
  • HEAD - A pointer to the latest commit in the current branch.

Setting Up Git

Configure Git

git config --global user.name "samridha"
git config --global user.email "m@m.com"

Initialize a Repository

git init  # Initializes a new Git repository in the current directory

Clone a Repository

git clone <repo-url>

Check Status of Changes

git status

View Commit History

git log

Push Changes to Remote Repository

git push origin <branch_name>

Pull Latest Changes from Remote Repository

git pull origin <branch_name>

Working with Branches

Create and Switch Branches

git branch <branch-name>  # Creates a new branch
git checkout <branch-name>  # Switches to the specified branch

Merge Branches

git checkout main  # Switch to the main branch
git merge <branch-name>  # Merges the specified branch into the main branch

Undoing Changes in a Branch

Revert a Commit

git revert <commit-id>  # Creates a new commit that reverts the specified commit

Reset a Commit (DANGEROUS)

git reset --hard <commit-id>  # Resets branch history to the specified commit

Saving and Restoring Uncommitted Work

Stash Changes

git stash  # Temporarily saves changes that are not ready for commit

Apply Stashed Changes

git stash pop  # Restores the most recent stashed changes

Rebase vs. Merge

Merge

  • Combines changes from another branch and creates a new commit.

Rebase

  • Moves the commits from one branch on top of another, making the history linear.
git checkout main  # Switch to main branch
git rebase feature-branch  # Apply feature branch commits on top of main branch

Git Interview Q&A

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

CommandDescription
git fetchDownloads changes from the remote repository but does not merge them.
git pullFetches and automatically merges the changes into your local branch.

Example:

git fetch origin
git diff main origin/main  # Compare local and remote versions
git merge  # Manually merge changes

2. What is commit squashing?

  • Squashing combines multiple commits into a single commit to keep history clean.
git rebase -i HEAD~4  # Squash the last 4 commits

3. What does a force push do?

  • A force push (git push origin main --force) overwrites the remote branch with your local branch.

4. How do you rename a branch?

git branch -m <old-name> <new-name>

5. What is Git Cherry Pick?

  • Cherry-picking allows applying a specific commit from one branch to another without merging the entire branch.
git cherry-pick <commit-id>

6. How would you find a specific commit?

git log --grep "commit message"

7. How to start an interactive rebase?

git rebase -i HEAD~4  # Rebase the last 4 commits interactively

8. How do you reset the last commit?

ScenarioCommand
Before pushinggit reset --soft HEAD~1 (Keeps changes unstaged)
After pushinggit revert HEAD (Creates a new commit to undo the last commit)

9. How does git revert work?

  • git revert creates a new commit that undoes the last commit without modifying history.

10. Difference between Git and SVN?

AspectGitSVN
ArchitectureDistributed Version Control System (DVCS)Centralized Version Control System (CVCS)
Offline WorkCan commit changes offlineRequires network access
BranchingLightweight and fastHeavyweight and slow
MergingEasier and supports rebaseComplex and less flexible

Summary

  • Git is a powerful distributed version control system.
  • It supports branching, merging, and collaboration across teams.
  • Commands like git add, git commit, git push, and git pull are essential for daily workflows.
  • Understanding rebase, cherry-picking, and interactive rebasing helps in managing cleaner histories.
  • Git interview questions often focus on version control concepts and best practices.