如何将新分支作为主要分支添加到现有的GitHub仓库中?

3
一个月前,我创建了一个GitHub仓库用于我的项目,并将之前的所有实现推送到了我的github repo上。然而,现在我有了一种新的实现,它比以前的版本更加稳定,更新后的解决方案更加高效。我的目标是,我想保留我的项目仓库中的旧实现,同时,在我的github repo中引入一个名为“major”的新分支,同时将旧实现标记为“default”。如何以安全的方式实现这一点?我不想失去以前的工作,并且我希望确保新引入的分支可以作为“major”分支活跃,同时停用保存旧工作的“default”分支。我该怎么做?谁能指导我详细的工作流程?谢谢!
编辑:为了澄清一点,我没有将我的新实现推送到github repo上。我想重新打开保存我的新实现的新分支,并将此新分支转发到在线软件包构建服务器。还需要进一步的帮助吗?
1个回答

3
我不想丢失以前的工作,并确保新引入的分支可以作为主分支活跃。您可以在项目实现中遵循语义化版本控制。只需在以前的实现的最新提交上打一个标签(例如,v1.0.0),然后将更新/最新更改与默认分支master合并。现在,如果您想要回退/获取以前的版本,则可以检出标签v1.0.0
  1. Add, Commit your latest changes to a branch (say, feature). So, your feature branch holds all the latest changes.

    $ git checkout -b feature
    $ git add .
    $ git commit -m 'new implementation'
    $ git push origin HEAD                 # push to remote 'feature' branch
    
  2. Now checkout to master branch and give a tag on the top of your previous implementation's commit.

    $ git checkout master
    $ git tag -a v1.0.0 -m "my previous implementation"
    $ git push --tags                   # push v1.0.0 tag to remote
    
    $ git checkout -b prev-impl-v1.0.0  # create a new branch ('prev-impl-v1.0.0') from the tag 'v1.0.0' which holds your previous implementation
    $ git push origin HEAD              # push 'prev-impl-v1.0.0' branch 
    
  3. Now merge your updated changes (exists in feature branch) into default branch master. Merge feature branch into master branch. If you finished with your updated implementation then give a new tag on the top of new implementation's commit.

    $ git checkout master             # checkout master branch
    $ git pull origin feature         # merge feature with master
    
    $ git tag -a v2.0.0               # give a new tag ('v2.0.0') on the new implementation
    
    $ git push origin HEAD            # update the remote 'master'
    $ git push --tags                 # push 'v2.0.0' tag to remote
    
    $ git checkout -b new-impl-v2.0.0 # create a new branch from tag 'v2.0.0' tag
    $ git push origin HEAD            # push 'new-impl-v2.0.0' branch 
    
现在你的master已经更新了你最新的更改。同时你还有不同的分支,比如:
master = new-impl-v2.0.0 = 拥有新的实现(v2.0.0标签)
prev-impl-v1.0.0 = 拥有之前的实现(v1.0.0标签)
现在,你可以从你的master上创建一个新的分支(例如,feature2),并在该分支上进行下一次实现的工作(如果需要的话)。
$ git checkout master
$ git checkout -b `feature2`

# do changes for your next implementation

当你完成下一个实现后,如同 feature 分支一样,重复步骤 #3
如果你想回退到先前的实现,只需 checkout 到你想要的特定标签。你还可以切换至两个分支(prev-impl-v1.0.0new-impl-v2.0.0),以查看之前的任何实现。
 $ git checkout v1.0.0           # checkout to v1.0.0 = previous implementation
 $ git checkout v2.0.0           # checkout to v2.0.0 = new implementation 

 # create a new branch from any checked out tag/commit
 $ git checkout -b <branch-name> # create & checkout a new branch from the tag

更多关于语义化版本控制的信息,请访问http://semver.org/

你能否用Git命令详细说明一下你的答案?我的目标是重新打开一个新分支,把所有新的实现放在那里。你在这里的回答太抽象了。 - user6070577
1
请澄清一下,您的默认分支master是保留了以前的实现还是更新了实现?您的目标是将以前的实现备份到任何其他分支,并使用最新更改更新默认分支master吗? - Sajib Khan
我的默认分支master保存了之前的实现。我还没有将新的实现推送到github仓库。我想确保我遵循正确的方法。如你所知,git有时会对这种事情抱怨很多。希望你能理解 :) - user6070577
1
@datageek 我在这里使用git命令逐步说明了人们通常如何管理项目版本控制。如果您有任何问题,请告诉我。 - Sajib Khan
我非常感谢您的帮助,它是非常有动力的。我将在我的网站上尝试您的解决方案。如果有任何问题,我会向您报告 :) - user6070577

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接