Git 克隆特定版本的远程存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3555107/
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-10 08:56:41  来源:igfitidea点击:

Git clone particular version of remote repository

gitgit-clone

提问by nandu

I cloned a remote git repository about a month ago. The remote repository has undergone many changes and has now become unstable. Now I need another copy of the repository, version identical to the one I cloned a month ago.

大约一个月前,我克隆了一个远程 git 存储库。远程存储库经历了许多变化,现在变得不稳定。现在我需要另一个存储库副本,版本与我一个月前克隆的版本相同。

How do I do this?

我该怎么做呢?

回答by Rui Carneiro

You could "reset" your repository to any commit you want (e.g. 1 month ago).

您可以将您的存储库“重置”为您想要的任何提交(例如 1 个月前)。

Use git-resetfor that:

为此使用git-reset

git clone [remote_address_here] my_repo
cd my_repo
git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]

回答by M.Othman

You Can use simply

你可以简单地使用

git checkout  commithash

in this sequence

在这个序列中

git clone `URLTORepository`
cd `into your cloned folder`
git checkout commithash

commit hash looks like this "45ef55ac20ce2389c9180658fdba35f4a663d204"

提交哈希看起来像这样“45ef55ac20ce2389c9180658fdba35f4a663d204”

回答by jweyrich

Use git logto find the revision you want to rollback to, and take note of the commit hash. After that, you have 2 options:

使用git log找到你想要回滚到修改,并注意到提交哈希值。之后,您有两个选择:

  1. If you plan to commit anything after that revision, I recommendyou to checkout to a new branch: git checkout -b <new_branch_name> <hash>

  2. If you don't plan to commit anything after that revision, you can simply checkout without a branch: git checkout <hash>- NOTE: This will put your repository in a 'detached HEAD' state, which means its currently not attached to any branch - then you'll have some extra work to merge new commits to an actual branch.

  1. 如果您打算在该修订后提交任何内容,我建议您结帐到一个新分支:git checkout -b <new_branch_name> <hash>

  2. 如果您不打算在该修订后提交任何内容,则可以在没有分支的情况下简单地签出:git checkout <hash>- 注意:这将使您的存储库处于“分离的 HEAD”状态,这意味着它当前未附加到任何分支 - 那么您将有一些额外的工作将新提交合并到实际分支

Example:

例子:

$ git log
commit 89915b4cc0810a9c9e67b3706a2850c58120cf75
Author: Jardel Weyrich <suppressed>
Date:   Wed Aug 18 20:15:01 2010 -0300

    Added a custom extension.

commit 4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7
Author: Jardel Weyrich <suppressed>
Date:   Wed Aug 18 20:13:48 2010 -0300

    Missing constness.

$ git checkout 4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7
Note: moving to '4553c1466c437bdd0b4e7bb35ed238cb5b39d7e7'
which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 4553c14... Missing constness.

That way you don't lose any informations, thus you can move to a newer revision when it becomes stable.

这样你就不会丢失任何信息,因此当它变得稳定时你可以移动到更新的版本。

回答by unclechu

If that version you need to obtain is either a branch or a tag then:

如果您需要获取的版本是分支或标签,则:

git clone -b branch_or_tag_name repo_address_or_path

回答by Bruno

Unlike centralized version control systems, Git clones the entire repository, so you don't only get the current remote files, but the whole history. You local repository will include all this.

与集中式版本控制系统不同,Git 会克隆整个存储库,因此您不仅可以获取当前的远程文件,还可以获取整个历史记录。您的本地存储库将包含所有这些。

There might have been tagsto mark a particular version at the time. If not, you can create them yourself locally. A good way to do this is to use git logor perhaps more visually with tools like gitk(perhaps gitk --allto see all the branches and tags). If you can spot the commits hashes that were used at the time, you can tag them using git tag <hash>and then check those out in new working copies (for example git checkout -b new_branch_name tag_nameor directly with the hash instead of the tag name).

当时可能有标记特定版本的标签。如果没有,您可以在本地自己创建它们。这样做的一个好方法是使用git log或更直观的工具,例如gitk(也许gitk --all可以查看所有分支和标签)。如果您可以发现当时使用的提交哈希,则可以使用标记它们git tag <hash>,然后在新的工作副本中检查它们(例如git checkout -b new_branch_name tag_name或直接使用哈希而不是标记名称)。

回答by Ghislain Zabatio

You can solve it like this:

你可以这样解决:

git reset --hard sha

where shae.g.: 85a108ec5d8443626c690a84bc7901195d19c446

其中,sha例如:85a108ec5d8443626c690a84bc7901195d19c446

You can get the desired sha with the command:

您可以使用以下命令获取所需的 sha:

git log

回答by Ghislain Zabatio

Probably git resetsolves your problem.

大概git reset可以解决你的问题。

git reset --hard -#commit hash-

回答by gpampara

The source tree you are requiring is still available within the git repository, however, you will need the SHA1 of the commit that you are interested in. I would assume that you can get the SHA1 from the current clone you have?

您需要的源代码树在 git 存储库中仍然可用,但是,您将需要您感兴趣的提交的 SHA1。我假设您可以从您拥有的当前克隆中获取 SHA1?

If you can get that SHA1, the you can create a branch / reset there to have the identical repository.

如果您可以获得该 SHA1,您可以在那里创建一个分支/重置以拥有相同的存储库。

Commands as per Rui's answer

根据 Rui 的回答命令