在 git 中,是否有一种简单的方法可以将不相关的分支引入存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1384325/
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
In git, is there a simple way of introducing an unrelated branch to a repository?
提问by hillu
While helping a friend with a git problem today, I had to introduce a
branch that needed to be totally separate from the master
branch.
The contents of this branch really had a different origin from what
had been developed on the master
branch, but they were going to be
merged into the master
branch at a later time.
今天在帮朋友解决git问题的时候,不得不引入一个需要与分支完全分离的master
分支。这个分支的内容确实和master
分支上开发的内容有出处,不过以后会合并到master
分支中。
I remembered from reading John Wiegley's Git from the bottom uphow branches are essentially a label to a commit that follows a certain convention and how a commit is tied to a tree of files and, optionally to parent commits. We went to create a parentless commit to the existing repository using git's plumbing:
通过自下而上阅读 John Wiegley 的Git,我记得分支本质上是遵循特定约定的提交的标签,以及提交如何绑定到文件树以及可选的父提交。我们使用 git 的管道创建对现有存储库的无父提交:
So we got rid of all files in the index ...
所以我们删除了索引中的所有文件......
$ git rm -rf .
... extracted directories and files from a tarball, added those to the index ...
... 从 tarball 中提取目录和文件,将它们添加到索引 ...
$ git add .
... and created a tree object ...
...并创建了一个树对象...
$ git write-tree
(git-write-tree
told us the sha1sum of the created tree object.)
(git-write-tree
告诉我们创建的树对象的 sha1sum。)
Then, We committed the tree, without specifying parent commits...
然后,我们提交了树,没有指定父提交......
$ echo "Imported project foo" | git commit-tree $TREE
(git-commit-tree
told us the sha1sum of the created commit object.)
(git-commit-tree
告诉我们创建的提交对象的 sha1sum。)
... and created a new branch that points to our newly created commit.
...并创建了一个指向我们新创建的提交的新分支。
$ git update-ref refs/heads/other-branch $COMMIT
Finally, we returned to the master
branch to continue work there.
最后,我们回到master
分行继续工作。
$ git checkout -f master
This seems to have worked as planned. But this is clearly not the kind of procedure I would recommend to someone who is just getting started using git, to put it mildly. Is there an easier way of creating a new branch that is entirely unrelated to everything that has happened in the repository so far?
这似乎按计划进行。但这显然不是我会向刚开始使用 git 的人推荐的那种程序,说得客气一点。有没有一种更简单的方法来创建一个与存储库中迄今为止发生的一切完全无关的新分支?
回答by tcovo
There is a new feature (since V1.7.2) which makes this task a little more high-level than what's in any of the other answers.
有一个新功能(自 V1.7.2 起),它使此任务比任何其他答案中的内容更高级一点。
git checkout
now supports the --orphan
option. From the man page:
git checkout
现在支持该--orphan
选项。从手册页:
git checkout [-q] [-f] [-m] --orphan <new_branch> [<start_point>]
git checkout [-q] [-f] [-m] --orphan <new_branch> [<start_point>]
Create a new orphanbranch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.
创建一个新的孤立分支,名为 <new_branch>,从 <start_point> 开始并切换到它。在这个新分支上进行的第一次提交将没有父级,它将成为与所有其他分支和提交完全断开的新历史的根。
This doesn't do exactlywhat the asker wanted, because it populates the index and the working tree from <start_point>
(since this is, after all, a checkout command). The only other action necessary is to remove any unwanted items from the working tree and index. Unfortunately, git reset --hard
doesn't work, but git rm -rf .
can be used instead (I believe this is equivalent to rm .git/index; git clean -fdx
given in other answers).
这并不完全符合提问者的要求,因为它填充了索引和工作树<start_point>
(因为这毕竟是一个结帐命令)。唯一需要的其他操作是从工作树和索引中删除任何不需要的项目。不幸的是,git reset --hard
不起作用,但git rm -rf .
可以代替使用(我相信这等效rm .git/index; git clean -fdx
于其他答案中给出的)。
In summary:
总之:
git checkout --orphan newbranch
git rm -rf .
<do work>
git add your files
git commit -m 'Initial commit'
I left <start_point>
unspecified because it defaults to HEAD, and we don't really care anyway. This sequence does essentially the same thing as the command sequence in Artem's answer, just without resorting to scary plumbing commands.
我没有<start_point>
指定,因为它默认为 HEAD,无论如何我们都不在乎。该序列与Artem 的答案中的命令序列的作用基本相同,只是没有使用可怕的管道命令。
回答by Artem Tikhomirov
From Git Community Book:
来自Git 社区书:
git symbolic-ref HEAD refs/heads/newbranch
rm .git/index
git clean -fdx
<do work>
git add your files
git commit -m 'Initial commit'
回答by Jakub Nar?bski
Although the solution with git symbolic-ref
and removing index works, it might be conceptually cleanerto create newrepository
尽管使用git symbolic-ref
和删除索引的解决方案有效,但创建新存储库在概念上可能更清晰
$ cd /path/to/unrelated
$ git init
[edit and add files]
$ git add .
$ git commit -m "Initial commit of unrelated"
[master (root-commit) 2a665f6] Initial commit of unrelated
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
then fetch from it
然后从中获取
$ cd /path/to/repo
$ git fetch /path/to/unrelated master:unrelated-branch
warning: no common commits
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
From /path/to/unrelated
* [new branch] master -> unrelated-branch
Now you can delete /path/to/unrelated
现在您可以删除 /path/to/unrelated
回答by Greg Hewgill
Githubhas a feature called Project Pageswhere you can create a particular named branch in your project to provide files that will be served by Github. Their instructions are as follows:
Github有一个名为Project Pages的功能,您可以在其中创建一个特定的命名分支,以提供将由 Github 提供的文件。他们的指示如下:
$ cd /path/to/fancypants
$ git symbolic-ref HEAD refs/heads/gh-pages
$ rm .git/index
$ git clean -fdx
From there you have an empty repository which you can then add your new content to.
从那里您有一个空的存储库,然后您可以将新内容添加到其中。
回答by unknownprotocol
The currently selected answer is correct, I would just add that coincidentally...
当前选择的答案是正确的,我只是巧合地补充一下......
This is actually exactly how github.com lets users create Github Pages for their repos, thru an orphaned branch called gh-pages
.
The pretty steps are given and explained here:
这实际上正是 github.com 让用户为他们的存储库创建 Github Pages 的方式,通过一个名为gh-pages
. 这里给出并解释了漂亮的步骤:
https://help.github.com/articles/creating-project-pages-manually
https://help.github.com/articles/creating-project-pages-manually
Basically the git commands to set this up are as follows:
设置它的 git 命令基本上如下:
git checkout --orphan gh-pages
(create a parentless branch called gh-pages on your repo)git rm -rf .
(removes any files from branch working tree)rm '.gitignore'
(even the gitignore)- Now add website content (add index.html, etc) and commit and push.
- Profit.
git checkout --orphan gh-pages
(在您的 repo 上创建一个名为 gh-pages 的无父分支)git rm -rf .
(从分支工作树中删除任何文件)rm '.gitignore'
(即使是 gitignore)- 现在添加网站内容(添加 index.html 等)并提交和推送。
- 利润。
Note you can also designate a /docs folderon you repo to be the source of the "Project Site" that Github uses to build the website.
请注意,您还可以将存储库上的 /docs 文件夹指定为Github 用于构建网站的“项目站点”的来源。
Hope this helps!
希望这可以帮助!
回答by Sing
Sometimes I just want to create empty branch in project instantly then start doing work, I will just excute following command :
有时我只想立即在项目中创建空分支然后开始工作,我将执行以下命令:
git checkout --orphan unrelated.branch.name
git rm --cached -r .
echo "init unrelated branch" > README.md
git add README.md
git commit -m "init unrelated branch"
回答by VonC
If your existing content was already committed, you now (Git 2.18 Q2 2018) can extract it into its own new orphan branch, since the implementation of "git rebase -i --root
" has been updated to use
the sequencer machinery more.
如果您现有的内容已经提交,您现在(Git 2.18 Q2 2018)可以将其提取到自己的新孤立分支中,因为“ git rebase -i --root
”的实现已更新为更多地使用定序器机制。
That sequencer is the one now allowing to transplant the whole topology of commit graph elsewhere.
该排序器现在允许将提交图的整个拓扑结构移植到其他地方。
See commit 8fa6eea, commit 9c85a1c, commit ebddf39, commit 21d0764, commit d87d48b, commit ba97aea(03 May 2018) by Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
--in commit c5aa4bc, 30 May 2018)
请参阅Johannes Schindelin ( ) 的commit 8fa6eea、commit 9c85a1c、commit ebddf39、commit 21d0764、commit d87d48b、commit ba97aea(2018 年 5 月 3 日)。(由Junio C Hamano合并-- --在提交 c5aa4bc 中,2018 年 5 月 30 日)dscho
gitster
sequencer: allow introducing new root commits
In the context of the new
--rebase-merges
mode, which was designed specifically to allow for changing the existing branch topology liberally, a user may want to extract commits into a completely fresh branch that starts with a newly-created root commit.This is now possible by inserting the command
reset [new root]
beforepick
ing the commit that wants to become a root commit. Example:
定序器:允许引入新的根提交
在新
--rebase-merges
模式的上下文中,该模式是专门为允许自由更改现有分支拓扑而设计的,用户可能希望将提交提取到一个全新的分支中,该分支以新创建的根提交开始。现在可以通过
reset [new root]
在pick
想要成为根提交的提交之前 插入命令来实现。例子:
reset [new root]
pick 012345 a commit that is about to become a root commit
pick 234567 this commit will have the previous one as parent
This does not conflict with other uses of the
reset
command because[new root]
is not (part of) a valid ref name: both the opening bracket as well as the space are illegal in ref names.
这与该
reset
命令的其他用途不冲突,因为[new root]
它不是(部分)有效的引用名称:引用名称中的左括号和空格都是非法的。
回答by Beno?t
Found this script at http://wingolog.org/archives/2008/10/14/merging-in-unrelated-git-branchesand it works very fine !
在http://wingolog.org/archives/2008/10/14/merging-in-unrelated-git-branches找到这个脚本,它工作得很好!
#!/bin/bash
set -e
if test -z "" -o -n ""; then
echo "usage: ##代码## REPO BRANCHNAME" >&2
exit 1
fi
repo=
branch=
git fetch "$repo" "$branch"
head=$(git rev-parse HEAD)
fetched=$(git rev-parse FETCH_HEAD)
headref=$(git rev-parse --symbolic-full-name HEAD)
git checkout $fetched .
tree=$(git write-tree)
newhead=$(echo "merged in branch '$branch' from $repo" | git commit-tree $tree -p $head -p $fetched)
git update-ref $headref $newhead $head
git reset --hard $headref