Top 50 Interview question And answers of Git

A quick intro to git

Git is an Open Source Distributed Version Control System. So Git can be used to store content — it is mostly used to store code due to the other features it provides. Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel.

It was created by Linus Torvalds in 2005 and has been maintained by Junio Hamano since then.

Now let’s move to questions and Answers

1. what is GIT

Answer: Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to track changes in the source code, enabling multiple developers to work together on non-linear development.

2. What is a git repository?

Answer: A git repository is a file structure where git stores all the project-based files. Git can either stores the files on the local or the remote repository.

3. What does the command git config do?

Answer: The git config command is a convenient way to set configuration options for defining the behavior of the repository, user information and preferences, git installation-based configurations, and many such things.

For example:
To set up your name and email address before using git commands, we can run the below commands:

git config –global
user. name
“<>”

git config –global user.email “<>”

4. What does the git add command do?

Answer:

  • This command adds files and changes to the index of the existing directory.
  • You can add all changes at once using git add. command.
  • You can add files one by one specifically using the git add command.
  • You can add contents of a particular folder by using the git add // command

5. How will you create a git repository?

Answer:

  • Have git installed in your system.
  • Then in order to create a git repository, create a folder for the project and then run git init.
  • Doing this will create a .git file in the project folder which indicates that the repository has been created.

6. What is the command used to delete a branch?

Answer:

7. What are the Differences between git pull and git fetch.

Answer:

8. How would you recover a branch that has already pushed changes in the central repository but has been accidentally deleted from every team member’s local machines?

Answer: We can recover this by checking out the latest commit of this branch in the reflog and then checking it out as a new branch.

9. What consists of a commit object?

Answer: A commit object consists of the following components:

  • A set of files that represents the state of a project at a given point in time.
  • Reference to parent commits objects.
  • A 40 character string termed as SHA-1 name uniquely identifies the commit object.

10. Name a few Git commands with their function.

Answer:

  • Git config – Configure the username and email address
  • Git add – Add one or more files to the staging area
  • Git diff – View the changes made to the file
  • Git init – Initialize an empty Git repository
  • Git commit – Commit changes to head but not to the remote repository

11. What are the advantages of using Git?

Answer:

  • Faster release cycles
  • Easy team collaboration
  • Widespread acceptance
  • Maintains the integrity of source code
  • Pull requests

12. Which command is used to create an empty Git repository?

Answer: git init – This command helps to create an empty repository while working on a project.

13. What does the git push command do?

Answer:

14. How do you resolve conflicts in Git?

Answer: Here are the steps that will help you resolve conflicts in Git:

  • Identify the files responsible for the conflicts.
  • Implement the desired changes to the files
  • Add the files using the git add command.
  • The last step is to commit the changes in the file with the help of the git commit command.

15. What is the functionality of git ls-tree?

Answer: The git ls-tree command is used to list the contents of a tree object.

16. What does git clone do?

Answer: Git clone allows you to create a local copy of the remote GitHub repository. Once you clone a repo, you can make edits locally in your system rather than directly in the source files of the remote repo

17. What is Git stash?

Answer: Let’s say you’re a developer and you want to switch branches to work on something else. The issue is you don’t want to make commits in uncompleted work, so you just want to get back to this point later. The solution here is the Git stash.

Git stash takes your modified tracked files and saves them on a stack of unfinished changes that you can reapply at any time. To go back to the work you can use the stash pop.

18. What is the use of the git config command?

Answer: The git config command is used to set git configuration values on a global or local level. It alters the configuration options in your git installation. It is generally used to set your Git email, editor, and any aliases you want to use with the git command.

19. What is the functionality of the git clean command?

Answer: git clean is a convenient method for deleting untracked files in a repo’s working directory. Untracked files are those that are in the repo’s directory but have not yet been added to the repo’s index with git add.

20. What is SubGit and why is it used?

Answer: SubGit is a tool that is used to migrate SVN to Git. It transforms the SVN repositories to Git and allows you to work on both systems concurrently. It auto-syncs the SVN with Git.

21. Explain the different points when a merge can enter a conflicted stage.

Answer: There are two stages when a merger can enter a conflicted stage.

  1. Starting the merge process

If there are changes in the working directory of the stage area in the current project, the merge will fail to start. In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands.

  1. During the merge process

The failure during the merge process indicates that there’s a conflict between the local branch and the branch being merged. In this case, Git resolves as much as possible, but some things have to be fixed manually in the conflicted files.

22. What is the command used to fix a broken commit?

Answer: To fix a broken commit in Git, you may use the “git commit –amend” command, which helps you combine the staged changes with the previous commits instead of creating an entirely new commit.

23. How do you recover a deleted branch that was not merged?

Answer: To recover a deleted branch, first, you can use the git reflog command. It will list the local recorded logs for all the references. Then, you can identify the history stamp and recover it using the git checkout command.

24. What are the three main trees maintained by GIT?

