Interview Questions for Web Developer - Git


Here are answers corresponding to the previously listed Git interview questions:


Basic Git Concepts:


1. What is Git and what are its advantages?

Answer: 

Git is a distributed version control system that allows multiple developers to work on the same project simultaneously. Its advantages include decentralized repository, fast performance, robust branching and merging, and better support for non-linear workflows.


2. Explain the difference between Git and GitHub.

Answer: 

Git is a version control system used for tracking changes in source code, while GitHub is a web-based hosting service for Git repositories facilitating collaboration, code review, and project management.


3. What is a commit in Git?

Answer: 

A commit is a snapshot of changes made to a Git repository. It represents a point in the project's history and includes a unique identifier (hash), author details, commit message, and changes made.


4. What is a repository in Git?

Answer: 

A Git repository is a storage location where a project's files and revision history are stored. It contains all project files, commits, branches, and configuration data.


5. What is the purpose of the `git clone` command?

Answer: 

The `git clone` command is used to create a copy of an existing Git repository to a local machine. It retrieves the entire repository, including all branches and commits.


Branching and Merging:


6. What is branching in Git and why is it important?

Answer: 

Branching in Git allows for creating separate lines of development, enabling developers to work on features or fixes independently without affecting the main codebase. It's crucial for parallel development and isolating changes.


7. What is the difference between `git merge` and `git rebase`?

Answer: 

`git merge` integrates changes from one branch into another, preserving the commit history of both branches. `git rebase` rewrites the commit history, placing the local commits on top of the updated branch, resulting in a linear history.


8. Explain what a merge conflict is and how to resolve it.

Answer: 

A merge conflict occurs when Git cannot automatically reconcile differences between two commits. To resolve it, the conflicting changes must be manually edited to create a final, acceptable version. After resolving conflicts, the changes are staged and committed.


Version Control:


9. What is the purpose of version control in Git?

Answer: 

Version control in Git tracks changes made to files over time, enabling multiple developers to collaborate efficiently, revert to previous versions, maintain a history of changes, and ensure code integrity.


10. What are the advantages of using branches in version control?

Answer: 

Branches allow for parallel development, experimentation with new features, bug fixes in isolation, easier collaboration, and the ability to merge changes back into the main codebase without affecting it.


Commands and Operations:


11. Explain the purpose of the `git status` command.

Answer: 

`git status` displays the current state of the repository, showing modified, staged, and untracked files. It provides an overview of changes made and their status in the working directory.


12. What is the use of the `git log` command?

Answer: 

`git log` displays the commit history of a repository, showing details such as commit hash, author, date, and commit message. It helps in tracking changes and reviewing the history of commits.


13. Explain the process of reverting changes using `git revert` and `git reset`.

Answer: 

`git revert` creates a new commit that undoes the changes made in a specific commit, while keeping a record of the original commit. `git reset` resets the current branch to a previous commit, potentially discarding commits and altering history.


14. How do you undo the last commit in Git?

Answer: 

To undo the last commit while keeping changes in the working directory, you can use `git reset HEAD~1`. To discard changes completely, you can use `git reset --hard HEAD~1`.


Collaboration:


15. What are Git remote repositories?

Answer: 

Git remote repositories are versions of a project hosted on remote servers (like GitHub, GitLab, or Bitbucket) that enable collaboration between multiple developers. They facilitate fetching, pushing, and pulling changes to and from the local repository.


16. Explain the process of pushing changes to a remote repository using `git push`.

Answer: 

`git push` uploads the locally committed changes to a remote repository. For example, `git push origin master` sends the changes from the local `master` branch to the remote repository named `origin`.


17. How do you pull changes from a remote repository using `git pull`?

Answer:

`git pull` fetches changes from the remote repository and merges them into the current branch. It is equivalent to running `git fetch` followed by `git merge` for the fetched branch.


Miscellaneous:


18. What is `.gitignore` and what is its purpose?

Answer:

`.gitignore` is a configuration file in Git that specifies files or patterns to be ignored (not tracked) by Git. It helps avoid committing unnecessary or sensitive files, such as build artifacts or credentials.


19. Explain the difference between a Git pull request and a Git merge request.

Answer: 

Git pull request and Git merge request are synonymous terms used interchangeably on different platforms like GitHub or GitLab. They both represent a request made by a developer to merge changes from one branch into another, usually the main branch, after code review and approval.


20. How do you create and apply Git patches?

Answer: 

To create a patch, use `git format-patch` followed by specifying the commit range or commit hash. To apply a patch, use `git apply` followed by the patch file's name.


During an interview, explaining these concepts with practical examples and your experience using Git can further solidify your understanding and expertise in the subject.