git 为新项目创建一个新的空分支

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

Creating a new empty branch for a new project

gitbranchgit-branch

提问by fazineroso

We are using a git repository to store our project. We have our branches departing from the original branch. But now we want to create a small new project to track some documentation. For that we would want to create a new empty branch to start storing our files, and I would want other users of the network to clone that branch.

我们正在使用 git 存储库来存储我们的项目。我们有我们的分支离开原来的分支。但是现在我们想创建一个小的新项目来跟踪一些文档。为此,我们希望创建一个新的空分支来开始存储我们的文件,并且我希望网络的其他用户克隆该分支。

How can we do that?

我们怎么做?

I tried some things, but they didn't work.

我尝试了一些东西,但它们不起作用。

$ mkdir proj_doc; cd proj_doc
$ git init
$ git add .
$ git commit -m 'first commit'
$ git br proj_doc
$ git co proj_doc
$ git br -d master
$ git push origin proj_doc

It seems to push the branch ok, but when I do a fetch or pull, it downloads information from other branches, and then I also get some extra files from other projects. What's the best solution?

似乎推分支正常,但是当我执行 fetch 或 pull 时,它会从其他分支下载信息,然后我还会从其他项目中获取一些额外的文件。最好的解决办法是什么?

回答by Hiery Nomus

You can create a branch as an orphan:

您可以创建一个分支作为孤儿:

git checkout --orphan <branchname>

This will create a new branch with no parents. Then, you can clear the working directory with:

这将创建一个没有父级的新分支。然后,您可以使用以下命令清除工作目录:

git rm --cached -r .

and add the documentation files, commit them and push them up to github.

并添加文档文件,提交它们并将它们推送到 github。

A pull or fetch will always update the local information about all the remote branches. If you only want to pull/fetch the information for a single remote branch, you need to specify it.

拉取或提取将始终更新有关所有远程分支的本地信息。如果您只想拉/取单个远程分支的信息,则需要指定它。

回答by Sid Jain

The correct answer is to create an orphan branch. I explain how to do this in detail on my blog.(Archived link)

正确答案是创建一个孤儿分支。我在我的博客上详细解释了如何做到这一点(存档链接)

...

Before starting, upgrade to the latest version of GIT. To make sure you're running the latest version, run

which git

If it spits out an old version, you may need to augment your PATH with the folder containing the version you just installed.

Ok, we're ready. After doing a cd into the folder containing your git checkout, create an orphan branch. For this example, I'll name the branch “mybranch”.

git checkout --orphan mybranch

Delete everything in the orphan branch

git rm -rf .

Make some changes

vi README.txt

Add and commit the changes

git add README.txt
git commit -m "Adding readme file"

That's it. If you run

git log

you'll notice that the commit history starts from scratch. To switch back to your master branch, just run

git checkout master

You can return to the orphan branch by running

git checkout mybranch

...

在开始之前,升级到最新版本的 GIT。要确保您运行的是最新版本,请运行

which git

如果它吐出旧版本,您可能需要使用包含您刚刚安装的版本的文件夹来扩充您的 PATH。

好的,我们准备好了。将 cd 放入包含 git checkout 的文件夹后,创建一个孤立分支。在这个例子中,我将分支命名为“mybranch”。

git checkout --orphan mybranch

删除孤儿分支中的所有内容

git rm -rf .

做一些改变

vi README.txt

添加并提交更改

git add README.txt
git commit -m "Adding readme file"

就是这样。如果你跑

git log

你会注意到提交历史是从头开始的。要切换回您的主分支,只需运行

git checkout master

您可以通过运行返回到孤儿分支

git checkout mybranch

回答by jthill

Make an empty new branch like this:

像这样创建一个空的新分支:

true | git mktree | xargs git commit-tree | xargs git branch proj-doc

If your proj-doc files are already in a commit under a single subdir you can make the new branch this way:

如果您的 proj-doc 文件已经在单个子目录下的提交中,您可以通过这种方式创建新分支:

git commit-tree thatcommit:path/to/dir | xargs git branch proj-doc

which might be more convenient than git branch --orphanif that would leave you with a lot of git rmand git mving to do.

这可能是比更方便git branch --orphan,如果这样做会让你有很多的git rmgit mv荷兰国际集团做。

Try

尝试

git branch --set-upstream proj-doc origin/proj-doc

and see if that helps with your fetching-too-much problem. Also if you really only want to fetch a single branch it's safest to just specify it on the commandline.

看看这是否有助于解决获取过多的问题。此外,如果您真的只想获取单个分支,最安全的方法是在命令行中指定它。

回答by cyb0k

If your git version does not have the --orphan option, this method should be used:

如果您的 git 版本没有 --orphan 选项,则应使用此方法:

git symbolic-ref HEAD refs/heads/<newbranch> 
rm .git/index 
git clean -fdx 

After doing some work:

做了一些工作后:

git add -A
git commit -m <message>
git push origin <newbranch>

回答by user1723157

Let's say you have a masterbranch with files/directories:

假设您有一个master包含文件/目录的分支:

> git branch  
master
> ls -la # (files and dirs which you may keep in master)
.git
directory1
directory2
file_1
..
file_n

Step by step how to make an empty branch:

一步一步如何制作一个空分支:

  1. git checkout —orphan new_branch_name
  2. Make sure you are in the right directory before executing the following command:
    ls -la |awk '{print $9}' |grep -v git |xargs -I _ rm -rf ./_
  3. git rm -rf .
  4. touch new_file
  5. git add new_file
  6. git commit -m 'added first file in the new branch'
  7. git push origin new_branch_name
  1. git checkout —orphan new_branch_name
  2. 在执行以下命令之前,请确保您位于正确的目录中:
    ls -la |awk '{print $9}' |grep -v git |xargs -I _ rm -rf ./_
  3. git rm -rf .
  4. touch new_file
  5. git add new_file
  6. git commit -m 'added first file in the new branch'
  7. git push origin new_branch_name

In step 2, we simply remove all the files locally to avoid confusion with the files on your new branch and those ones you keep in masterbranch. Then, we unlink all those files in step 3. Finally, step 4 and after are working with our new empty branch.

在第 2 步中,我们只是在本地删除所有文件,以避免与新分支上的文件和您保留在master分支中的文件混淆。然后,我们在第 3 步中取消所有这些文件的链接。最后,第 4 步及之后的工作是使用我们新的空分支。

Once you're done, you can easily switch between your branches:

完成后,您可以轻松地在分支之间切换:

git checkout master 
git checkout new_branch

回答by Kamal Hossain

Check out this documentation, it has clear details about

查看这个文档,它有关于

--orphan <new_branch>
Create a new orphan branch, 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.

The index and the working tree are adjusted as if you had previously run git checkout . This allows you to start a new history that records a set of paths similar to by easily running git commit -a to make the root commit.

--orphan <new_branch>
创建一个新的孤立分支,命名为<new_branch>,从<start_point>它开始并切换到它。在这个新分支上进行的第一次提交将没有父级,它将成为与所有其他分支和提交完全断开的新历史的根。

索引和工作树被调整,就像你之前运行过 git checkout 一样。这允许您开始一个新的历史记录,记录一组类似于通过轻松运行 git commit -a 进行根提交的路径。

回答by Liner

On base this answer from Hiery Nomus.

基于Hiery Nomus 的这个答案

You can create a branch as an orphan:

您可以创建一个分支作为孤儿:

git checkout --orphan <branchname>

This will create a new branch with no parents. Then, you can clear the working directory with:

这将创建一个没有父级的新分支。然后,您可以使用以下命令清除工作目录:

git rm --cached -r .

And then you just commit branch with empty commit and then push

然后你只需提交带有空提交的分支,然后推送

git commit -m <commit message> --allow-empty
git push origin <newbranch>