准备工作,clone 项目,然后进入项目目录。
1.创建一个(空)分支
使用参数 --orphan
,这个参数的主要作用有两个,一个是拷贝当前所在分支的所有文件,另一个是没有父结点,可以理解为没有历史记录,是一个完全独立背景干净的分支。
使用 git help checkout
命令可以看到相关介绍:
--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 <start_point>". This allows you to start a new history that records a set of paths similar to <start_point> by easily running "git commit -a" to make the root commit.
This can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code.
If you want to start a disconnected history that records a set of paths that is totally different from the one of <start_point>, then you should clear the index and the working tree right after creating the orphan branch by running "git rm -rf ." from the top level of the working tree. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc.
具体命令如下:
# 创建一个 xxx 的分支,这个分支是独立的
git checkout --orphan <新的分支名>
# Switched to a new branch '<新的分支名>'
如果提示
error: The following untracked working tree files would be overwritten by checkout:
则执行git clean -d -fx
,该操作会清除所有git clone下的所有文件,只剩.git目录。
然后再重新执行一遍git checkout --orphan <新的分支名>
2.清空当前分支下的所有文件
这个操作不会影响别的分支,特别是你的<start point>
。
#清除所有git文件历史,为了空白分支
git rm -rf .
git commit -m "提交信息"
3.push 新建的分支到远程仓库
#推送远程分支
git push origin <新的分支名>
如果提示错误:
error: src refspec hexo does not match any.
这是因为不能提交空分支,解决方法:重新添加文件,提交分支。
# 比如添加一个新的 README.md
git add README.md
git commit -m "提交信息"
git push origin <新的分支名>
至此,创建新分支操作已经完成,可以在 github/gitee 页面查看。
评论区