Information Technology

Git Interview Questions and Answers

Git Interview Questions and Answers

Git Interview Questions and Answers

Welcome to our comprehensive guide on Git interview questions and answers! Whether you're gearing up for a job interview or simply looking to deepen your understanding of version control with Git, this collection of questions covers the essential topics you need to know. From basic concepts like repositories and branches to advanced techniques such as rebasing and resolving conflicts, we've got you covered. So, grab a cup of coffee and let's dive into the world of Git!

1. What is Git and why is it used?
Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to collaborate efficiently and manage code versions effectively.

2. What is the difference between Git and GitHub?
Git is the version control system itself, while GitHub is a web-based platform that provides hosting for Git repositories and offers additional features like issue tracking, project management, and collaboration tools.

3. Explain the basic Git workflow.
The basic Git workflow involves initializing a repository, adding files to the staging area, committing changes, and pushing commits to a remote repository. It also includes pulling changes from the remote repository and resolving any conflicts.

4. What are branches in Git?
Branches in Git are separate lines of development that allow developers to work on features or fixes without affecting the main codebase. They enable parallel development and easy experimentation.

5. How do you create a new branch in Git?
You can create a new branch in Git using the `git checkout -b <branch_name>` command, which creates a new branch and switches to it in one step.

6. What is the difference between 'git init' and 'git clone'?
'git init' command is used to initialize an existing directory as empty repository locally, whereas the 'git clone' command copies an existing Github repository to your local directory.

7. What is a Git repository?
A Git repository, or repo, is a collection of files and folders along with metadata stored in a .git directory. It contains the entire history of changes to the project and allows for collaboration and version control.

8. How do you revert a commit in Git?
You can revert a commit in Git using the `git revert <commit>` command, which creates a new commit that undoes the changes introduced by the specified commit.

9. What is a Git conflict and how do you resolve it?
A Git conflict occurs when two or more changes conflict with each other, such as when the same line of code is modified differently in separate branches. You can resolve conflicts by manually editing the affected files to incorporate the desired changes.

10. Explain the purpose of Git hooks.
Git hooks are scripts that are triggered by various Git events, such as committing, merging, or pushing. They allow developers to automate tasks or enforce policies, such as code formatting or running tests before commits are accepted.

11. How do you undo the last commit in Git?
You can undo the last commit in Git using the `git reset HEAD~1` command, which resets the HEAD to the previous commit without modifying the working directory. 

12. What is a Git stash?
Git stash is a feature that allows you to temporarily shelve changes in your working directory so that you can work on something else. Stashed changes can be reapplied later using the `git stash apply` command.

13. Explain the difference between git pull and git fetch.
`git pull` fetches changes from the remote repository and merges them into the current branch, while `git fetch` only downloads changes from the remote repository without merging them.

14. What is the purpose of the .gitignore file?
The .gitignore file specifies intentionally untracked files that Git should ignore. It allows developers to exclude certain files or directories, such as build artifacts or temporary files, from version control.

15. How do you delete a branch in Git?
You can delete a branch in Git using the `git branch -d <branch_name>` command, which deletes the specified branch if it has been fully merged into the current branch. Use `git branch -D <branch_name>` to force delete a branch regardless of its merge status.

16. Explain the difference between git rebase and git merge.
`git rebase` moves the entire feature branch to begin on the tip of the destination branch, rewriting the commit history. `git merge` combines changes from one branch into another, creating a new commit for the merge.

17. What is a Git submodule?
A Git submodule is a reference to a specific commit in another repository. It allows you to include external repositories as dependencies in your project and manage them independently.

18. How do you view the commit history in Git?
You can view the commit history in Git using the `git log` command, which displays a chronological list of commits along with their metadata and changes.

19. What is a Git tag and how do you create one?
A Git tag is a named pointer to a specific commit in the repository history. You can create a tag using the `git tag <tag_name>` command, optionally specifying a commit hash or branch name to tag.

20. Explain the purpose of Git rebase.
Git rebase is used to integrate changes from one branch into another by moving the entire feature branch to begin on the tip of the destination branch. It helps maintain a linear commit history and resolve conflicts more easily.

21. How do you configure Git?
You can configure Git using the `git config` command, which allows you to set user preferences, repository settings, and aliases. Configuration options are stored in the .gitconfig file in your home directory.

22. What is the Git HEAD?
The Git HEAD is a pointer to the current commit in the repository. It can refer to a branch, a commit hash, or a tag, depending on the current state of the repository.

23. What is forking in Git?
A fork is a rough copy of a repository. Forking a repository allows you to freely test and debug with changes without affecting the original project. One of the excessive use of forking is to propose changes for bug fixing.

24. How do you rename a remote branch in Git?
You can rename a remote branch in Git by pushing a new local branch with the desired name and deleting the old remote branch using the `git push` and `git push origin --delete <old_branch_name>` commands.

25. Explain the purpose of Git cherry-pick.
Git cherry-pick is used to apply a single commit from one branch to another. It allows you to selectively choose which commits to include in a branch without merging the entire branch.

26. What is a Git rebase interactive?
Git rebase interactive allows you to selectively choose which commits to include, edit, or squash during a rebase operation. It provides greater control over the commit history and allows for more fine-grained changes.

27. How do you resolve a merge conflict in Git?
You can resolve a merge conflict in Git by manually editing the affected files to incorporate the desired changes, marking the conflicts as resolved using Git markers, and then committing the changes to complete the merge.

28. What is the purpose of Git bisect?
Git bisect is a binary search tool used to find the commit that introduced a bug or regression in the codebase. It automatically checks out commits and allows you to test whether they are good or bad until the problematic commit is identified.

29. How do you squash commits in Git?
You can squash commits in Git using an interactive rebase (`git rebase -i`) to combine multiple commits into a single commit. During the rebase, mark the commits you want to squash with `squash` or `fixup`.

30. What is the function of 'git diff' in git?
The 'git diff' command helps you see, compare, and understand changes in your project.

Remember:
Above interview questions are conceptual. You also need to prepare git commands, with syntax and working understading. To prepare Git commands, go through our 'Basic Git Commands You Need To Know' blog.