git 如何开始一个没有祖先的干净分支,然后逐步提交文件?

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

How can I start a clean branch with no ancestry, then commit files progressively?

gitbranch

提问by Javis

I have a PHP framework versioned with GIT and I'm planning several (drastic) changes to its core.

我有一个用 GIT 版本化的 PHP 框架,我正计划对其核心进行几项(重大)更改。

What I want to do is to start working on the new core in a new branch, but since this change might require some restructuring on the filesystem too, I'd like to start this new branch as cleanly as possible.

我想要做的是在新分支中开始处理新核心,但由于此更改也可能需要对文件系统进行一些重组,因此我想尽可能干净地启动这个新分支。

I want the clean branch to include only with the core files. As I'm finishing my changes, I want to add the rest of the modules from the framework to the working directory one by one, but keep the ability to merge if I do changes on master.

我希望干净的分支只包含核心文件。当我完成更改时,我想将框架中的其余模块一一添加到工作目录中,但如果我在 master 上进行更改,则保留合并的能力。

How can I do that?

我怎样才能做到这一点?

回答by Todd A. Jacobs

Branch with No Ancestors

没有祖先的分支

You want the --orphanflag. For example:

你想要--orphan标志。例如:

git checkout master
git checkout --orphan foo

# Unstage all the files in your working tree.
git rm --cached $(git ls-files)

will create a new branch named foowith no ancestors, but it will preserve the current working tree from whatever branch you were on when you called the command (in this case, the masterbranch). You can then modify your working tree to suit, and then make a commit to start that branch's history afresh.

将创建一个名为foo的新分支,没有祖先,但它会保留当前工作树从您调用命令时所在的任何分支(在本例中为master分支)。然后您可以修改您的工作树以适应,然后提交以重新开始该分支的历史记录。

Incremental Staging of Files

文件的增量暂存

To perform incremental additions to your history, use git addto stage just the files you want for each commit. The git-add(1) manual page has this to say about adding files selectively:

要对您的历史执行增量添加,请使用git add仅暂存您每次提交所需的文件。git-add(1) 手册页有关于选择性地添加文件的说明:

Fileglobs (e.g. *.c) can be given to add all matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively.

可以给出 Fileglobs(例如 *.c)来添加所有匹配的文件。还可以给出一个前导目录名称(例如,用于添加 dir/file1 和 dir/file2 的 dir)以递归地添加目录中的所有文件。

回答by Hans Ginzel

If you must work with an old version of git:

如果您必须使用旧版本的 git:

 mkdir some_dir
 cd some_dir
 git init
 echo a>some_file; git add some_file; git commit -m 'initial commit'
 cd ..
 git fetch ./some_dir/ master:new_independent_branch
 rm -rf some_dir

回答by Samy Dindane

What you can do is simply moving to a new branch, git co -b my_new_branch, clean up your code and keep the things you need, and finally commit. That commit, the first on my_new_branch, would then be a cleanone.

你可以做的就是简单地移动到一个新的分支,git co -b my_new_branch清理你的代码并保留你需要的东西,最后提交。那个提交,第一个提交,my_new_branch然后将是一个干净的提交。