简体   繁体   English

git差异在git克隆后无法正常工作

[英]git difference does not work properly after git clone

I have a bash script in which I have to clone and get difference of some repositories.我有一个 bash 脚本,我必须在其中克隆并获取一些存储库的差异。 I am trying to get difference between range of a date我正在尝试获取日期范围之间的差异

git clone $repository 
cd $path
git diff master@{2019-10-1}..master@{2019-10-14} -- package.json

but it shows error: warning: Log for 'master' only goes back to Tue, 15 Oct 2019 09:51:16 +0000.但它显示错误:警告:'master' 的日志仅返回 2019 年 10 月 15 日星期二 09:51:16 +0000。

But this repository is old and has many commits.但是这个存储库很旧并且有很多提交。 when I do it locally on the machine in which I had cloned same repository some weeks back I get proper difference.当我在几周前克隆相同存储库的机器上本地执行此操作时,我得到了适当的区别。

$ git diff master@{2019-10-1}..master@{2019-10-14} -- package.json
diff --git a/package.json b/package.json
index d29ffcb..8766fde 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "accountd",
-  "version": "0.0.95",
+  "version": "0.0.102",
   "main": "dist/src/index.js",
   "private": true,
   "scripts": {
@@ -8,7 +8,8 @@
     "start-dev": "npm run build && nodemon . port=5000 stage",
     "start-sand": "npm run build && nodemon . port=5000 stage",
-    "test": "nyc --extension .ts --reporter=html --reporter=cobertura --reporter=text mocha -r ts-node/register src/**/*.spec.ts --exit",
+    "test": "mocha -r ts-node/register test/**/*.spec.ts  test/**/**/*.spec.ts --exit",

I want the changes made in this specific file over a period of time.我希望在一段时间内对此特定文件所做的更改。

The notation you're using ( master@{<date>} ) says that you want to refer to the version of master on <date> according to the local reflogs .您使用的符号( master@{<date>} )表示您想根据本地 reflogs 引用<date>上的master版本。 That is, you're saying that you want to know what this particular clone's master ref pointed to on that date - not what of the current master commits had been committed on that date.也就是说,您是说您想知道该特定克隆的master引用在该日期指向的内容 - 而不是在该日期提交的当前master提交中的哪些内容。

And git is telling you "this clone didn't have a master ref on that date". git 告诉你“这个克隆在那个日期没有master参考”。

To do what you mean, you first have to find the last commit before the "then' date, then diff against that. There are a number of ways, but something like要做到你的意思,你首先必须找到“当时”日期之前的最后一次提交,然后对此进行diff 。有很多方法,但类似

git diff $(git rev-list -n1 --before="<date>" --first-parent master) master

might be more what you want可能是你想要的更多

The @{date} syntax will not get you what you want. @{date}语法不会得到你想要的。 I'd go into what you do want, but I got beaten to the answer;我会 go 变成想要的,但我被打败了; see Mark Adelsberger's answer instead.请参阅Mark Adelsberger 的答案 :-) :-)

What to know about the @{...} syntax关于@{...}语法的知识

With the @{date} syntax, it doesn't matter how old the repository source is.使用@{date}语法,存储库源的年龄无关紧要。 It does not matter how many commits you have.你有多少次提交并不重要。 All that matters is what is in this particular clone's so-called reflogs .重要的是这个特定克隆的所谓reflogs中的内容。

Every Git repository—every clone—has its own reflogs.每个 Git 存储库(每个克隆)都有自己的引用日志。 In general, no two different Git repositories will agree as to what goes into the reflogs.一般来说,没有两个不同的 Git 存储库会就 reflog 中的内容达成一致。 The reflogs are not meant for this kind of job. reflogs 不适合这种工作。 Which kind of job?什么样的工作? This kind:这种

I want the changes made in this specific file over a period of time.我希望在一段时间内对此特定文件所做的更改。

The reflogs tell you absolutely nothing about changes in files, or changes not in files, over time.随着时间的推移,reflogs 绝对不会告诉您有关文件更改或文件未更改的任何信息。 They tell you about changes in your Git's references over time.它们会告诉您 Git引用随时间的变化。

This may leave you puzzling over what, precisely, a ref or reference is.这可能会让您对refreference究竟是什么感到困惑。 We probably should not go into too much detail here, but every branch name like master is actually a reference whose full spelling is refs/heads/master ;我们可能不应该在这里对 go 进行太多详细说明,但是像master这样的每个分支名称实际上都是一个参考,其完整拼写是refs/heads/master every tag name like v1.2 is a ref whose full spelling is refs/tags/v1.2 ;每个像v1.2这样的标签名称都是一个 ref,其完整拼写是refs/tags/v1.2 and every remote-tracking name like origin/master is a ref whose full spelling starts with refs/remotes/ .并且像origin/master这样的每个远程跟踪名称都是一个 ref,其完整拼写以refs/remotes/开头。 So they're just a generalization of the various names that humans tend to use, to talk about commits.所以它们只是人类倾向于使用的各种名称的概括,用于谈论提交。 What Git needs, to talk about commits, is their raw hash IDs.谈到提交, Git需要的是它们的原始 hash ID。 The names are, to some extent, just there so that us weak humans don't have to remember arbitrary, big ugly hash IDs.在某种程度上,这些名称只是为了让我们弱者不必记住任意、又大又丑的 hash ID。 1 1

The key to understanding this, and the @{...} syntax, is to realize that these names—eg, refs/heads/masterchange over time.理解这一点以及@{...}语法的关键是要认识到这些名称——例如, refs/heads/master随着时间而改变。 Right now, your master commit might be a123456... .现在,您的master提交可能是a123456... Yesterday, it might have been some other commit, and tomorrow, it might be yet another commit.昨天,它可能是另一个提交,明天,它可能是另一个提交。 Your Git's master will change over time, and every time it does change, your Git keeps a record of what it was .你的Git 的master会随着时间的推移而改变,每次它确实改变时,你的Git 都会记录它什么。 This record only goes back so far: the commits are permanent, but the record of which hash ID master meant, when, is not.这条记录只能追溯到到目前为止:提交是永久的,但 hash ID master的记录是什么时候,不是。 Moreover, it's not carried from one clone to another: Every clone's record of what master meant when is private to that one particular Git .此外,它不会从一个克隆传送到另一个克隆:每个克隆记录的master何时表示什么是该特定 Git 私有的 In a fresh clone—which has all the commits 2 —there's only one value master has ever had, which is what it has right now.在一个包含所有提交2的新克隆中,只有一个master曾经拥有的值,这就是它现在拥有的值。

Note that you can also use @{ number } .请注意,您也可以使用@{ number } This selects the number'th entry in the reflog.这将选择 reflog 中的第 number 个条目。

To view the actual reflog for any particular ref, run git reflog ref , eg, git reflog master .要查看任何特定 ref 的实际 reflog,请运行git reflog ref ,例如git reflog master The git reflog command has various other sub-commands for dealing with the logs, too. git reflog命令还有其他各种处理日志的子命令。 See its documentation for details.有关详细信息,请参阅其文档


1 The names do have other functions. 1名称确实有其他功能。 For much more about this, see Think Like (a) Git .有关这方面的更多信息,请参阅Think Like (a) Git

2 Assuming it's not a shallow clone, that is, and not using some of the new "promisor pack" features that are not yet in general use. 2假设它不是浅层克隆,也就是说,没有使用一些尚未普遍使用的新“promisor pack”功能。 Also, if you cloned with -b or --single-branch , or the upstream repository is set up in a less-usual fashion, you might not have a branch named master yet at all.此外,如果您使用-b--single-branch进行克隆,或者上游存储库以不常见的方式设置,您可能根本没有名为master的分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM