[英]How can I find files what I need in branch what I choose in git?
In git, I want to find files, I want to choose a branch.在git中,我要查找文件,我要选择一个分支。 for example, look at below.
例如,看看下面。
git diff --name-status comitaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..comitbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
M fileA.txt // it was changed in xxx branch.
M fileB.txt // it was changed in yyy branch.
M fileC.txt // it was changed in zzz branch.
if I wanted to find fileB.txt, how can I use the git command?如果我想找到 fileB.txt,我该如何使用 git 命令?
Files aren't changed in branches , in Git.在 Git 中的分支中不会更改文件。 In fact, files aren't even stored in branches.
事实上,文件甚至不存储在分支中。 Files are stored in commits .
文件存储在commits 中。
You might wonder what good branches are, in Git.你可能想知道在 Git 中什么是好的分支。 You probably should wonder, because branches aren't really much good at all in Git—though this depends on how, exactly, you define branch , because the word branch itself is ambiguous, in Git.
你可能觉得奇怪,因为分支机构是不是真的都在多好Git的,虽然这取决于如何,究竟你定义的分支,因为这个词本身分公司是模糊的,在Git中。 There's a better-defined term, branch name , that you might want to use instead.
有一个更好定义的术语,分支名称,您可能想要使用它。 What branch names do, in Git, is allow you—and Git—to find commits.
在 Git 中,分支名称的作用是允许您和 Git查找提交。 The reason for this is that the true name of a commit is a big, ugly, random-looking number, expressed in hexadecimal , impossible for humans to remember or deal with.
这样做的原因是提交的真实名称是一个大的、丑陋的、随机的数字,以十六进制表示,人类无法记住或处理。
git diff --name-status xxx..yyy
Note: this means the same thing as:注意:这与以下含义相同:
git diff --name-status xxx yyy
The xxx
and yyy
here are commit hash IDs , or something that resolves to a commit hash ID , such as a branch name.这里的
xxx
和yyy
是提交哈希 IDs ,或者解析为提交哈希 ID 的东西,例如分支名称。 Git needs these so that it can find the two specific commits. Git 需要这些,以便它可以找到两个特定的提交。
Git then extracts both commits . Git 然后提取两个提交。 Each commit holds a full snapshot of every file , so this produces two different snapshots.
每个提交都保存每个文件的完整快照,因此这会生成两个不同的快照。 Git then compares the two snapshots:
Git 然后比较两个快照:
yyy
, missing in xxx
) or deleted (exists in xxx
, missing in yyy
).yyy
存在,在xxx
丢失)或被删除(在xxx
存在,在yyy
丢失)。xxx
) copy, will produce the right-side ( yyy
) copy.xxx
) 副本,将生成右侧( yyy
) 复制。 1 I say usually because there are a lot of corner cases, including, eg, --name-only
, where the status is dropped, or rename detection , where a file that went missing on the left is matched up with a newly created file on the right, as a rename operation. 1我说通常是因为有很多极端情况,例如,--
--name-only
,状态被丢弃,或重命名检测,其中左侧丢失的文件与新创建的文件相匹配在右侧,作为重命名操作。
M fileA.txt // it was changed in xxx branch. M fileB.txt // it was changed in yyy branch. M fileC.txt // it was changed in zzz branch.
The comments here imagine something that is simply not findable: M
just means that these three files exist in both left and right side commits, but have different contents.这里的注释想象了一些根本找不到的东西:
M
只是意味着这三个文件存在于左右提交中,但内容不同。
Any one commit can exist in multiple different branches.任何一个提交都可以存在于多个不同的分支中。 Each commit holds a full snapshot of every file, so no file is ever changed in a commit.
每次提交都会保存每个文件的完整快照,因此在提交中不会更改任何文件。 Files are changed between two commits , or aren't changed between those two commits.
文件在两次提交之间更改,或者在这两次提交之间不更改。
At the same time, though, each commit also holds information saying which particular commit hash ID(s) are the immediate predecessor of that commit.但与此同时,每个提交也包含说明哪些特定提交哈希 ID 是该提交的直接前驱的信息。 So if we have a string of commits, all in a row, like this:
因此,如果我们有一串提交,全部排成一行,如下所示:
... <-F <-G <-H
we can have Git start at commit H
and work backwards:我们可以让 Git 从提交
H
开始并反向工作:
First, we have Git use commit H
to locate commit G
, and compare the two snapshots.首先,我们让 Git 使用 commit
H
来定位 commit G
,并比较两个快照。 For whatever files are different in these two commits , we claim that these files are changed in commit H
.对于这两次提交中不同的文件,我们声称这些文件在提交
H
中发生了更改。 This is just a way of looking at things—commit H
is purely a snapshot, not changes, but by comparing it to an earlier snapshot, we can see it as changes.这只是一种看待事物的方式——commit
H
纯粹是一个快照,而不是更改,但是通过将它与之前的快照进行比较,我们可以将其视为更改。
Then, we have Git use commit H
to locate commit G
again, but this time we now consider commit G
on its own.然后,我们让 Git 再次使用 commit
H
来定位 commit G
,但这次我们现在考虑单独提交G
It has an earlier commit F
recorded, so we have Git use G
to locate F
and compare the two snapshots .它有一个较早的提交
F
记录,所以我们让 Git 使用G
来定位F
并比较两个快照。 Whatever is different, we claim is "changed in G
", as before.无论有什么不同,我们都声称“在
G
发生了变化”,就像以前一样。
Of course, G
's parent commit F
is an ordinary commit as well, with some earlier commit (presumably E
).当然,
G
的父提交F
也是一个普通的提交,有一些更早的提交(大概是E
)。 We repeat this process, over and over again.我们一遍又一遍地重复这个过程。
If these three commits are only found by starting at branch name feature
, people claim that the files were "changed in branch feature
":如果这三个提交仅通过从分支名称
feature
开始找到,则人们声称这些文件已“在分支feature
更改”:
A--B--C <-- main
\
D--E--F--G--H <-- feature
But here's the weird thing about Git branch names: we can create and destroy them at any time .但这里的大约GIT中分支名称奇怪的事情:我们可以创建并随时消灭他们。 Let's create a name,
zorg
, right now and make it point to commit G
:现在让我们创建一个名称
zorg
并使其指向提交G
:
A--B--C <-- main
\
D--E--F--G <-- zorg
\
H <-- feature
The set of commits in this repository is unchanged.此存储库中的提交集未更改。 All commits are frozen for all time: no part of any commit can ever change.
所有提交都被永久冻结:任何提交的任何部分都不能改变。 So the changes between commits
F
and G
, which we said were feature
-branch changes a moment ago ... well, they're still there, between F
and G
, but now they're zorg
-branch changes.所以提交
F
和G
之间的变化,我们刚才说的是feature
分支的变化......好吧,它们仍然存在,在F
和G
之间,但现在它们是zorg
变化。
What this means is that unless you hold the branch names fixed too (which Git doesn't, on purpose), you can't say that some change is due to some branch .这意味着除非你也保持分支名称固定(Git 故意不这样做),否则你不能说某些更改是由于某些branch 。 It's merely due to some set of commits .
这仅仅是由于一些commits 。 Don't ask about branches;
不问分行; ask about commits .
询问提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.