简体   繁体   English

克隆一个 git repo 并保持原来的不变

[英]Cloning a git repo and keeping the original unchanged

I'm trying to expand on a project I previously worked on while keeping the original unchanged.我正在尝试扩展我以前从事的项目,同时保持原始项目不变。 How would I do this?我该怎么做? Was told to simply clone the original git repo to another one but it's unclear to me whether once I make changes to the code if this will affect the original repo and project.被告知只需将原始 git repo 克隆到另一个,但我不清楚一旦我对代码进行更改是否会影响原始 repo 和项目。

Unless you explictly push to the original, no local changes will apply to the original.除非您明确推送到原始文件,否则不会对原始文​​件应用任何本地更改。 That is one of the fundamental features of git: Only push will actually change a remote repository.这是 git 的基本特性之一:只有push才会真正改变远程存储库。 Any commit/branch/revert/... is purely local until you distribute those actions via a push (or a pull from the other side).任何 commit/branch/revert/... 都是纯本地的,直到您通过推送(或从另一端拉取)分发这些操作。

If you want to make sure that you don't accidentally push to the original, you can remove the remote after you cloned it (of course that also means you won't be able to pull new changes from there until you re-add it).如果您想确保不会意外推送到原始版本,则可以在克隆后 删除遥控器(当然这也意味着您将无法从那里提取新更改,直到您重新添加它) )。

Alternatively you can keep the remote (so you're able to keep pulling), but configure the push URL to something invalid, as described in this question .或者,您可以保留遥控器(因此您可以继续拉动),但将推送 URL 配置为无效的内容,如本问题所述

You can just clone a repository and then make a new repository from the place you cloned the original one.您可以只克隆一个存储库,然后从您克隆原始存储库的位置创建一个新存储库。 Then you can clone that new repository somewhere else and you can make changes there.然后,您可以在其他地方克隆该新存储库,并可以在那里进行更改。 But why do you need 2 repositories?但是为什么需要 2 个存储库? Consider using branches考虑使用分支

您可以对存储库进行分支以保留两个独立的项目。

After cloning a repo, there are two distinct repos located at two different places.克隆一个 repo 后,有两个不同的 repos 位于两个不同的地方。 Nothing you do on any of them will affect the other.你对其中任何一个所做的任何事情都不会影响另一个。 The only relation between the two is the cloned repo will have one of its remotes set to point to the original repo — named origin .两者之间的唯一关系是克隆的repo 将其远程之一设置为指向原始repo - 命名为origin You can list them with git remote -v in the cloned repo — -v is for --verbose , shows the url.您可以在克隆的存储库中使用git remote -v列出它们 - -v用于--verbose ,显示网址。

Without further settings, the only way the clone repo can effect the original repo is when you do a git push from the cloned repo.如果没有进一步的设置,克隆repo 可以影响原始repo 的唯一方法是当您从克隆的repo 执行git push

You can even cut the tie by removing the origin remote from the cloned repo with git remote remove origin .您甚至可以通过使用git remote remove origin克隆的repo 中删除origin remote 来切断关系。 Now there is no where to push to.现在无处可推。 But this is usually not what you want.但这通常不是您想要的。 When the original repo has changes (fixes, updates, ...), you will mostly want those changes in the cloned too.原始repo 发生更改(修复、更新等)时,您通常也希望克隆中的这些更改。 This is when you might do a git fetch or git pull in the cloned repo to bring over those changes.这是您可能会在克隆的存储库中执行git fetchgit pull以带来这些更改的时候。

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

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