Git Interview Questions
1. What is Git?
Git is a distributed version control system (VCS) for tracking changes in source code during software development. It allows for multiple developers to work on the same codebase, track changes, and collaborate on the development process.
2. What are the main features of Git?
The main features of Git are version control, branching and merging, distributed development, and open-source. Git allows for tracking changes in the codebase, creating branches to work on new features, merging branches back into the main codebase, and collaborating with other developers through a distributed development model.
3. How does Git differ from other VCS?
Git differs from other VCS such as Subversion and CVS in that it is a distributed VCS. This means that every developer has a complete copy of the codebase and repository on their local machine, allowing them to work independently and commit changes without the need of a central server.
4. What is a repository in Git?
A repository in Git is a collection of files and their history, along with the metadata that Git uses to manage them. It contains the entire history of the project, including all the different versions of the files and the changes made to them.
5. What is a branch in Git?
A branch in Git is a way of isolating changes made to the codebase from the main development line. It allows for multiple developers to work on different features or bug fixes without affecting the main codebase. Branches can be created, merged, and deleted as needed.
6. What is a commit in Git?
A commit in Git is a snapshot of the codebase at a specific point in time. It records the changes made to the codebase, along with a message describing the changes. Commits can be made to any branch and are used to track the history of the codebase.
7. What is a merge in Git?
A merge in Git is the process of bringing changes made in one branch into another branch. When changes are made in a branch, they can be merged into the main branch to integrate the changes into the codebase.
8. What is a pull request in Git?
A pull request in Git is a way for developers to propose changes to a project. It allows for other developers to review the changes, discuss them, and then merge them into the main codebase.
9. What is a remote repository in Git?
A remote repository in Git is a version of the repository that is stored on a server and can be accessed by multiple developers. It allows for collaborating on the codebase and sharing changes with other developers.
10. How do you resolve conflicts in Git?
Conflicts in Git occur when changes made in one branch conflict with changes made in another branch. To resolve conflicts, the developer needs to manually edit the conflicting files to choose which changes to keep and which to discard, then commit the resolution.
11. What is the difference between Git and GitHub?
Git is a distributed version control system, while GitHub is a web-based platform that uses Git for version control and provides additional features for collaboration and management of code. GitHub allows for sharing code, tracking issues, and reviewing pull requests, among other features.
12. What is Git stash?
Git stash is a command used to temporarily save changes made to the working directory without committing them. It allows developers to switch to a different branch, work on something else, and then come back to their original changes later.
13. What is a Git tag?
A Git tag is a lightweight marker that can be applied to a specific commit, providing a way to mark important points in the codebase, such as releases.
14. What is a Git hook?
A Git hook is a script that can be executed automatically by Git at various points in the development process, such as before committing changes or after pushing to a remote repository. Hooks can be used to automate tasks, such as running tests or building the code.
15. What is Git rebase?
Git rebase is a command that allows developers to modify the commit history of a branch. It can be used to clean up the commit history, squash multiple commits into one, or to change the parent of a branch.
16. What is the difference between Git pull and Git fetch?
Git pull is a command that combines Git fetch and Git merge. It fetches changes from a remote repository and then merges them into the local branch. Git fetch, on the other hand, only fetches changes from a remote repository and does not merge them into the local branch.
17. How do you configure a global .gitignore file?
You can configure a global .gitignore file by running the command git config --global core.excludesfile path/to/.gitignore. This will tell Git to ignore the specified files or directories for all projects on your machine.
18. How do you revert a commit in Git?
You can revert a commit in Git by using the git revert command followed by the commit hash. This will create a new commit that undoes the changes made in the original commit.
19. How do you delete a branch in Git?
You can delete a branch in Git by using the git branch -d command followed by the branch name. If the branch has not been merged, use git branch -D instead.
20. How do you set up a remote repository in Git?
You can set up a remote repository in Git by using the git remote add command followed by the repository name and URL. This will allow you to push and pull changes from the remote repository.
21. How do you clone a remote repository in Git?
You can clone a remote repository in Git by using the git clone command followed by the repository URL. This will create a local copy of the repository on your machine.
22. How do you push changes to a remote repository in Git?
You can push changes to a remote repository in Git by using the git push command followed by the remote repository name and branch name. This will upload your local changes to the remote repository.
23. How do you fetch changes from a remote repository in Git?
You can fetch changes from a remote repository in Git by using the git fetch command followed by the remote repository name. This will download the changes from the remote repository to your local machine, but it will not merge them into your local branches.
24. How do you resolve merge conflicts in Git?
You can resolve merge conflicts in Git by using a text editor to manually edit the conflicting files. Look for the <<<<<​oaicite:{"index":0,"invalid_reason":"Malformed citation <<, =======, and >>>>>>>"}​ markers, which indicate the conflicting lines. Once you have resolved the conflicts, you can commit the changes.
25. How do you create a new branch in Git?
You can create a new branch in Git by using the git branch command followed by the branch name. This will create a new branch that is a copy of the current branch.
26. How do you switch between branches in Git?
You can switch between branches in Git by using the git checkout command followed by the branch name. This will switch your working directory to the specified branch.
27. How do you merge branches in Git?
You can merge branches in Git by first switching to the branch you want to merge into, and then using the git merge command followed by the branch name you want to merge.
28. How do you list all branches in Git?
You can list all branches in Git by using the git branch command. This will show a list of all branches, with the current branch indicated by an asterisk (*)
29. How do you rename a branch in Git?
You can rename a branch in Git by using the git branch -m command followed by the old and new branch names.
30. How do you create a tag in Git?
You can create a tag in Git by using the git tag command followed by the tag name and the commit hash. This will create a tag at the specified commit.
Leave a Comment