WhatsApp WhatsApp

Career TipsInterview Questions

Top Git Interview Questions and Answers for Freshers

Top Git Interview Questions and Answers for Freshers

Git is the most widely used distributed version control system in software development. Companies such as Amazon, Microsoft, Google, IBM, Infosys, TCS, Accenture, Cognizant, Wipro, and Capgemini frequently ask Git-related interview questions because developers use Git daily for source code management, collaboration, branching, merging, and deployment. Understanding Git fundamentals is essential for Software Developers, DevOps Engineers, QA Engineers, Data Engineers, and Cloud Professionals.

Top Linux Commands and Interview Questions for Beginners


1. What is Git?

Answer:

Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to work on the same project simultaneously without overwriting each other’s work. Git maintains a complete history of changes, making it easy to track modifications and revert to previous versions when needed. It supports branching, merging, collaboration, and code review workflows. Git is widely used because it is fast, reliable, and scalable for projects of all sizes.

Example:

git init

This command initializes a new Git repository.


2. What is Version Control?

Answer:

Version control is a system that records changes made to files over time so that specific versions can be recalled later. It helps developers track modifications, collaborate effectively, and recover previous versions if mistakes occur. Version control systems maintain a history of code changes and improve project management. Git is one of the most popular version control systems used today. Without version control, managing large software projects would be extremely difficult.

Example:

If a bug is introduced in version 5 of an application, developers can revert to version 4 using version control history.


3. What is the Difference Between Git and GitHub?

Answer:

Git is a version control system used to track source code changes, while GitHub is a cloud-based platform used to host Git repositories. Git works locally on a developer’s machine, whereas GitHub provides collaboration features such as pull requests, issue tracking, and code reviews. Git can function without GitHub, but GitHub requires Git. Understanding this distinction is a common interview question for freshers.

Git GitHub
Version Control System Repository Hosting Platform
Works Locally Cloud-Based Service
Tracks Changes Enables Collaboration

4. What is a Repository in Git?

Answer:

A repository, often called a repo, is a storage location that contains project files, directories, and the complete history of changes. Repositories help developers manage source code efficiently and collaborate with team members. Repositories can be local (on a developer’s machine) or remote (on platforms like GitHub, GitLab, or Bitbucket). Every Git project starts with a repository. It acts as the central source of truth for the project.

Example:

git init

This creates a new repository in the current directory.


5. What is the Difference Between a Local Repository and a Remote Repository?

Answer:

A local repository exists on a developer’s computer and is used for development and testing. A remote repository is hosted on a server or cloud platform and enables team collaboration. Developers commit changes locally and then push them to a remote repository. Remote repositories serve as centralized storage accessible by multiple team members. Understanding local and remote repositories is fundamental to Git workflows.

Example:

  • Local Repository → Developer Laptop
  • Remote Repository → GitHub Repository

6. What is the git init Command?

Answer:

The git init command initializes a new Git repository in the current directory. It creates a hidden .git folder that stores repository metadata, commit history, configuration files, and tracking information. This command is usually the first step when starting a new project under version control. Once initialized, Git can track changes made to project files.

Example:

git init

7. What is the git clone Command?

Answer:

The git clone command creates a copy of an existing remote repository on a local machine. It downloads all files, commit history, branches, and configuration information. Developers use clone when joining an existing project. Cloning allows team members to begin contributing immediately while preserving the complete repository history.

Example:

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

8. What is the git status Command?

Answer:

The git status command displays the current state of the repository. It shows modified files, staged files, untracked files, and branch information. Developers use this command frequently to understand what changes will be included in the next commit. It helps prevent accidental commits and ensures proper version control management.

Example:

git status

9. What is the git add Command?

Answer:

The git add command moves changes from the working directory to the staging area. Only staged changes are included in the next commit. Developers use git add to selectively choose which modifications should be committed. This provides greater control over version history and commit organization.

Example:

git add file.txt

git add .

The second command stages all modified files.


10. What is the Staging Area in Git?

Answer:

The staging area is an intermediate area where changes are prepared before being committed to the repository. It allows developers to review and organize modifications before creating a commit. This helps maintain clean and meaningful commit histories. The staging area acts as a bridge between the working directory and the repository.

Workflow:

Working Directory
       ↓
Staging Area
       ↓
Repository

11. What is a Commit in Git?

Answer:

A commit is a snapshot of changes saved to the repository. Each commit has a unique identifier called a commit hash and includes metadata such as author information, timestamp, and commit message. Commits help developers track project evolution and revert changes if necessary. Writing meaningful commit messages is considered a best practice.

Example:

git commit -m "Added login functionality"

12. What is the git log Command?

Answer:

The git log command displays the commit history of a repository. It provides details such as commit IDs, author names, dates, and commit messages. Developers use it to review project history, investigate changes, and understand code evolution. It is particularly useful during debugging and auditing activities.

Example:

git log

13. What is a Branch in Git?

Answer:

A branch is an independent line of development that allows developers to work on features, bug fixes, or experiments without affecting the main codebase. Branching enables parallel development and reduces conflicts. Once development is complete, branches can be merged back into the main branch. Branching is one of Git’s most powerful features.

Example:

git branch feature-login

14. Why Are Branches Important?

Answer:

Branches allow developers to isolate changes and work independently without disrupting stable code. Teams can develop multiple features simultaneously while maintaining production stability. Branches also facilitate testing and experimentation before merging changes into the main branch. Most modern software development workflows rely heavily on branching strategies.

Example:

  • Main Branch → Production Code
  • Feature Branch → New Feature Development

15. How Do You Create a New Branch in Git?

Answer:

New branches are created using the git branch command. Developers typically create branches when starting a new feature or bug fix. Working in separate branches helps keep changes organized and reduces risks. After development is complete, branches can be merged into the main codebase.

Example:

git branch feature-payment

16. How Do You Switch Between Branches?

Answer:

The git checkout or git switch command is used to move between branches. Switching branches allows developers to work on different features without affecting other development efforts. Git updates the working directory to reflect the selected branch’s contents. Efficient branch management is essential in collaborative projects.

Example:

git checkout feature-payment

git switch feature-payment

Git Interview Questions and Answers for Freshers – Part 2

This section covers intermediate Git concepts such as merging, rebasing, stashing, resetting, reverting, fetching, pulling, merge conflicts, pull requests, and real-world Git scenarios. These topics are frequently asked in Software Developer, DevOps, Cloud Engineer, QA Automation, Data Engineer, and Full Stack Developer interviews.


17. What is Git Merge?

Answer:

Git Merge is used to combine changes from one branch into another branch. It helps integrate completed feature development into the main branch. During a merge, Git creates a new merge commit that combines the histories of both branches. Merging is one of the most common operations in collaborative development. Teams frequently merge feature branches after testing and code reviews are completed.

Example:

git checkout main

git merge feature-login

18. What is Git Rebase?

Answer:

Git Rebase is used to move or replay commits from one branch onto another branch. Unlike merge, rebase creates a cleaner and linear commit history. It makes project history easier to read and maintain. However, rebasing shared branches should be avoided because it rewrites commit history. Many organizations use rebase before merging feature branches.

Example:

git checkout feature-login

git rebase main

19. What is the Difference Between Merge and Rebase?

Answer:

Git Merge Git Rebase
Preserves history Rewrites history
Creates merge commit No merge commit
Safer for shared branches Better for clean history

Merge is preferred when preserving project history is important, while rebase is preferred for maintaining a cleaner commit timeline.


20. What is Git Fetch?

Answer:

Git Fetch downloads changes from a remote repository without modifying the local working branch. It allows developers to review updates before integrating them. Fetch is considered a safe operation because it only updates remote tracking branches. Developers often use fetch before deciding whether to merge incoming changes.

Example:

git fetch origin

21. What is Git Pull?

Answer:

Git Pull is used to fetch changes from a remote repository and immediately merge them into the current branch. It combines the functionality of git fetch and git merge. Pulling ensures that the local repository remains synchronized with the remote repository. Developers frequently pull updates before starting their work.

Example:

git pull origin main

22. What is the Difference Between Git Fetch and Git Pull?

Answer:

Git Fetch Git Pull
Downloads changes only Downloads and merges changes
No modification to working branch Updates current branch
Safer for review Faster synchronization

Fetch allows inspection before integration, while pull automatically updates the branch.


23. What is Git Stash?

Answer:

Git Stash temporarily saves uncommitted changes without creating a commit. It allows developers to switch branches or pull updates without losing ongoing work. Stashed changes can later be restored when needed. This feature is extremely useful when urgent tasks interrupt current development work.

Example:

git stash
 How LinkedIn is useful for students in case of Internships, jobs and materials 

24. How Do You View Stashed Changes?

Answer:

