本文介绍了将所有代码从master转移到新分支,然后从master删除代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码位于项目的master分支中.我想要将代码放在一个单独的分支中,而在主服务器中则不需要.我可以在master上创建一个新分支.但是在创建分支之后,是否有可能以一种方式从母版中删除所有代码,如果以后我将新分支重新建立基础或将其合并到母版中时,它不会引起任何问题?

谢谢

解决方案

这有点棘手,如果其他人已经拥有您提交的副本,则您将无法正确执行此操作. IE.如果您将master分支推送到其他人正在使用的远程存储库中,或者如果人们直接从您的存储库中拉出,则没有干净的方法来消除这种情况.

您必须重写master分支的历史记录.我们必须在两种情况下查看.根据您的问题,我假设在master中,有一个要在之前创建的提交,您要从母版中删除任何代码提交.令该提交的ID为rootcommit.如果您已经有一个新的代码分支(从master分支出来),请跳过第1步和第2步.

  1. git checkout master:请确保您在掌握中.
  2. git checkout -b my-fancy-new-branch:创建新的功能分支.
  3. git checkout master:切换回主控台
  4. git reset --hard rootcommit:在您自己提交之前将master重置为状态.
  5. 可选,如果您有遥控器,则从以下位置进行拉取:git pull --ff(如果由于拉取不是快速进行而失败,则必须重新考虑rootcommit.它包含您的一些工作)

实际上,如果您在处理代码时从任何远程操作中拉出,这甚至应该可以工作.如果您希望将提交置于远程提交的基础之上,则可以基于远程更改来重新建立新分支:

git checkout my-fancy-new-branch
git rebase master


可以将这些更改应用到遥控器上,虽然可以,但是要进行麻烦,您必须立即对主设备执行强制操作.拥有该存储库克隆的每个人,在提取时都必须格外小心,因为主数据库的历史已更改.他们必须执行与您基本上相同的步骤,然后拉出并重新应用其本地更改(如果有).要强制执行,您必须执行:

git checkout master
git push --force

如前所述,强行推挤确实是邪恶的,您应该避免这种情况.如果可以的话,只需将提交保留在历史记录中,下次再做就更好.

I have my code in the master branch of my project. I want the code in a separate branch and none of it in the master. I can create a new branch off master. But after creating the branch is it possible to remove all code from the master in such a way that if i later rebase or merge my new branch into master it wont cause any problems ?

Thank You

解决方案

This is a bit tricky and you cannot do it properly if others already have copies of your commits. I.e. if you pushed your master branch to a remote repository other people are using or if people directly pulled from your repository, there is no clean way to get rid of this.

You have to rewrite the history of the master branch. There are two cases we have to look at here. From your question, I assume that in master, there is a commit which has been created before any of the code commits you want to remove from master. Let the ID of that commit be rootcommit. Skip step 1 and 2 if you already have a new branch for your code, branched off master.

  1. git checkout master: make sure you are in master.
  2. git checkout -b my-fancy-new-branch: create your new feature branch.
  3. git checkout master: switch back to master
  4. git reset --hard rootcommit: reset master to the state before your own commits.
  5. optional and if you have a remote you pull from: git pull --ff (if this fails because the pull is not fast-forward, you have to reconsider rootcommit. It contains some of your work)

In fact, this should even work if you pulled from any remote while working on your code. If you want to have your commits on top of the commits from the remote, you can rebase your new branch on the remote changes:

git checkout my-fancy-new-branch
git rebase master


To apply these changes to the remote, which is possible, but a huge hassle, you have to do a force-push on master now. Everyone having a clone of that repository, has to take special care now when pulling, because the history of master has changed. They must do essentially the same steps as you did, then pull and then reapply their local changes, if any. To do the force push, you have to execute:

git checkout master
git push --force

As said, force pushs are really evil and you probably should avoid it. If you can, just leave the commits in the history and do it better next time.

这篇关于将所有代码从master转移到新分支,然后从master删除代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:51