我在Git中有一个存储库。我做了一个分支,然后对主分支和分支都做了一些更改。
然后,几十次提交之后,我意识到分支的状态比主分支好得多,所以我希望分支“成为”主分支,并忽略对主分支的更改。
我无法合并它,因为我不想将更改保留在master上。我该怎么办?
额外:在本例中,“旧”主控已被推送到另一个存储库,如GitHub。这是如何改变事情的?
我在Git中有一个存储库。我做了一个分支,然后对主分支和分支都做了一些更改。
然后,几十次提交之后,我意识到分支的状态比主分支好得多,所以我希望分支“成为”主分支,并忽略对主分支的更改。
我无法合并它,因为我不想将更改保留在master上。我该怎么办?
额外:在本例中,“旧”主控已被推送到另一个存储库,如GitHub。这是如何改变事情的?
当前回答
为了补充Cascabel的答案,如果你不想在源分支的历史中放置一个无意义的合并,你可以为我们的合并创建一个临时分支,然后扔掉它:
git checkout <source>
git checkout -b temp # temporary branch for merge
git merge -s ours <target> # create merge commit with contents of <source>
git checkout <target> # fast forward <target> to merge commit
git merge temp # ...
git branch -d temp # throw temporary branch away
这样,合并提交将只存在于目标分支的历史记录中。
或者,如果您根本不想创建合并,您可以简单地获取源的内容,并将其用于目标上的新提交:
git checkout <source> # fill index with contents of <source>
git symbolic-ref HEAD <target> # tell git we're committing on <target>
git commit -m "Setting contents to <source>" # make an ordinary commit with the contents of <source>
其他回答
对我来说,我希望我的开发分支在领先之后回到主分支。
在开发过程中:
git checkout master
git pull
git checkout develop
git pull
git reset --hard origin/master
git push -f
我很惊讶这里没有答案。这就是我所做的。
在我的文件系统中的不同目录中制作了两个repo副本,一个是主副本,另一个是分支副本。将分支中的所有文件(手动)复制到主文件已将更改提交给master。
这样,就有了一个包含所有差异的提交,保存了提交历史,不需要强制推送。
还可以将其他分支中的所有文件签出到master中:
git checkout master
git checkout better_branch -- .
然后提交所有更改。
通过以下方式将分支重命名为主分支:
git branch -M branch_name master
我的做事方式如下
#Backup branch
git checkout -b master_backup
git push origin master_backup
git checkout master
#Hard Reset master branch to the last common commit
git reset --hard e8c8597
#Merge
git merge develop