Answer: GIT maintains the following three trees:

  • HEAD: This is the last commit snapshot.
  • Index: This is the proposed next commit snapshot.
  • Working Directory: This is the sandbox for doing changes

25. What are the three main steps of working GIT?

Answer:

GIT has the following three main steps in a simple workflow:

  • Check out the project from HEAD to Working Directory.
  • Stage the files from Working Directory to Index.
  • Commit the changes from Index to HEAD.

26. What is git grep?

Answer:

27. What are the four major protocols used by GIT for data transfer?

Answer: GIT uses the following major protocols for data transfer:

  • Local
  • HTTP
  • Secure Shell (SSH)
  • Git

28. Is origin a special branch in GIT?

Answer:

29. How can we rename a remote repository?

Answer: We can use the command git remote rename for changing the name of a remote repository. This changes the short name associated with a remote repository in your local. The command would look as follows:

30. Do we have to store Scripts for GIT hooks within the same repository?

Answer:

  • A Hook is local to a GIT repository. But the script attached to a hook can be created either inside the hooks directory or it can be stored in a separate repository. But we have to link the script to a hook in our local repository.
  • In this way, we can maintain versions of a script in a separate repository, but use them in our repository where hooks are stored.
  • Also when we store scripts in a separate common repository, we can reuse the same scripts for different purposes in multiple repositories.

31. How can we attach an automated script to run in the event of a new commit by push command?

Answer:

  • In GIT we can use a hook to run an automated script on a specific event. We can choose between pre-receive, update or post-receive hook and attach our script on any of these hooks.
  • GIT will automatically run the script on the event of any of these hooks.

32. What is a shortlog in GIT?

Answer:

  • A shortlog in GIT is a command that summarizes the git log output.
  • The output of git shortlog is in a format suitable for release announcements.

33. How does GIT provide flexibility in version control?

Answer:

  • GIT is very flexible version control system. It supports non-linear development workflows. It supports flows that are compatible with external protocols and existing systems.
  • GIT also supports both branching and tagging that promotes multiple kinds of workflows in version control.

34. How can we convert git log messages to a different format?

Answer:

35. What are the programming languages in which git hooks can be written?

Answer: In Git the hook scripts can be implemented using any language but Shell, Ruby, Perl and Python scripts are the most common as far as I know. The language of the script is determined by the shebang notation as it is usually in Linux-based software.

36. What are the three most popular versions of the git diff command?

Answer: The three most popular git diff commands are as follows:

  • git diff: It displays the differences between the working directory and the index.
  • git diff –cached: It displays the differences between the index and the most recent commit.
  • git diff HEAD: It displays the differences between the working directory and the most recent commit

37. What is GIT version control?

Answer:

  • GIT version control helps us in managing the changes to source code over time by a software team. It keeps track of all the changes in a special kind of database. If we make a mistake, we can go back in time and see previous changes to fix the mistake.
  • GIT version control helps the team in collaborating on developing software and working efficiently. Everyone can merge the changes with confidence that everything is tracked and remains intact in GIT version control. Any bug introduced by a change can be discovered and reverted back by going back to a working version.

38. What is the command for Rebasing in Git?

Answer:

39. What is Rebasing in GIT?

Answer:

  • Rebasing is the process of moving a branch to a new base commit.
  • It is like rewriting the history of a branch.
  • In Rebasing, we move a branch from one commit to another. By this we can maintain linear project history.
  • Once the commits are pushed to a public repository, it is not a good practice to use Rebasing.

40. What command will you use to delete a branch that has unmerged changes?

Answer: To forcibly delete an unwanted branch with unmerged changes, we use the following command:

41. What command will you use to delete a branch?

Answer:

After the successful merge of the feature branch in the main branch, we do not need the feature branch.

To delete an unwanted branch we use the following command:

42. How can we resolve a merge conflict in GIT?

Answer:

When GIT reports merging conflict in a file, it marks the lines as follows:

Example:

43. How will you add a new feature to the main branch?

Answer: We do the development work on a feature branch that is created from the master branch. Once the development work is ready we use the git merge command to merge it into the master branch.

44. How will you create a new branch in GIT?

Answer: We use the following command to create a new branch in GIT:

45. What is the syntax for “Rebasing” in Git?

Answer: The syntax used for rebase is “git rebase [new-commit] “

46. Name a few Git repository hosting services

Answer:

  • Pikacode
  • Visual Studio Online
  • GitHub
  • GitEnterprise
  • SourceForge.net

47. Is it necessary to push the commits to a server to conclude Git workflow?

Answer: No, it is not necessary to push the commits to the server

48. When does a branch diverge?.

Answer: A branch diverges from another branch once an extra and different commit is included in the chain.

49. What are some alternatives to SubGit?

Answer: Some of the SubGit alternatives are as follows:

  • SmartGit
  • GitKraken
  • TortoiseGit
  • git-cola
  • Tower
  • gitg

50. What is SubGit?

Answer: SubGit is a software tool used to migrate from SVN to Git. To migrate your repository from SVN to Git, you must install SubBit on your Git Server.

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x