git:复制一个版本以供玩耍 [英] git: Copy a version for playing around

查看:96
本文介绍了git:复制一个版本以供玩耍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发应用程序时,我希望在某一时刻拥有一份我的工作的单独副本,以尝试进行多项更改.这些更改并不意味着要落实!它只是在玩耍,尝试各种可能性.

While developing an application, I would like to have at one point a separate copy of my work for experimenting with several changes. Theses changes are not meant to be committed! It's just playing around, to try out various possibilities.

执行此操作的"最佳"方法是什么?我发现了以下可能性,但希望获得比git经验更多的人的建议:

What is the "best" way to do this? I found the following possibilities, but would like to get the advice of people who have more experience with git than I do:

  1. 我可以使用git clone将存储库的副本复制到另一个目录中,然后使用它来玩耍.
  2. 我可以将我当前的git工作目录cp -r移至新目录.
  1. I could use git clone to get a copy of the repository into a different directory, and use it to play around.
  2. I could just cp -r my current git working directory to the new one.

在情况(1)中,git知道该克隆,如果以后我真的愿意,我可以rebase,但是我不太可能这样做.

In case (1), git is aware about the clone, and I could later rebase if I really would want to, but it is unlikely that I will ever do this.

在这种情况下,我应该注意些什么,它使解决方案(1)或(2)更可取?还是有更好的方法呢?

In this case, is there something I should be aware of, which makes either solution (1) or (2) preferable? Or is there an even better way to do it?

推荐答案

TL; DR

使用克隆/副本

TL;DR

Using a clone/copy

  • 优点:主存储库及其克隆/副本是完整的孤岛
  • 缺点:空间效率低下

使用分支

  • 优点:空间效率高(Git分支便宜)
  • 缺点:如果需要大量隐藏,工作流程相对繁琐

您写

我希望在某一时刻拥有一份我的作品的单独副本,以尝试进行多项更改. [...]做到这一点的最佳"方法是什么?

I would like to have at one point a separate copy of my work for experimenting with several changes. [...] What is the "best" way to do this?

您还在您的评论中写道:很可能会在原始作品及其副本"之间来回切换,而无需对后者进行任何更改.

You also write in your comment that you're likely to switch back and forth between the original work and its "copy", without committing any changes carried out in the latter.

您在这里有不同的可能方法;让我们先回顾一下您的两个建议,然后再探讨第三种可能性.为了解决问题,我假设您的存储库位于一个名为main的目录中.

You have different possible approaches, here; let's review your two suggestions first, and then explore a third possibility. To fix ideas, I'll assume your repository lives in a directory called main.

如您所建议,您可以将main的副本生成到名为exp的目录中,

As you suggest, you could produce a clone of main, say, into a directory called exp,

git clone <path-to-main> <path-to-exp>

,然后在exp中进行疯狂的实验. main存储库将是exp的上游",即expmain列为远程存储之一,名称为origin.因此,如果您在main中进行了更多提交,并希望使expmain中的最新信息保持最新,则可以从main获取到exp,然后在后者中合并或变基.

and conduct your crazy experiment in exp. The main repo would be exp's "upstream", i.e. exp would list main as one of its remotes under the name origin. Therefore, if you made more commits in main and wanted to keep exp up to date with what's going on in main, you could fetch from main to exp and then merge or rebase in the latter.

此方法的主要问题是空间效率低:由于克隆包含原始回购的全部历史记录,因此磁盘上会出现很多重复的文件.这可能是问题,也可能不是问题,具体取决于原始存储库的大小,但这是要考虑的问题.

The main problem with this approach is space inefficiency: because a clone carries the entire history of the original repo with it, you will get a lot of duplicate files on your disk. That may or may not be a problem, depending on the size of your original repo, but it's something to consider.

从本质上讲,这类似于克隆main,但是,除非您手动添加main作为exp的远程对象,否则您将无法从main获取到exp.与第一种方法相比,我认为这种方法没有任何优势(也许不包括意外地将内容从exp推送到main的风险).

That's essentially like cloning main but, unless you manually add main as a remote of exp, you won't be able to fetch from main to exp. I don't see any advantage in this approach compared to the first one (except, perhaps, that it doesn't involve the risk of accidentally pushing stuff from exp to main).

第三种方法是直接在main存储库内内创建一个新分支(我们称其为exp),

A third approach is to create a new branch (let's call it exp) directly within your main repo,

git branch exp

签出,

git checkout exp

并在那里进行实验.这种方法相对于其他两种方法的主要优点是节省空间:Git分支便宜,因为创建新分支不需要任何文件复制.

and conduct your experiment in there. The main advantage of this approach over the other two is space efficiency: Git branches are cheap, in that creating a new one doesn't involve any copying of files.

但是,如果您在检出exp时进行了更改但没有提交它们,则Git会将您的工作目录视为不干净",并且除非您执行以下操作,否则您将无法切换回您的master分支纠正这种情况.清理存储库的一种可能性是通过运行

However, if you make changes while exp is checked out but do not commit them, Git will deem your working directory "not clean", and won't allow you to switch back to your master branch until you remedy the situation. One possibility for cleaning your repository is to stash your uncommitted changes, by running

# while on exp, if there are uncommitted changes
git stash save

然后您可以切换回master,进行一些更改,提交更改,切换回exp,然后通过运行来检索隐藏的更改

You could then switch back to master, do some changes, commit them, switch back to exp, and retrieve your stashed changes by running

git stash pop

但是,如果涉及大量隐藏(savepop),您可能会发现此工作流程过于繁琐和笨拙...

If a lot of stashing (save and pop) is involved, though, you may find this workflow too tedious and unwieldy...

这篇关于git:复制一个版本以供玩耍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