好的,我认为这是一个简单的git场景,我错过了什么?

我有一个主分支和一个特征分支。我在master上做了一些工作,在feature上做了一些工作,然后在master上做了一些工作。我最终得到了这样的东西(字典顺序暗示了提交的顺序):

A--B--C------F--G  (master)
       \    
        D--E  (feature)

我对git push origin master来保持远程master的更新没有问题,也没有git push origin feature(当在特性上时)来维护我的特性工作的远程备份。到目前为止,我们都很好。

但现在我想在master上的F- G提交的基础上rebase feature,所以我git checkout feature和git rebase master。还好。现在我们有:

A--B--C------F--G  (master)
                 \
                  D'--E'  (feature)

问题:当我想备份新的基于git的推源特性分支的重基特性时,推被拒绝了,因为树已经因为重基而改变了。这只能用git的push -force origin特性来解决。

我讨厌在不确定自己是否需要的情况下使用武力。那么,我需要它吗?调整基地是否意味着下一步的行动应该是强有力的?

这个功能分支并没有与其他开发者共享,所以事实上我对强制推送没有任何问题,我不会丢失任何数据,问题更多的是概念性的。


当前回答

我将使用“checkout -b”来代替,这样更容易理解。

git checkout myFeature
git rebase master
git push origin --delete myFeature
git push origin myFeature

当您删除时,您阻止在包含不同SHA ID的现有分支中推入。 在本例中,我只删除远程分支。

其他回答

而不是使用-f或——force开发人员应该使用

--force-with-lease

Why? Because it checks the remote branch for changes which is absolutely a good idea. Let's imagine that James and Lisa are working on the same feature branch and Lisa has pushed a commit. James now rebases his local branch and is rejected when trying to push. Of course James thinks this is due to rebase and uses --force and would rewrite all Lisa's changes. If James had used --force-with-lease he would have received a warning that there are commits done by someone else. I don't see why anyone would use --force instead of --force-with-lease when pushing after a rebase.

获取master的新变化,并在最新master的基础上重新建立功能分支

git checkout master
git pull
git checkout feature
git pull --rebase origin master
git push origin feature

我将使用“checkout -b”来代替,这样更容易理解。

git checkout myFeature
git rebase master
git push origin --delete myFeature
git push origin myFeature

当您删除时,您阻止在包含不同SHA ID的现有分支中推入。 在本例中,我只删除远程分支。

因为OP确实理解问题,只是寻找一个更好的解决方案…

把这个作为一个实践怎么样?

Have on actual feature-develop branch (where you never rebase and force-push, so your fellow feature developers don't hate you). Here, regularly grab those changes from main with a merge. Messier history, yes, but life is easy and no one get's interupted in his work. Have a second feature-develop branch, where one feature team member regulary pushes all feature commits to, indeed rebased, indeed forced. So almost cleanly based on a fairly recent master commit. Upon feature complete, push that branch on top of master.

这个方法可能已经有了一个模式名。

以下是我的工作:

Git push -f origin branch_name

而且它不会删除我的任何代码。

但是,如果你想避免这种情况,你可以做以下事情:

git checkout master
git pull --rebase
git checkout -b new_branch_name

然后,您可以挑选所有提交到新分支的文件。 git选择COMMIT ID 然后推你的新树枝。