Home > Development Tools > git > body text

20 interview questions about Git

青灯夜游
Release: 2020-01-04 09:41:21
forward
8646 people have browsed it

I love Git at work. Git plays an important role in many development teams and is an essential technology. Therefore, here are some common Git interview questions prepared.

20 interview questions about Git

The first question about Git interview must be:

1. What is the difference between Git and SVN?

##1. Git is a distributed version control tool2. It belongs to 3rd generation version control tools3. Clients can clone the entire repository on their local system4. Submissions can be made even when offline5.Push/pull operation is faster6.Projects can be automated using commit Sharing

2. What is Git?

I suggest you first understand the architecture of git before answering this question, as shown in the figure below, try to explain this figure:

●Git is a distributed version control system (DVCS). It can track changes to files and allow you to revert changes to any specific version.

● Compared to other version control systems (VCS) such as SVN, its distributed architecture has many advantages. One major advantage is that it does not rely on a central server to store all versions of project files.

●Each developer can "clone" a copy of the repository I marked with "Local repository" in the diagram and have the full history of the project on his hard drive, so when the server goes down At this time, all the recovery data you need is in your teammate's local Git repository.

●There is also a central cloud repository to which developers can submit changes and share them with other team members, as shown in the figure, all collaborators are submitting changes to the "remote repository".

20 interview questions about Git

The next set of Git interview questions will test your experience using Git:

3. What is the commit command in Git?

The answer is very simple.
The command used to write the commit is git commit -a.

Now explain the -a flag. By adding -a on the command line, you instruct git to commit the new content of all tracked files that have been modified. Also mention that if you need to submit a new file for the first time, you can git add before git commit -a.

4. What is a "bare repository" in Git?

You should explain the difference between "working directory" and "bare repository".

A "bare" repository in Git contains only version control information and no working files (no working tree), and it does not contain the special .git subdirectory. Instead, it contains everything in the .git subdirectory directly in the home directory itself, where the working directory includes:

 1. A .git subdirectory, where Contains all relevant Git revision history for your repository.

2. The working tree, or a copy of the checked-out project files.

5. What language is Git written in?

You need to explain why you are using it, not just name the language. I suggest you answer this:

Git is written in C language. GIT is fast, and C does this by reducing runtime overhead.

6. In Git, how do you restore a commit that has been pushed and made public?

There can be two answers to this question and make sure that you include both because any of the below options can be used depending on the situation: 1
There can be two answers to this question answer, be sure to include both answers as well, as the following options are available depending on the situation:

Delete or fix the erroneous file in the new commit and push it to the remote repository. This is the most natural way to fix errors. After making the necessary modifications to the file, commit it to the remote repository I will be using

git commit -m "commit message"
Copy after login

● Create a new commit, undoing all changes made in the bad commit. You can use the command:

git revert <name></name>
Copy after login

7. What is the difference between git pull and git fetch? The

git pull command pulls new changes or commits for a specific branch from the central repository and updates the target branch in the local repository.

git fetch is also used for the same purpose, but it works slightly differently. When you execute git fetch, it fetches all new commits from the desired branch and stores them in a new branch in your local repository. If you want these changes to be reflected in the target branch, git merge must be performed after git fetch. The target branch is updated only after merging the target branch and the fetched branch. For convenience, remember the following equation:

git pull = git fetch git merge

8. What is "staging area" or "index" in git?

For this answer try to explain the below diagram as you can see: Format and review it for the "staging area" or the middle area of ​​the "index". As you can see from the diagram, each change is first validated in the staging area, which I call a "stage file", and then the changes are committed to the repository.

20 interview questions about Git

9. 什么是 git stash?

首先应该解释 git stash 的必要性。

通常情况下,当你一直在处理项目的某一部分时,如果你想要在某个时候切换分支去处理其他事情,事情会处于混乱的状态。问题是,你不想把完成了一半的工作的提交,以便你以后就可以回到当前的工作。解决这个问题的答案是 git stash。

再解释什么是git stash。

stash 会将你的工作目录,即修改后的跟踪文件和暂存的更改保存在一堆未完成的更改中,你可以随时重新应用这些更改。

10. 什么是git stash drop?

通过说明我们使用 git stash drop 的目的来回答这个问题。

git stash drop 命令用于删除隐藏的项目。默认情况下,它将删除最后添加的存储项,如果提供参数的话,它还可以删除特定项。

下面举个例子。

如果要从隐藏项目列表中删除特定的存储项目,可以使用以下命令:

git stash list:它将显示隐藏项目列表,如:

stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert “added file_size”
stash@{2}: WIP on master: 21d80a5 added number to log

如果要删除名为 stash@{0} 的项目,请使用命令 git stash drop stash@{0}

11. 如何找到特定提交中已更改的文件列表?

对于这个问题,不能仅仅是提供命令,还要解释这个命令究竟做了些什么。

要获取特定提交中已更改的列表文件,请使用以下命令:

git diff-tree -r {hash}

给定提交哈希,这将列出在该提交中更改或添加的所有文件。 -r 标志使命令列出单个文件,而不是仅将它们折叠到根目录名称中。

你还可以包括下面提到的内容,虽然它是可选的,但有助于给面试官留下深刻印象。

输出还将包含一些额外信息,可以通过包含两个标志把它们轻松的屏蔽掉:

git diff-tree –no-commit-id –name-only -r {hash}

这里 -no-commit-id 将禁止提交哈希值出现在输出中,而 -name-only 只会打印文件名而不是它们的路径。

