Git Interview Questions

Answers and Explanations for Git Interview Questions

Whether you're a beginner or have several years of experience, you may encounter some basic questions in your Git interview. This section covers fundamental Git interview questions. Let's explore them.

My writing skills are poor, so I used chatgpt in some parts. Please focus on the content and try to understand it; don't speak in a way that sounds like chatgpt. Writing this article was tiring, using up all my knowledge, haha.

1. What is Git?

Answer: Candidates will find this question among the most common Git interview questions. Git Git is a distributed version control system (DVCS) and source code management system (SCMS) that helps track changes to a file. Git's functionality allows developers to roll back any specific change. Git can also help manage small and large projects with greater speed and efficiency. Git's distributed architecture is the main reason for its superiority over other version control systems. The most notable feature of Git is that it does not depend on a central server to store all versions of files in a project.

2. Are Git and GitHub different?

Answer: Candidates may encounter this question among basic Git interview questions. Git is a version control system for tracking changes in source code during software development. GitHub, on the other hand, is a repository hosting service for Git. GitHub also offers its own additional features. Some important features in GitHub include access control and collaboration features. Additionally, GitHub provides simple task management tools for various projects.

3. What are the benefits of using Git?

Answer: Candidates should prepare for this question as it is one of the most common Git interview questions. Below are the most notable benefits of Git.

  • Any project can use Git without any restrictions.
  • Git supports collaboration.
  • High availability along with data redundancy and backup capabilities.
  • Better disk usage and network performance.
  • There is only one Git directory per repository.

4. What programming language does Git use?

Answer: This is one of the seemingly simple but important questions among the top Git interview questions. However, candidates should try to explain their reasons for using the C language, rather than simply stating it! Git uses the C language to minimize the runtime overhead common in high-level languages.

5. What is a repository in Git and how can you create one?

Answer: Candidates will encounter a bit of technicality when answering this type of basic Git interview question. A repository in Git is where Git stores all files. Git stores files in local repositories or in remote repositories. The process of creating a repository in Git is one of the most frequently asked questions. First, you must create a directory for the project to create the repository. Then, you can run the “git init” command and create a git repository in the project directory.

6. What is a bare repository?

Answer: Candidates may also encounter this question among the top Git interview questions as a follow-up question. An empty repository contains version management information. An empty repository does not contain any working files or special .git subdirectories. Conversely, an empty repository contains all the information of the .git subdirectories in the live home directory. The working directory contains the working tree, i.e., verified copies of the project files and .git subdirectories with all the Git-related revision history for your repository.

7. How is Git different from SVN?

Answer: The difference between Git and SVN is one of the most noteworthy points in the best Git interview questions. First, Git lacks the functionality to manage excessively large files or frequently changing binary files. SVN, on the other hand, can manage multiple projects within the same repository.

The second difference is that Git doesn't support commits across multiple branches or tags. In contrast, SVN or Subversion support creating directories regardless of their location within the repository layout. Changes in Git are not possible. However, Subversion offers the flexibility for committers to treat a tag as a branch and create multiple revisions within a single tag root directory. However, Git is more distributed than SVN, which is more centralized.

8. What is Git Commit and what is the content of the commit object?

Answer: Candidates should focus on this question as one of the key fundamental interview questions. A Git commit is a command executed in a project to record the project's progress. The commit object contains a set of files representing the state of a file at different points in time. The commit object also includes a reference to the parent commit. Finally, you can also find the SHAI name, a unique identifier with a 40-character string for the commit object.

9. What are the different functions of Git repositories?

Answer: This question is a highlight among the best Git interview questions. Besides GitHub, other prominent Git repository hosting functionalities include Gitlab, Bitbucket, GitEnterprise, and SourceForge.

10. What are conflicts in Git?

Answer: Candidates may often encounter questions about conflicts in Git in basic Git interview questions. Git can handle most merges automatically by leveraging its automation features. Conflicts occur when two different branches make edits to the same line in a file. Another situation leading to a Git conflict is deleting a file on one branch and editing the same file on another branch. Conflicts are common occurrences, especially when working in a team environment.

11. What is the conflict resolution process in Git?

Answer: This question is one of the most common among top Git interview questions. Candidates can expect this question, along with questions about defining conflicts in Git, as in the previous question. Below are the steps to resolve conflicts in Git.

  • Identify the files responsible for the conflict.
  • Make the necessary changes to the files to avoid any potential conflicts.
  • Add files using the "git add." command.
  • The final step involves committing the changed file using the “git commit” command.

12. What is branching and how many types of branching are there in Git?

Answer: Candidates should prepare for this question as it is one of the most common Git interview questions. Branching in Git is a useful tool that helps users create individual branches and switch between them. Users can return to previous work without affecting their current work. Different types of branching in Git include feature branching, task branching, and release branching. Feature branching involves keeping all changes to a specific feature within a branch.

After a feature has been fully tested and validated, feature branching involves merging that feature into the main branch. Task branching involves including the new feature in a separate branch along with including the task key in the branch name. Release branching involves creating a copy of a branch in case a developed branch includes enough features for release.

13. What is SubGit?

Answer: Candidates can expect this question as one of the important Git interview questions at the beginner level. SubGit is a tool for migrating from SVN to Git. SubGit can help create a writable Git mirror of a local or remote Subversion repository. It can use Subversion as well as Git for any length of time the user needs. SubGit also provides fast one-time imports from Subversion to Git. Additionally, you can use SubGit within an Atlassian Bitbucket server. SubGit does not require any changes to existing infrastructure. Furthermore, SubGit also offers the flexibility to use all the features of Git and Subversion.

14. What are the uses of Git InstaWeb?

Answer: The applications of Git commands are one of the prominent topics for the latest Git interview questions. The “git instaweb” command automatically redirects a web browser and a web server with an interface to a local repository.

15. What are the common Git commands and their uses?

Answer: Candidates can easily find this question in most discussions about Git interview questions. Below are some common Git commands, along with their functions.

"git diff" helps display changes between commits and between commits and the working tree.

"git status" helps display the difference between the index and the working directory.

"git stash applies" is the command to revert saved changes to the working directory.

"git log" helps find a specific commit in the history.

"git checkout" is a command to update the working tree directories with directories from a different branch without merging them.

"git rm" removes files from the staging area and files on disk.

"git add" adds file changes in the current directory to the index.

The "git reset" command resets the index. It also resets the working directory to the state of the last commit.

"git is a tree" is ideal for representing a tree object along with the mode and name for each item.

16. What is the difference between clone, fork, and branch in Git?

Answer: This question is one of the most unique among the best Git interview questions. A Git fork is a remote, server-side copy of a repository, distinct from the original. It's important to note that forking isn't a Git concept but rather a social model. Cloning in Git is a local copy of a specific remote repository. During cloning, the user copies all the source repository information along with all branches and history. Branching is a process for managing all changes within a repository before merging them into code. You can think of a branch as a project development thread that exists within a repository.

17. How is a “branch” different from a “pull request”?

Answer: The difference between a branch and a pull request is one of the key DevOps interview questions for GitHub. A branch is simply a separate version of the code. A pull request occurs when an individual takes the repository, creates their own branch, makes changes, and then attempts to merge that branch into another code repository.

18. Do you know how to use “git cherry-pick”?

Answer: Commands will be a frequent element in Git interview questions. The use of the “git cherry-pick” command is evident in processes for introducing specific commits from one branch in the repository to another. A common application of “git cherry-pick” is clearly in forwarding or backing up commits from a maintenance branch to a development branch. This approach differs from other approaches such as merges and refactoring, which often apply multiple commits to a different branch.

19. What is the staging area in Git?

Answer: Candidates can find this question among the most common and best Git interview questions. The staging area is an intermediate area that helps format and review commits before finalizing them. It's also known as the index. The staging area is the first place to verify any changes before committing them to the repository.

20. Is it reasonable to create an additional commit or modify an existing commit?

Answer: Questions like these in Git interview questions can be confusing. You need to make it clear that creating an additional commit is more advantageous than modifying an existing commit. Modifications can destroy previously saved state within the commit. Modifications to the commit's contents can lead to the potential loss of critical data. Overuse of “git commit-amend” can lead to the development of a small commit and the accumulation of unwanted changes.

Intermediate Git Interview Questions

If you have a few years of experience working with Git, the interviewer may ask some command-line questions to test your knowledge. Therefore, here are some common interview questions asked of mid-level Git professionals. Take a look at these questions.

21. What is the difference between head, working tree, and index in Git?

Answer: Candidates should prepare for this question among the latest Git interview questions for intermediate level interviews. The working tree is also known as the working directory or workspace. It is the directory tree of source files that users see and edit. The index or staging area is simply a single binary file in /.git/Index. The index contains a list of all files in the current branch, SHA1 checksums, filenames, and timestamps. “HEAD” implies a reference to the last commit in the current branch that was checked out.

22. What is the purpose of "git config"?

Answer: You can find this question among the most common Git interview questions. Git uses your username to associate commits with a specific identity. The ‘git config’ command helps change Git configuration as well as your username. For example, if you want a username and email address to associate a commit with a specific identity, you can use the following commands.

git config -global user.Name The “Name” field can be used to add a username.

`git config -global user.email “E-mail Address”` will add the email address.

23. What is Git stash?

Answer: This is one of the mid-level Git interview questions that comes up in most Git interviews. Working on a specific project involves a lot of mess and requires branch switching. Stashing on Git helps to get your working directory with tracked edited files and staging revisions and store them on a stack of unfinished changes. Users can reapply changes at any time according to their preferences.

24. What types of jobs can you recover by restoring a deleted branch?

Answer: Candidates should prepare for unique Git interview questions like this one. When recovering a deleted file, you can recover archived files or files stored in an archived index. Untracked files cannot be recovered. The recommended best practice is to staging and commit your work in all cases or ensure they are archived.

25. How is 'git diff' different from 'git status'?

Answer: This question is also one of the important DevOps interview questions for GitHub interviews. 'git diff' helps represent changes between commits and changes between commits and the working tree. On the other hand, 'git status' helps find the differences between the working directory and the index. As a result, it helps to understand a specific git. The key difference is that “git diff” shows the differences between different commits, while “git status” does not.

26. How can I tell if a branch has been merged into master?

Answer: The answer to this type of Git interview question is usually quite simple. The following commands can help determine if a branch has been merged into master.

“git branch –merged” lists the branches that have been merged into the current branch.

“git branch –no -merged” lists the branches that have not been merged into the current branch.

27. What are the contents of 'hooks' in Git?

Answer: Candidates may find this question among the latest Git interview questions. The “hooks” directory contains shell scripts that are triggered when running related Git commands. For example, Git will attempt to execute a post-commit script after running a commit.

28. How can I find a list of files that have changed in a specific commit?

Answer: Candidates will find practical questions like this among the most common Git interview questions. The answer is quite simple! Just use the command ‘git diff-tree -r {hash}’. The ‘-r’ flag helps list the individual files. The output may contain some additional information. However, you can reduce the extra information with the help of two additional flags. The command with the additional flags would be “git diff-tree –no-commit-id –name-only -r {hash}.” –no-commit-id removes the commit hash from the output. The “–name-only” flag prints the filename instead of the path.

29. How can I delete a file from Git without deleting it from the file system?

Answer: Candidates should prepare for this question among the difficult Git interview questions. “git rm” can help remove files from the staging area as well as the file system or working tree. However, applying “git rm” is not valid in all cases. Therefore, you should use the “git reset” command with syntax like “git reset filename.” You can also use the command “echo filename >> .gitignore” and add it to the .gitignore library to avoid having to add it again.

30. What is the difference between rebasing and merging in Git?

Answer: As you know, commands make up a large part of the latest Git interview questions. The `rebase` command in Git helps integrate changes from one branch to another. You can use it instead of the `merge` command. The `merge` command takes independent development lines within a Git branch and integrates them into a single branch. The difference between the two commands is clear: `rebases` has to rewrite the commit history to create a straight, linear commit order.

31. What is the difference between revert and reset?

Answer: Candidates may find this question among the new DevOps interview questions for GitHub interviews. The `reset` command in git reverts local changes to the state of a Git repository. `git reset` works on the commit history, working directory, and staging area. The `revert` command in git creates a new commit that removes changes from the previous commit. The `revert` command adds a new history to the project without changing the existing history.

32. How can you revert a commit that has already been pushed and made public?

Answer: “git revert” can revert one or more commits. The command creates a new commit that removes the changes made in the previous commits. The following command can revert two previous commits.

“git revert HEAD~2.HEAD”

33. What are the benefits of the forking workflow?

Answer: Candidates may find this question to be one of the important Git interview questions. The first difference is that the forking workflow doesn't use a single server-side repository as the "central" code repository. Each developer has their own server-side repository. Therefore, the forking workflow is common in public open-source projects. Another benefit is integrating contributions without pushing to a specific central repository. Only the project manager can push to the official repository. Developers can notify the project manager that an update is ready for integration via a pull request.

34. What is the difference between ‘git fetch’ and ‘git pull’?

Answer: Candidates should prepare for this question among DevOps interview questions for GitHub interviews. 'git fetch' only downloads new data from the remote repository. It doesn't guarantee integrating the downloaded data into your working files. 'git pull' is ideal for downloading and merging data from the remote repository into local working files.

35. What is the syntax for rebasing in Git?

Answer: The syntax for rebasing in Git is “git rebase [new-commit]”

36. What is the purpose of 'git bisect'?

Answer: ‘git bisect’ is an important Git command for finding the commit responsible for introducing a bug. The command uses a binary search algorithm to find the commit in the project history that is responsible for introducing a bug.

37. What is Git stash drop?

Answer: The Git stash drop command removes the list after work is complete on the stored item. As a result, it ensures that any specific items or the most recently added items from the argument are removed.

38. Một số máy khách Git đồ họa tốt nhất cho LINUX là gì?

Trả lời: Ứng viên có thể tìm thấy câu hỏi này phổ biến trong các câu hỏi phỏng vấn Git. Máy khách Git tốt nhất cho Linux như sau:

  • Git GUI
  • Giggle
  • Git Cola
  • Smart Git
  • qGit
  • Git-g

39. git pull origin là gì?

Trả lời: Lệnh ‘git pull origin master’ lấy các commit từ nhánh master của origin có thể là origin cục bộ hoặc nhánh master. Sau đó, nó hợp nhất origin hoặc master vào nhánh hiện đang được kiểm tra.

40. Lợi ích mà các công cụ SCM cung cấp với Git là gì?

Trả lời: Người dùng có thể nhận được các lợi ích đặc biệt với các công cụ SCM như CVS, Subversion, ClearCase và Perforce. Các tính năng như khu vực staging tiện lợi, nhiều quy trình làm việc và phân nhánh cục bộ tiết kiệm chi phí xác nhận lợi ích của các công cụ SCM.

Câu Hỏi Phỏng Vấn Git Nâng Cao

Là một chuyên gia Git cấp cao, bạn có thể gặp các câu hỏi phỏng vấn Git nâng cao trong cuộc phỏng vấn của mình. Hãy xem qua một số câu hỏi phỏng vấn Git nâng cao và chuẩn bị cho cuộc phỏng vấn.

41. Quy trình để nén N commit cuối cùng thành một commit duy nhất là gì?

Trả lời: Câu hỏi này là một trong những câu hỏi phỏng vấn Git nâng cao với hai phản hồi riêng biệt tùy thuộc vào ngữ cảnh. Trong trường hợp viết một thông báo commit mới từ đầu, bạn có thể sử dụng lệnh sau.

“git reset -soft HEAD~N &&git commit.”

Nếu bạn phải chỉnh sửa một thông báo commit mới với việc thêm các thông báo commit hiện có, thì bạn nên trích xuất các thông báo và chuyển chúng đến Git commit. Lệnh sau giúp đạt được chức năng nêu trên.

42. Làm thế nào tôi nên cấu hình một kho lưu trữ Git để chạy các công cụ kiểm tra tính hợp lệ của mã?

Trả lời: Kiểm tra tính hợp lệ giúp xác định khả năng và tính khả thi của kiểm tra liên tục. Một kiểm tra tính hợp lệ có thể thực hiện thông qua một tập lệnh đơn giản liên quan đến hook pre-commit của kho lưu trữ liên quan. Tập lệnh cũng giúp chạy các công cụ khác như linters và thực hiện kiểm tra tính hợp lệ cho các thay đổi đã commit vào kho lưu trữ. Dưới đây là một ví dụ.

#!/bin/sh
files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)
if [ -z files ]; then
exit 0
fi
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
fi
echo “Some .go files are not fmt’d”
exit 1

Tập lệnh trên đánh giá nhu cầu chuyển bất kỳ tệp .go nào qua công cụ định dạng mã nguồn Go tiêu chuẩn. Thoát với một giá trị khác không giúp tập lệnh ngăn chặn việc áp dụng commit vào kho lưu trữ.

43. git reflog là gì?

Trả lời: Lệnh “git reflog” giúp theo dõi tất cả các thay đổi được thực hiện trong các tham chiếu của một kho lưu trữ. Nó duy trì một lịch sử nhật ký của các tham chiếu được tạo hoặc kiểm tra cục bộ đến kho lưu trữ.

44. Làm thế nào tôi có thể cherry-pick một commit hợp nhất?

Trả lời: Ứng viên có thể tìm thấy câu hỏi này trong số các câu hỏi phỏng vấn Git nâng cao phổ biến. Cherry-pick sử dụng một diff để tìm ra sự khác biệt giữa các nhánh. Với commit hợp nhất cho một nhánh khác, nó có hai bộ thay đổi và hai cha mẹ. Ví dụ, nếu bạn có commit hợp nhất ref 64cv89d, bạn phải chỉ định -m và sử dụng cha mẹ 1 làm cơ sở như sau –

git checkout release-branch

git cherry-pick -m 1 64cv89d

45. Làm thế nào tôi có thể sao chép một commit trong một nhánh sang một nhánh khác?

Trả lời: Lệnh ‘cherry-pick’ là lựa chọn tốt nhất trong trường hợp này. Nó giúp tìm tính khả thi của việc hoàn nguyên một commit hiện có về nhánh hoặc vị trí hiện tại. Do đó, bạn phải chuyển sang nhánh mục tiêu và sau đó gọi lệnh “git cherry-pick {hash của commit đó}”. Kết quả là, bạn có thể tìm thấy một commit mới với một hash mới do việc áp dụng các thay đổi vào một đích đến khác.

46. Nếu một trong những đồng đội của tôi vô tình xóa một nhánh và đẩy các thay đổi vào kho lưu trữ git trung tâm và tôi muốn khôi phục nhánh đó thì sao?

Trả lời: Bạn cần xem xét commit mới nhất đến nhánh cụ thể với reflog. Sau đó, bạn có thể kiểm tra nó như một nhánh mới.

47. Quy trình làm việc Gitflow là gì?

Trả lời: Ứng viên có thể tìm thấy câu hỏi này trong số các câu hỏi phỏng vấn Git khó nhất. Quy trình làm việc Gitflow sử dụng hai nhánh chạy song song dài hạn được gọi là master và develop. Các thành phần trong quy trình làm việc Gitflow như sau.

Nhánh master luôn sẵn sàng để phát hành trực tiếp với mọi thứ sẵn sàng cho sản xuất.

Các nhánh Hotflix giúp vá nhanh các bản phát hành sản xuất.

Nhánh Develop giúp hợp nhất tất cả các nhánh tính năng và cũng thực hiện tất cả các kiểm tra.

Nhánh Tính năng ngụ ý một nhánh duy nhất cho mỗi tính năng mới. Nhánh tính năng có thể được đẩy vào nhánh phát triển giống như nhánh cha của chúng.

48. ‘git remote’ khác với ‘git clone’ như thế nào?

Trả lời: ‘git remote’ giúp tạo một mục trong cấu hình git trong khi chỉ định một tên cho một URL cụ thể. Mặt khác, ‘git clone’ giúp tạo một kho lưu trữ git mới bằng cách sao chép một kho lưu trữ hiện có trong URL.

49. Lệnh để sửa một commit bị hỏng là gì?

Trả lời: Để sửa một commit bị hỏng, bạn có thể sử dụng lệnh “git commit –amend”. Lệnh này có thể giúp sửa thông báo commit bị hỏng trong trình chỉnh sửa.

50. Mô hình phân nhánh chung trong Git là gì?

Trả lời: Cách tiếp cận chung nhất để tạo các nhánh trong Git là phát triển một nhánh “Chính”. Nó cũng liên quan đến việc tạo một nhánh khác để triển khai các tính năng mới. Mô hình này hữu ích trong trường hợp nhiều nhà phát triển làm việc trên một dự án duy nhất.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *