git 如何在没有历史记录的情况下推送新分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12543055/
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
How to push new branch without history
提问by Tomas Pruzina
I have git repo with two unrelated branches, master and configs. I've created configs, purged all the files and then placed in configuration files only. Now I want to push this on remote repo, but as it were new, empty branch (without logs of all my changes and old revisions of original branch).
我的 git repo 有两个不相关的分支,master 和 configs。我已经创建了配置,清除了所有文件,然后仅将其放置在配置文件中。现在我想把它推送到远程仓库,但因为它是新的、空的分支(没有我所有更改的日志和原始分支的旧修订)。
How can I purge all the history and push it?
如何清除所有历史记录并推送它?
回答by John Szakmeister
Purging all the files doesn't get rid of the history. You need to create a branch that has no history first, and the add your config files. These days git checkout
has a --orphan
option that makes a branch with no history. Here's the information on the --orphan
option:
清除所有文件不会消除历史记录。您需要先创建一个没有历史记录的分支,然后添加您的配置文件。这些天git checkout
有一个--orphan
选项,可以创建一个没有历史的分支。以下是该--orphan
选项的信息:
--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.
--orphan <新分支>
创建一个新的孤立分支,名为 <new_branch>,从 <start_point> 开始并切换到它。在这个新分支上进行的第一次提交将没有父级,它将成为与所有其他分支和提交完全断开的新历史的根。
索引和工作树的调整就像您之前运行过“git checkout <start_point>”一样。这允许您通过轻松运行“git commit -a”进行根提交来开始记录一组类似于 <start_point> 的路径的新历史记录。
当您想从提交中发布树而不公开其完整历史记录时,这会很有用。您可能希望这样做以发布当前树是“干净”的项目的开源分支,但其完整历史包含专有或其他阻碍的代码位。
如果要开始记录一组与 <start_point> 路径完全不同的路径的断开历史记录,则应在创建孤立分支后立即通过运行“git rm -rf”清除索引和工作树。 ” 从工作树的顶层。之后,您将准备好准备新文件、重新填充工作树、从其他地方复制它们、提取 tarball 等。
Here's a link to the documentation for checkout. You can also run git help checkout
as well.
这是checkout文档的链接。你也可以跑步git help checkout
。
Once you've created your branch without history, then when you push it to the server, it won't have that history either. FWIW, it helps me to think of git push
as "make the remote branch look the same as my local one". So if you have history, and push, it will have history. If you don't, then the pushed branch won't.
一旦您创建了没有历史记录的分支,那么当您将其推送到服务器时,它也不会具有该历史记录。FWIW,它帮助我认为git push
“使远程分支看起来与我的本地分支相同”。所以如果你有历史,并且推动,它就会有历史。如果你不这样做,那么推送的分支就不会。
回答by Geremia
How can I purge all the history and push it?
如何清除所有历史记录并推送它?
git checkout --orphan <name_you_choose_for_orphan_branch>
git commit
Then you can run your
然后你可以运行你的
git push <remote-name> <branch-name>
参见 this one-liner这个单行
回答by ThinkBonobo
I needed to do this to send a freelancer some code without sharing private information or deleting my history.
我需要这样做才能向自由职业者发送一些代码,而无需共享私人信息或删除我的历史记录。
In case any of you are using SourceTree, this is how I managed to do it. Please let me know if I did something wrong or am forgetting something as I am very unfamiliar with source control management.
如果你们中的任何人正在使用 SourceTree,这就是我设法做到的。如果我做错了什么或忘记了什么,请告诉我,因为我对源代码控制管理非常不熟悉。
- I went to the terminal button on source tree.
- I created a new 'orphan' branch by using the command: 'git checkout --orphan clean-ver' where clean-ver is my branch name
- I closed the terminal and saw that my commit log was cleared because I was in a new orphan branch. I staged all my source code and committed the changes.
- I set up my bitbucket repository in either repository>add remote or repository>repository settings>add.
- I closed that, and clicked on the push button
- I selected my new 'clean-ver' branch said ok.
- 我去了源树上的终端按钮。
- 我使用以下命令创建了一个新的“孤儿”分支:“git checkout --orphan clean-ver”,其中 clean-ver 是我的分支名称
- 我关闭了终端,看到我的提交日志被清除了,因为我在一个新的孤儿分支中。我上演了我所有的源代码并提交了更改。
- 我在存储库>添加远程或存储库>存储库设置>添加中设置了我的bitbucket存储库。
- 我关闭了它,然后点击了按钮
- 我选择了我的新“clean-ver”分支,说好的。
When I checked bitbucket, it seems that this seemed to have worked and discarded my commit history.
当我检查 bitbucket 时,这似乎有效并丢弃了我的提交历史。
Hope this helps someone out.
希望这可以帮助某人。