12. git config 的功能是什么?

首先说明为什么我们需要 git config

git 使用你的用户名将提交与身份相关联。 git config 命令可用来更改你的 git 配置,包括你的用户名。

下面用一个例子来解释。

假设你要提供用户名和电子邮件 ID 用来将提交与身份相关联,以便你可以知道是谁进行了特定提交。为此,我将使用:

git config –global user.name "Your Name": 此命令将添加用户名。

git config –global user.email "Your E-mail Address": 此命令将添加电子邮件ID。

13. 提交对象包含什么?

Commit 对象包含以下组件,你应该提到以下这三点:

 ● 一组文件,表示给定时间点的项目状态

 ● 引用父提交对象

 ● SHAI 名称,一个40个字符的字符串,提交对象的唯一标识。

14. 如何在Git中创建存储库?

这可能是最常见的问题,答案很简单。

要创建存储库,先为项目创建一个目录(如果该目录不存在),然后运行命令 git init。通过运行此命令,将在项目的目录中创建 .git 目录。

15. 怎样将 N 次提交压缩成一次提交?

将N个提交压缩到单个提交中有两种方式:

 ● 如果要从头开始编写新的提交消息,请使用以下命令:

git reset –soft HEAD~N &&
git commit
Copy after login

 ● 如果你想在新的提交消息中串联现有的提交消息,那么需要提取这些消息并将它们传给 git commit,可以这样:

git reset –soft HEAD~N &&
git commit –edit -m"$(git log –format=%B –reverse .HEAD@{N})"
Copy after login

16. 什么是 Git bisect?如何使用它来确定(回归)错误的来源?

我建议你先给出一个Git bisect 的小定义。

Git bisect 用于查找使用二进制搜索引入错误的提交。 Git bisect的命令是

git bisect <subcommand> <options></options></subcommand>
Copy after login

既然你已经提到过上面的命令,那就解释一下这个命令会做什么。

此命令用了二进制搜索算法来查找项目历史记录中的哪个提交引入了错误。你可以通过告诉它已知包含该错误的“错误”提交以及在引入错误之前已知的“良好”提交来使用它。然后 git bisect 在这两个端点之间选择一个提交,并询问你所选的提交是“好”还是“坏”。它继续缩小范围,直到找到引入更改的确切提交。

17. 如果想要在提交之前运行代码性检查工具,并在测试失败时阻止提交,该怎样配置 Git 存储库?

我建议你先介绍一下完整性检查。

完整性或冒烟测试用来确定继续测试是否可行和合理。

下面解释如何实现这一目标。

这可以通过与存储库的 pre-commit hook 相关的简单脚本来完成。git 会在提交之前触发 pre-commit hook。你可以在这个脚本中运行其他工具,例如 linters,并对提交到存储库中的更改执行完整性检查。

最后举个例子,你可以参考下面的脚本:

#!/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
Copy after login

这段脚本检查是否需要通过标准 Go 源代码格式化工具 gofmt 传递所有即将提交的 .go 文件。如果脚步以非 0 状态退出,脚本会有效地阻止提交操作。

18. 描述一下你所使用的分支策略?

这个问题被要求用Git来测试你的分支经验,告诉他们你在以前的工作中如何使用分支以及它的用途是什么,你可以参考以下提到的要点:

 ● 功能分支(Feature branching)

要素分支模型将特定要素的所有更改保留在分支内。当通过自动化测试对功能进行全面测试和验证时,该分支将合并到主服务器中。

 ● 任务分支(Task branching)

在此模型中,每个任务都在其自己的分支上实现,任务键包含在分支名称中。很容易看出哪个代码实现了哪个任务,只需在分支名称中查找任务键。

 ● 发布分支(Release branching)

一旦开发分支获得了足够的发布功能,你就可以克隆该分支来形成发布分支。创建该分支将会启动下一个发布周期,所以在此之后不能再添加任何新功能,只有错误修复,文档生成和其他面向发布的任务应该包含在此分支中。一旦准备好发布,该版本将合并到主服务器并标记版本号。此外,它还应该再将自发布以来已经取得的进展合并回开发分支。

最后告诉他们分支策略因团队而异,所以我知道基本的分支操作,如删除、合并、检查分支等。

19. 如果分支是否已合并为master,你可以通过什么手段知道?

答案很直接。

要知道某个分支是否已合并为master,你可以使用以下命令:

git branch –merged 它列出了已合并到当前分支的分支。

git branch –no-merged 它列出了尚未合并的分支。

20. 什么是SubGit?

SubGit 是将 SVN 到 Git迁移的工具。它创建了一个可写的本地或远程 Subversion 存储库的 Git 镜像,并且只要你愿意,可以随意使用 Subversion 和 Git。

这样做有很多优点,比如你可以从 Subversion 快速一次性导入到 Git 或者在 Atlassian Bitbucket Server 中使用SubGit。我们可以用 SubGit 创建现有 Subversion 存储库的双向 Git-SVN 镜像。你可以在方便时 push 到 Git 或提交 Subversion。同步由 SubGit 完成。

推荐学习:git教程

Git ##SVN
1. SVN is a centralized version control tool
2. It belongs to the 2nd generation version control tools
3. Version history is stored in the server-side repository
4. Only online submissions are allowed
5.Push/pull operation is slow
6. Nothing is automatically shared

The above is the detailed content of 20 interview questions about Git. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template