Git:如何克隆第一次提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3092925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:21:30  来源:igfitidea点击:

Git: How to clone the first commit?

git

提问by igniteflow

Could anyone tell me how to clone the first commit? I don't want to delete recent commits, just make a clone copy of the initial state so I can grab some of the files.

谁能告诉我如何克隆第一次提交?我不想删除最近的提交,只需制作初始状态的克隆副本,这样我就可以获取一些文件。

采纳答案by Cascabel

To answer the question I think you meant to ask:

要回答这个问题,我认为您的意思是:

You can get your entire repository into the state of the first commit with:

您可以使用以下命令使整个存储库进入第一次提交的状态:

git checkout <commit SHA1>

After you're done messing around, you can do git checkout masterto get back to where you were.

在你搞砸之后,你可以git checkout master回到你原来的地方。

And you can get individual files into their state from the first commit with:

您可以使用以下命令从第一次提交时将单个文件置于其状态:

git checkout <commit SHA1> <file or directory> ...

Again, after you're done, you can do git checkout master <file or directory>to get back to where you were.

同样,在你完成后,你可以做git checkout master <file or directory>回到你原来的地方。

There's no need to "clone" a commit (by which I assume you mean clone the repository, and check out the first commit?). Of course, if for some reason you couldn't bear to modify any files in your repository (e.g. don't want to make your build out of date) you could of course clone it, then do the exact same thing in the cloned repo.

没有必要“克隆”一个提交(我假设你的意思是克隆存储库,并检查第一个提交?)。当然,如果由于某种原因您不忍修改存储库中的任何文件(例如,不想使您的构建过时),您当然可以克隆它,然后在克隆的存储库中执行完全相同的操作.

回答by mpe

Updated 2018 answer: Use your own symbolic ref, ie. tag. And start using describeand annotated tags.

2018 年更新的答案:使用您自己的符号参考,即。标签。并开始使用describe和注释标签。

git tag -a -m "Initial version" root $( git rev-list --all | tail -n 1 )
git describe

Output description for a repo that never received a tag but occasionally committed to since 2011 with above tag:

从未收到标签但偶尔使用上述标签自 2011 年以来提交的存储库的输出描述:

root-1090-g88a80e93

If you want to discard all other history on the other hand I've never needed that, it is easy enough to copy the initial set. I can see the use-case if you wanted to refer to the original 'root' commit and stamps though. Maybe look at how ie. BFG work, --depthor GIT 2.19 its new clone --filter. [3]

另一方面,如果您想丢弃所有其他历史记录,我从不需要它,复制初始集很容易。如果您想参考原始的“根”提交和标记,我可以看到用例。也许看看如何ie。BFG 工作,--depth或 GIT 2.19 它的新clone --filter. [3]



I think I want to do the same thing. Checkout the initial commit, and branch from there. I'm not sure with the answer accepted. But I guess it could (partly) fullfil the original question. After reading this thread, I however will go with some bash scripting around

我想我想做同样的事情。签出初始提交,并从那里分支。我不确定是否接受了答案。但我想它可以(部分)完成最初的问题。阅读此线程后,我将使用一些 bash 脚本

git log --pretty=oneline  master | tail -1

I hoped there would be some commit- or tree-ish reference for this. Maybe to list nodes marked as root, but I suppose GIT by itself does not track this. So this method only works on a repository with only one root commit.

我希望对此会有一些提交或树状参考。也许列出标记为 root 的节点,但我认为 GIT 本身不会跟踪这一点。所以这个方法只适用于只有一个 root 提交的存储库。

The 2017 and 2018 updates are that there is a better option to git log, using git rev-list but that suffer from the same problem. They simply use the trailing item of a list of commits still.

2017 年和 2018 年的更新是 git log 有更好的选择,使用 git rev-list 但遇到同样的问题。他们仍然只是使用提交列表的尾随项目。

You can checkout the 'root commit' for a repository with only one root with the following, and it works for most of the repositories. But it is a hack, and it would be better to review your system why and where it needs to mark the root commits. And simply tag it or record it.

您可以使用以下内容为只有一个根的存储库签出“根提交”,并且它适用于大多数存储库。但这是一种黑客行为,最好检查您的系统为什么以及在哪里需要标记根提交。只需标记或记录即可。

You can checkout the first commit in a repo with one root with the following command:

您可以使用以下命令签出具有一个 root 的 repo 中的第一个提交:

git checkout $( git rev-list --all | tail -n 1 )

or (GIT <1.8.1):

或(GIT <1.8.1):

git checkout $( git log --pretty=oneline | tail -n 1 | sed 's/ .*$//' )

See git-revlistand also Using git, how do I go back to first commit then go through the history?.

请参阅git-revlist以及使用 git,我如何返回到第一次提交然后查看历史记录?.

[3]:

[3]:

回答by Michael Erickson

If by "first commit", you mean the parent commit of a branch that does not have any parents itself, then you want to use the git-rev-list to do that. Just use

如果“第一次提交”是指一个本身没有任何父级的分支的父级提交,那么你想使用 git-rev-list 来做到这一点。只需使用

git rev-list --max-parents=0 HEAD

git rev-list --max-parents=0 HEAD

That will get your "Initial commit" for the current HEAD. Use master if you list, or any other child revision specification. wrap that in $(...)in order to use that in an expression, such as:

这将获得当前 HEAD 的“初始提交”。如果列出,请使用 master 或任何其他子修订规范。将它包装起来$(...)以便在表达式中使用它,例如:

git checkout $(git rev-list --max-parents=0 HEAD)

git checkout $(git rev-list --max-parents=0 HEAD)

回答by ramwin

Use command:

使用命令:

git fetch --depth=1 ../testrepo/.git $SHA1

This will only clone the certain commit. You can checkout this answerfor more details.

这只会克隆特定的提交。您可以查看此答案以获取更多详细信息。

回答by brycemcd

I think all you need to do is create a new branch from the original commit you want, check that out, and then merge back into the HEAD of the branch you're working on.

我认为您需要做的就是从您想要的原始提交创建一个新分支,检查它,然后合并回您正在处理的分支的 HEAD。

If abcxyz... is the SHA1 of the commit you want and you're working in the master branch, this is generally what you'd want to do:

如果 abcxyz... 是您想要的提交的 SHA1 并且您正在主分支中工作,这通常是您想要做的:

git branch oldskool abcxyz... # creates a new branch called oldskool from the commit
git checkout oldskool
#make changes
git rebase master #bring the oldskool branch up to the HEAD of the master branch (shouldn't overwrite the changes in the files you edited)
git checkout master
git merge oldskool