简体   繁体   English

如何在我在 git 中选择的分支中找到我需要的文件?

[英]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.这里的xxxyyy提交哈希 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 然后比较两个快照:

  • If some file is in both snapshots, and is the same in both snapshots, Git says nothing at all about that file.如果某个文件在两个快照中,并且在两个快照中都相同,则 Git 对那个文件一无所知。
  • If some file exists in only one of the two snapshots, Git will—usually 1 —tell you that the file was created (exists in yyy , missing in xxx ) or deleted (exists in xxx , missing in yyy ).如果某个文件只存在于两个快照之一中,Git 会——通常是1——告诉你该文件是被创建的(在yyy存在,在xxx丢失)或被删除(在xxx存在,在yyy丢失)。
  • If the file exists in both snapshots, Git will (again, usually) tell you that the file was modified, and show you some set of changes that, if applied to the left-side ( xxx ) copy, will produce the right-side ( yyy ) copy.如果文件在两个快照中都存在,Git 将(再次,通常)告诉您该文件已被修改,并向您显示一些更改,如果应用于左侧 ( 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.所以提交FG之间的变化,我们刚才说的是feature分支的变化......好吧,它们仍然存在,在FG之间,但现在它们是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.

相关问题 如何找到我的git分支将提交到的SVN分支? - How can I find what SVN branch my git branch will dcommit to? 如何找到 gcloud 上部署的分支(git hash)? - How do I find what branch (git hash) is deployed on gcloud? 尝试合并git branch并在某些文件中获取&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD。 这是什么意思,我该如何修复? - Try to merge git branch and get <<<<<<< HEAD in some files. What does it mean and how can I repair this? 我刚刚参加了什么git分支? - What git branch was I just in? 搞砸了主git分支...无法弄清楚我需要恢复什么 - Screwed up the master git branch… Can't figure out what I need to revert 如果我在 Android Studio 3.3.4 中选择不同的分支与 Git 合并会发生什么? - What happened if I choose different branch to merge with Git in Android Studio 3.3.4? git submodules 是我需要的吗? - is git submodules what I need? 当“git branch”什么都不返回时我能得到什么信息 - What information can I get when "git branch" returns nothing 如何在不知道分支名称或跟踪内容的情况下获取要由`git pull`更新的文件列表? - How do I get the list of files about to be updated by `git pull` without knowing the branch name or what it tracks? GIT:在签出另一个分支之前,我是否需要提交我的分支,什么是存储? - GIT: Do I need to commit my branch before checking out another branch, what about stashing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM