git status

The git status command shows the current state of your working directory and staging area. It displays:

  • Modified files
  • Staged files
  • Untracked files

It is the quickest way to see what has changed before staging or committing.


Interview Answer

"I use git status to check the current state of my repository before every commit. It helps me identify modified, staged, and untracked files so that I commit only the intended changes."

Advertisement

What Does git status Show?

  • Files modified but not staged
  • Files staged for commit
  • Untracked files
  • Current branch
  • Merge conflicts (if any)

Example

 
git status
 

Typical Output

 
On branch feature/login

Changes not staged for commit:
    modified: LoginTest.java

Changes to be committed:
    new file: UserTest.java

Untracked files:
    config.properties
 

Real-Time Example

Before committing Selenium automation scripts, I always run git status to verify that only the required files are included in the commit.


git add

The git add command moves changes from the working directory to the staging area (index), preparing them for the next commit.

Only files added using git add will be included in the next commit.


Interview Answer

"I use git add to stage the required files before committing. It allows me to control exactly which changes are included in each commit."


Syntax

Stage a Specific File

 
git add LoginTest.java
 

Stage All Changes

 
git add .
 

What Does git add Do?

  • Stages modified files
  • Stages new files
  • Prepares changes for commit
  • Does not save anything permanently

Workflow

 
Working Directory
        │
        ▼
     git add
        │
        ▼
   Staging Area
 

Best Practice

Stage only the files related to a single logical change instead of blindly using git add ..


git commit

The git commit command saves the staged changes into the local Git repository, creating a permanent snapshot with a unique commit ID (hash).


Interview Answer

"After staging my changes, I use git commit with a meaningful commit message to permanently save them in the local repository. Every commit represents a logical unit of work."


Syntax

 
git add .

git commit -m "Added Selenium test for user registration"
 

Best Practices

Write meaningful commit messages.

Examples:

  • Feature: Added Login API automation
  • Fix: Resolved NullPointerException
  • Refactor: Improved utility methods

Commit Workflow

 
Working Directory
        │
        ▼
     git add
        │
        ▼
   Staging Area
        │
        ▼
    git commit
        │
        ▼
 Local Repository
 

Real-Time Example

In our API automation framework, I commit each completed API test case separately with descriptive messages, making it easy to track changes during code reviews.


git add vs git commit

Although these commands are used together, they perform different tasks.


Interview Answer

"git add stages changes for the next commit, while git commit permanently saves those staged changes in the local repository."


Comparison

Feature git add git commit
Purpose Stage changes Save changes permanently
Saves to Repository ❌ No ✅ Yes
Working Area Staging Area Local Repository
Creates Commit ❌ No ✅ Yes

Simple Analogy

Imagine online shopping:

  • git add → Add items to your shopping cart.
  • git commit → Complete the purchase.

Example

 
git add LoginTest.java

git commit -m "Added login test case"
 

git commit -m vs git commit --amend

Git provides two common ways to commit changes.


git commit -m

Creates a new commit.

 
git commit -m "Added Login API tests"
 

git commit --amend

Edits the most recent commit.

It can:

  • Change the commit message.
  • Add forgotten staged files.
  • Update the previous commit.
 
git commit --amend
 

Interview Answer

"I use git commit --amend when I need to correct the latest commit message or include a file I forgot to stage. I only use it before pushing because it rewrites commit history."


Comparison

Command Purpose
git commit -m Creates a new commit
git commit --amend Modifies the latest commit

Best Practice

Use --amend only if the commit has not been pushed to the remote repository.


git clone

The git clone command creates a complete local copy of an existing remote repository.

It downloads:

  • Source code
  • Branches
  • Commits
  • Tags
  • Complete Git history

Interview Answer

"I use git clone to download an existing project from GitHub or another Git server. It gives me a complete copy of the repository, including its version history."


Syntax

 
git clone <repository-url>
 

Example

 
git clone https://github.com/username/project.git
 

Clone into a Specific Folder

 
git clone https://github.com/username/project.git MyProject
 

What Happens After Cloning?

Git automatically:

  • Downloads the repository
  • Creates a local folder
  • Configures the remote as origin

Real-Time Example

When joining a new automation project, I first clone the repository, configure my Git identity, and create a feature branch before starting development.


git push

The git push command uploads local commits to the remote repository.


Interview Answer

"After verifying my code locally, I use git push to upload my commits to the shared repository so that other team members can review my changes."


Syntax

 
git push origin main
 

Push a Feature Branch

 
git push origin feature/login
 

Push a New Branch

 
git push --set-upstream origin feature/login
 

Workflow

 
Local Repository
        │
        ▼
     git push
        │
        ▼
Remote Repository
 

git pull

The git pull command downloads the latest changes from the remote repository and merges them into the current branch.

Internally, it performs:

 
git fetch
      +
git merge
 

Interview Answer

"Before starting new work or pushing my code, I perform a git pull to synchronize my branch with the latest remote changes and reduce merge conflicts."


Syntax

 
git pull origin main
 

Workflow

 
Remote Repository
        │
        ▼
    git fetch
        │
        ▼
    git merge
        │
        ▼
Local Branch Updated
 

Best Practice

Always pull before:

  • Starting work
  • Creating a Pull Request
  • Pushing changes

git fetch (git fetch vs git pull)

The git fetch command downloads the latest commits from the remote repository without merging them.


Interview Answer

"I use git fetch when I want to review remote changes before merging them into my local branch."


Syntax

 
git fetch origin
 

Review Downloaded Changes

 
git log HEAD..origin/main
 

Comparison

Feature git fetch git pull
Downloads Changes ✅ Yes ✅ Yes
Automatically Merges ❌ No ✅ Yes
Safe for Review ✅ Yes ❌ No

Workflow

git fetch

 
Remote Repository
        │
        ▼
Downloads Changes
        │
        ▼
Review Before Merge
 

git pull

 
Remote Repository
        │
        ▼
Fetch
        │
        ▼
Merge
        │
        ▼
Current Branch Updated
 

git remote -v

The git remote -v command displays all configured remote repositories along with their fetch and push URLs.


Interview Answer

"I use git remote -v to verify which remote repository my local project is connected to before pushing or pulling code."


Syntax

 
git remote -v
 

Sample Output

 
origin  https://github.com/company/project.git (fetch)

origin  https://github.com/company/project.git (push)
 

Why Use It?

  • Verify remote URLs
  • Check repository connections
  • Troubleshoot remote issues

git config

The git config command configures Git settings such as:

  • User name
  • Email
  • Default editor
  • Merge behavior
  • Other Git preferences

Configuration can be applied at three levels:

  • Local
  • Global
  • System

Interview Answer

"Before making commits, I configure my Git username and email using git config so that every commit is correctly associated with my identity."


Configure Username

 
git config --global user.name "Your Name"
 

Configure Email

 
git config --global user.email "you@example.com"
 

View Configuration

 
git config --list
 

Configure Default Editor

 
git config --global core.editor "vim"
 

Configuration Levels

Level Scope
Local Current repository only
Global Current user
System Entire machine

Real-Time Example

After installing Git on a new system, I configure my username and email using git config --global before cloning the automation project.


Frequently Asked Questions (FAQs)

1. What does git status do?

The git status command displays the current state of the working directory and staging area. It shows modified files, staged changes, untracked files, the current branch, and any merge conflicts.


2. What is the difference between git add and git commit?

  • git add stages files for the next commit.
  • git commit permanently saves the staged changes into the local Git repository.

3. What is the difference between git commit -m and git commit --amend?

  • git commit -m creates a new commit with the specified message.
  • git commit --amend modifies the most recent commit by updating its message or adding forgotten staged changes.

Use --amend only before pushing the commit.


4. What does git clone do?

The git clone command creates a complete local copy of a remote repository, including all branches, commits, tags, and version history. It also configures the remote repository as origin.


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

  • git fetch downloads remote changes without merging them.
  • git pull downloads the changes and immediately merges them into the current branch.

Use git fetch when you want to review changes before merging.


6. What is git remote -v?

The git remote -v command lists all configured remote repositories along with their fetch and push URLs. It helps verify where code will be pulled from or pushed to.


7. What is git config used for?

The git config command configures Git settings such as the user name, email address, default editor, merge behavior, and other preferences.

Configuration can be applied at:

  • Local – Current repository
  • Global – Current user
  • System – Entire machine

Using git config ensures Git records the correct author information and follows your preferred configuration.