Git provides commands to view all stashed entries. Each stash is stored with an identifier and message. Reviewing stashes helps developers manage multiple unfinished tasks. This is especially useful when working on several features simultaneously.

Example:

git stash list

25. How Do You Restore Stashed Changes?

Answer:

Stashed changes can be restored using git stash apply or git stash pop. The apply command restores changes while keeping them in the stash list. The pop command restores changes and removes them from the stash. Choosing the appropriate command depends on whether the stash should be retained.

Example:

git stash pop

26. What is Git Reset?

Answer:

Git Reset is used to move the current branch pointer to a previous commit. It can modify the staging area and working directory depending on the reset mode used. Reset is often used to undo commits or remove unwanted changes. Developers should use reset carefully because it can permanently discard work.

Example:

git reset HEAD~1

27. What is Git Revert?

Answer:

Git Revert creates a new commit that reverses the changes introduced by a previous commit. Unlike reset, revert does not alter commit history. This makes it safer for shared repositories because other developers are not affected. Revert is commonly used in production environments to undo problematic changes.

Example:

git revert a1b2c3

28. What is the Difference Between Git Reset and Git Revert?

Answer:

Git Reset Git Revert
Removes commits Creates undo commit
Changes history Preserves history
Risky for shared branches Safe for shared repositories

29. What is a Merge Conflict?

Answer:

A merge conflict occurs when Git cannot automatically combine changes from different branches. This usually happens when multiple developers modify the same lines of code. Git marks conflicting sections and requires manual resolution. Understanding merge conflicts is essential because they frequently occur in collaborative projects.

Example:

Developer A modifies line 10 while Developer B modifies the same line in another branch.


30. How Do You Resolve a Merge Conflict?

Answer:

To resolve a merge conflict, developers review conflicting sections, decide which changes to keep, edit the files manually, and commit the resolved version. Proper communication among team members often helps resolve conflicts quickly. After resolving conflicts, Git records the merged state in a new commit.

Example:

git add resolved_file.txt

git commit

31. What is a Pull Request (PR)?

Answer:

A Pull Request is a feature provided by GitHub, GitLab, and Bitbucket that allows developers to propose code changes before merging them into the main branch. Pull requests facilitate code reviews, discussions, testing, and approval workflows. They improve code quality and collaboration within development teams.

Example:

A developer creates a PR after completing the login feature for review by senior developers.


32. Why Are Pull Requests Important?

Answer:

Pull requests improve software quality by enabling peer reviews before code reaches production. They help identify bugs, maintain coding standards, and encourage knowledge sharing among team members. Most modern software development teams require pull request approvals before merging code.

Benefits:

  • Code Review
  • Knowledge Sharing
  • Bug Detection
  • Quality Assurance

33. What is .gitignore?

Answer:

The .gitignore file specifies files and directories that Git should not track. It prevents unnecessary files such as logs, temporary files, build artifacts, and sensitive information from being committed. Proper use of .gitignore keeps repositories clean and secure.

Example:

*.log

node_modules/

.env

34. Scenario: You Accidentally Committed a Sensitive Password. What Would You Do?

Answer:

If a sensitive password is committed, the first step is to remove it from the code and rotate the compromised credentials immediately. Then, Git history may need to be rewritten using tools such as git filter-repo or BFG Repo Cleaner. Finally, the cleaned repository should be pushed and team members informed. Security incidents should always be handled promptly.

Real-Time Example:

An AWS access key accidentally committed to GitHub should be revoked immediately and replaced with a new key.


35. Scenario: A Team Member’s Changes Are Missing After a Merge. How Would You Investigate?

Answer:

I would review the commit history using git log, inspect merge commits, compare branches using git diff, and verify whether the changes were accidentally overwritten during conflict resolution. Understanding Git history is critical for troubleshooting collaboration issues. Careful analysis usually reveals where the missing changes occurred.

Useful Commands:

git log

git diff

git branch

git reflog

Most Frequently Asked Git Topics in Interviews

  • Git Basics
  • GitHub Fundamentals
  • Repositories
  • Commits
  • Branches
  • Merging
  • Rebasing
  • Stashing
  • Reset vs Revert
  • Fetch vs Pull
  • Merge Conflicts
  • Pull Requests
  • .gitignore
  • Git Workflows
  • Code Reviews
  • Version Control Best Practices
  • Git Troubleshooting
  • Real-Time Git Scenarios

Leave a Reply

Your email address will not be published. Required fields are marked *