DEV Community

Nozibul Islam
Nozibul Islam

Posted on

GIT Interview Questions & Answers

GIT Interview Questions & Answers.

1. What is GIT?

Answer: GIT is a distributed version control system and source code management (SCM) system with an emphasis on handling both small and large projects with speed and efficiency.

2. What is a Distributed Control System?

Answer: In a distributed control system, we work locally on our machine and later transfer the code to a centralized repository (like GitHub). We don’t need to be connected to the centralized repository to work.

3. What is GIT version control?

Answer: GIT version control allows you to track the history of files (e.g., code files). It supports creating different versions of file collections, where each version captures a snapshot of the files at a certain point in time, allowing you to revert to a previous version if needed.

4. What is the difference between SVN and Git?

Answer: SVN is a centralized version control system, meaning you directly interact with a central repository. Git, on the other hand, is distributed, allowing you to work on your local machine and later push your changes to the centralized repository.

5. What is a repository in GIT?

Answer: A Git repository contains the history of files and the information needed to track changes made to them over time.

6. How can you create a local repository in Git?

Answer: You can create a local repository using the command:

git init
Enter fullscreen mode Exit fullscreen mode

7. What is a ‘bare repository’ in Git?

Answer: A bare repository contains only the version control information and no working files (it lacks the special .git sub-directory).

8. How to configure GitHub repository locally?

Answer: Set your username and email for Git configuration with these commands:

git config --global user.name "user_name"
git config --global user.email "user_email"
Enter fullscreen mode Exit fullscreen mode

9. How to create an alias to Git commands?

Answer: You can create an alias for Git commands using:

git config --global alias.lo "log --oneline"
Enter fullscreen mode Exit fullscreen mode

10. What is git clone?

Answer: git clone is used to download an existing repository from a remote source (e.g., GitHub) to your local machine.

git clone <url>
Enter fullscreen mode Exit fullscreen mode

11. What is ‘git add’?

Answer: git add is used to add files from your working directory to the staging area (index).

git add <file_name1> <file_name2>
Enter fullscreen mode Exit fullscreen mode

12. What is the staging area?

Answer: The staging area is an intermediate space where files are held before they are committed to the repository. This allows you to format and review your changes.

13. What is the use of ‘git log’?

Answer: git log is used to see the commit history in a project. You can filter by author, date, or content using options.

git log
Enter fullscreen mode Exit fullscreen mode

14. How can we add modified files to the staging area and commit them at the same time?

Answer: You can commit all modified files using:

git commit -a -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

15. How to edit an incorrect commit message?

Answer: Use the --amend option to edit the last commit message:

git commit --amend -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

16. How to revert a commit to the staging area?

Answer: Use git reset to move a commit to the staging area:

git reset --soft <previous_commit_id>
Enter fullscreen mode Exit fullscreen mode

17. What is git reset?

Answer: git reset is used to reset the current HEAD to a specific state, whether that’s soft, mixed, or hard reset.

18. What is the ‘HEAD’ in Git?

Answer: HEAD is a reference to the last commit in the currently checked-out branch. The default branch is typically called master.

19. What is .gitignore file?

Answer: A .gitignore file lists files that Git should ignore and not track. This is useful for excluding files like build outputs or sensitive data.

20. How to see the difference between 2 commits?

Answer: Use git diff to compare two commits:

git diff <commit_id1>..<commit_id2>
Enter fullscreen mode Exit fullscreen mode

21. How to create a branch?

Answer: To create a new branch, use the following command:

git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

22. How to switch to a different branch?

Answer: To checkout to a branch, use:

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

23. What is git push?

Answer: git push is used to upload local commits to a remote repository.

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

24. What is git pull?

Answer: git pull downloads and merges data from a remote repository to your local repository.

git pull origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

25. What is git merge?

Answer: git merge is used to combine changes from two branches into one.

git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

26. What is git conflict?

Answer: A git conflict happens when two branches have changes to the same part of a file. You need to resolve the conflict before merging.

27. How to resolve merge conflict?

Answer: Manually edit the files to fix the conflicting changes, then add and commit the resolved files.

28. What is git stash?

Answer: Git stash is used to temporarily save changes in your working directory that are not yet committed.

git stash save "<message>"
Enter fullscreen mode Exit fullscreen mode

29. What is the difference between git merge and git rebase?

Answer: Merge combines the changes into a single commit.
Rebase applies the commits one by one and rewrites the commit history.

30. How do you undo the last commit?

Answer: You can revert the last commit using:

git revert <commit_id>
Enter fullscreen mode Exit fullscreen mode

31. What is a pull request?

Answer: A pull request is a request to merge changes from one branch into another, typically used in collaborative environments like GitHub.

32. How to delete a repository in GitHub?

Answer: To delete a repository on GitHub:

  • Navigate to the main page of the repository.
  • Under the repository name, click Settings.
  • Scroll to the bottom and click Delete this repository.
  • Confirm the deletion by typing the repository name.

33. How to give access to a specific person to a repository?

Answer: To give someone access to your GitHub repository:

  • Navigate to Settings of the repository.
  • In the left sidebar, click Collaborators.
  • Start typing the collaborator's username and select it from the dropdown.
  • Click Add collaborator and the user will receive an invitation email.

34. How to Lock a branch? Why do we need to lock a branch?

Answer: To lock a branch:

  • Navigate to Settings on your repository page.
  • In the left sidebar, click Branches.
  • Select the branch you want to lock from the dropdown menu.
  • Check the box for Protect this branch to lock it. Locking branches is essential to prevent unauthorized changes and ensure that only authorized users can push to important branches like master or main.

35. How to delete a remote branch?

Answer: To delete a branch remotely, use:

git push origin --delete <branch_name>
Enter fullscreen mode Exit fullscreen mode

36. What is the difference between git clone and git pull?

Answer:

  • git clone: Used when you want to download an entire existing repository from a remote source to your local machine.

  • git pull: Used when you have an existing local repository, and you want to fetch and merge new changes from a remote repository into your local one.

37. How to see the local and remote branch list?

Answer: To see the local and remote branch list, use:

git branch -a
Enter fullscreen mode Exit fullscreen mode

38. How to push a new branch and its data to a remote repository?

Answer: To push a new branch and its data to a remote repository, use:

git push -u origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

39. How to create an alias for git commands?

Answer: To create an alias for a git command:

git config --global alias.<alias_name> "<actual_git_command>"
Enter fullscreen mode Exit fullscreen mode

For example, to create an alias for git log --oneline, use:

git config --global alias.lo "log --oneline"
Enter fullscreen mode Exit fullscreen mode

40. What is git rebase?

Answer: Git rebase is an alternative to git merge. It allows you to apply commits from one branch onto another. It rewrites the commit history and keeps the history linear.

41. What is the difference between git merge and git rebase?

Answer:

  • Merge: Combines the changes from two branches, creating a merge commit.
  • Rebase: Moves or combines commits from one branch onto another, rewriting the commit history without a merge commit.

42. What is the function of ‘git rm’?

Answer: git rm is used to remove files from the working directory and the staging area. If a file is deleted using git rm, it can be reverted later.
However, files deleted using the rm command outside Git cannot be recovered.

43. How do you resolve merge conflict?

Answer: To resolve a merge conflict, you need to manually edit the conflicted files to fix the conflicting changes. Once resolved, you can add and commit the changes to complete the merge.

44. What is git fetch?

Answer: git fetch is used to download new data from a remote repository but doesn't automatically merge the data with your current branch. It updates your local repository with the latest changes from the remote repository.

45. What is the function of ‘git reset’?

Answer: git reset is used to reset your current HEAD to a specific state. It can be used with different options (--soft, --mixed, --hard) depending on whether you want to keep changes or discard them.

46. How to rename a local branch?

Answer: To rename a local branch:

git branch -m <old_branch_name> <new_branch_name>
Enter fullscreen mode Exit fullscreen mode

47. How to see the remote branch list?

Answer: To see remote branches:

git branch -r
Enter fullscreen mode Exit fullscreen mode

Or, use:

git remote show origin
Enter fullscreen mode Exit fullscreen mode

48. What is the use of ‘git checkout’?

Answer: git checkout is used to switch between branches or restore files to their previous state. For example, to switch branches:

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

49. How to see the difference between 2 branches?

Answer: To see the difference between two branches:

git diff <branch1>..<branch2>
Enter fullscreen mode Exit fullscreen mode

50. How to get back a file from the staging area to the working area?

To move a file from the staging area back to the working directory:

git reset HEAD <file_name>
Enter fullscreen mode Exit fullscreen mode

51. What is git push?

Answer: git push is used to upload your local commits to a remote repository. It is used to share changes with others in a collaborative environment.

52. How do you undo a commit?

To undo a commit:

git revert <commit_id>
Enter fullscreen mode Exit fullscreen mode

53. How to create a branch while checking out?

Answer: To create and switch to a new branch in one step:

git checkout -b <branch_name>
Enter fullscreen mode Exit fullscreen mode

54. What is git pull and how does it work?

Answer: git pull is a combination of git fetch and git merge. It fetches new commits from a remote repository and merges them into your local branch.

55. How do you remove a file from Git tracking but keep it locally?

Answer: To stop tracking a file with Git but keep it in your local working directory:

git rm --cached <file_name>
Enter fullscreen mode Exit fullscreen mode

You can continue to format the rest of the questions similarly. This list will provide a comprehensive set of Git-related questions and answers suitable for sharing on Dev.to, where developers can reference them during interviews or when learning Git.

Top comments (0)