22 Most Common Git Interview Questions and Answers (2024)
Blog / 22 Most Common Git Interview Questions and Answers (2024)
Git is a distributed version control system that allows multiple developers to collaborate on a project, track changes to code, and manage different versions of the codebase.
Demonstrating knowledge of Git's advanced features in your interview can go a long way in impressing your interviewer.
This blog will bring you up to speed with the most important Git interview questions and ensure you nail your next interview.
What is Git, and how does it differ from other version control systems?
Junior
- Git is a distributed version control system that allows multiple developers to collaborate on a project, track changes to code, and manage different versions of the codebase.
- Unlike centralized version control systems (e.g., SVN), Git stores a complete copy of the repository on each developer's machine, enabling offline work and faster operations.
What are the key features of Git?
Junior
- Repository: A directory where Git tracks changes to files.
- Commit: A snapshot of the repository at a certain point in time.
- Branch: A separate line of development, used for isolating work on a specific feature or task.
- Merge: Integrating changes from one branch into another.
- Pull Request / Merge Request: A request to review and merge code changes from one branch to another, especially in online repositories like GitHub or GitLab.
- Clone: Making a local copy of a remote repository.
- Fork: Creating a personal copy of someone else's project.
- Push: Sending local repository changes to a remote repository.
- Pull: Fetching and integrating changes from a remote repository into the local repository.
- Staging Area: A place where changes are prepared before committing.
- HEAD: A pointer to the current branch or commit in that branch.
- Tag: A marker used to denote a specific point in the repository's history, often a release.
- Remote: A version of the project hosted on the internet or a network.
Explain the difference between Git and GitHub.
Junior
- Git is a version control system that allows individuals and teams to track changes in code during software development.
- GitHub, on the other hand, is a hosting service for Git repositories that provides a web-based graphical interface. It offers additional collaboration features like bug tracking, task management, and feature requests for every project.
- Essentially, Git is the tool, and GitHub is a service that hosts projects using Git.
What is a Git repository, and how do you create one?
Junior
- A Git repository is a storage space where your project's history is stored. It tracks changes to your project files.
- To create one, use the command git init in your project's root directory. This initializes a new Git repository.
What is a Git commit, and how do you create one?
Junior
- A Git commit represents a snapshot of your project at a particular point in time.
- To create a commit, you use the git commit command along with a message to describe the changes made since the last commit.
- For example: git commit -m "Added new feature"
Don't let one question ruin your next technical interview...
Explain the purpose of Git branches, and how do you create and switch between them?
Junior
- Git branches allow you to work on isolated features or fixes without affecting the main codebase.
- To create a new branch, you can use the git branch command followed by the branch name.
- To switch between branches, you can use the git checkout command, e.g., git checkout my-feature-branch
What is a Git merge, and how does it work?
Junior
- Git merge combines changes from one branch into another. To merge branches, you use the git merge command followed by the branch you want to merge into your current branch. Git will automatically incorporate the changes, creating a new merge commit if necessary.
- A Git merge is the process of integrating changes from one branch into another. It combines the histories of the two branches, often incorporating changes from a feature branch into the main branch.
- To perform a merge, use git merge [branch-name], where [branch-name] is the name of the branch you want to merge into your current branch. This creates a new "merge commit" that combines the changes of the merged branches.
Explain what a Git rebase is and when it is used.
Mid
- A Git rebase is a process of moving or combining a sequence of commits to a new base commit. It's used to integrate changes from one branch into another, similar to a merge, but it rearranges the commit history to create a linear sequence of commits.
- Rebasing is often used to clean up a messy history before merging a feature branch into the main branch. It's achieved with the git rebase command.
What is a Git conflict is and how do you resolve it?
Mid
- A Git conflict occurs when changes in different branches are incompatible, typically during a merge or rebase.
- To resolve it, manually edit the files to fix the conflicting changes, then mark the conflict as resolved by staging the files with git add, and complete the merge or rebase process with git commit.
What is a Git pull request?
Mid
- A Git pull request (PR) is a method of submitting contributions to a project. It's a request to review and merge a branch into another branch, typically used in online Git repository hosting services like GitHub or GitLab.
- It facilitates code review, discussion, and approval before changes are merged into the main branch.
Explain the difference between Git's pull and fetch operations.
Mid
- Git Pull: The git pull command fetches changes from a remote repository and automatically merges them into the current branch. It combines both the fetch and merge steps into one operation.
- Git Fetch: The git fetch command retrieves changes from a remote repository and updates the remote-tracking branches in your local repository. It does not automatically merge the changes into your working branch, allowing you to review and decide when and how to merge.
What is the difference between Git local and remote repositories?
Mid
- Local Repository: This is on your computer, where you make changes to your files. It's where you commit your work, create branches, and manage your version history locally.
- Remote Repository: Hosted on a server (like GitHub or GitLab), it's a shared space where multiple contributors can access, fetch, and push changes. It serves as a central hub for collaboration and backup.
What is a Git fork, and how does it differ from cloning a repository?
Mid
- A Git fork is a copy of a remote repository on a Git hosting platform, like GitHub or GitLab. It allows you to make changes independently without affecting the original repository.
- Cloning a repository, on the other hand, creates a local copy of a repository, and changes made to the clone can be pushed back to the original repository.
Explain the purpose of Git stash.
Mid
- Git stash is used to temporarily save changes that are not ready to be committed but need to be set aside. It is often used when you need to switch to a different branch or perform an operation that would conflict with your current changes.
- You can stash changes using git stash and later apply them using git stash apply or git stash pop.
Explain the git squash operation.
Mid
- Git squash is an operation used during a rebase or when merging a pull request, allowing you to combine multiple commits into a single commit.
- This creates a cleaner, more readable history by condensing a series of smaller changes or fixes into one coherent commit.
What is gitignore file.
Mid
- .gitignore is a file in Git repositories used to specify intentionally untracked files that Git should ignore. It's commonly used to exclude temporary files, build outputs, and other non-source files from being added to the version control.
- Each line in a .gitignore file specifies a pattern for files/directories to ignore.
What is Git's HEAD pointer?
Mid
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
What is Git reflog?
Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Explain the concept of a Git bisect.
Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Explain the purpose and use of Git hooks in a repository.
Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
What are Git tags?
Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Explain the difference between hard, soft, and mixed resets in Git.
Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.