简体   繁体   English

重新设置后的Git推送

[英]Git push after rebase

I know this has been asked before but i cant seem to wrap my head around this thing... 我知道这已经被问过了,但是我似乎无法把这个事情包住头...

git checkout master
git pull
git git checkout feature
git rebase origin/master

then resolve all the problems....
Try to push - not gonna happen...

git is really telling me that after doing a rebase ( dealing with n:ths of conflicts ) git真的是在告诉我,在进行了重新设置之后(处理n:ths冲突)

i have two options, use --force which seems risky and stupid. 我有两个选择,使用--force似乎有风险和愚蠢。

or pull again and deal with the merge conflicts again... and end up in same situation? 还是再次pull并再次处理合并冲突...并最终处于相同情况?

error: failed to push some refs to 'ssh://git@git.zzz.com/yyy/xxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I have locally: feature branch, master ( up to date ) 我在本地:feature分支,master(最新)

and remote: featureBranch ( which is ahead now ?! ) and master. 和远程:featureBranch(现在在前面?!)和master。

I just want to update my feature branch so it's even close to version on master... Why is git like this... 我只想更新我的功能分支,所以它甚至接近母版上的版本...为什么git像这样...

I've read many threads about this, and the only solution seem to be to use --force 我已经读过很多关于此的主题,唯一的解决方案似乎是使用--force

This dosent seem like an solution at all, for me, for such commonly used tool... 对于我来说,对于这种常用工具而言,这似乎是一个解决方案……

There is nothing wrong with git push --force on principle. git push --force原则上没有错。

What it does is to replace the remote head of your branch with your local. 它的作用是用本地替换分支的远程头。

There are two cases, one where it is fine to push force, and one where it is not fine at all: 有两种情况,一种情况适用推力,另一种根本不适用:

If you rebased (and therefore created a new chain of commits for your branch), your branch and the remote diverged, which is to be expected. 如果您重新设置了基础(并因此为分支创建了新的提交链),则分支和远程分支将出现差异。 And you made them diverge on purpose. 并且您使它们故意偏离。 For example your branch was not up to date with master , so you rebase it to "move it" on top of master (technically, the commits are recreated from the new base, in this case master but effectively it looks as if it has been moved). 例如,您的分支不是master最新版本,因此您将其重新建立基础以将其“移动”到master (从技术上讲,提交是从新的基础(在本例中为master重新创建的,但实际上看起来好像已经移动)。 So you know that they diverged and that your local version is the correct one. 因此,您知道它们有所不同,并且您的本地版本是正确的版本。 In this case it is fine to tell git: "Take this version, discard the one you have". 在这种情况下,最好告诉git:“获取此版本,放弃您拥有的版本”。

However, if you work with people on a same branch and one person pushes the branch with new changes while you make your own changes, then when you will want to push, Git will also tell you that the your local branch and its upstream diverged, and so you should pull first and so on. 但是,如果您与同一个分支上的人一起工作,而一个人在进行自己的更改时以新的更改推送该分支,那么当您要推送时,Git还会告诉您您本地的分支及其上游分支,所以你应该先拉等等。 In this case it is important to not push --force otherwise your colleague's work will be erased, and he/she will be pretty upset. 在这种情况下不要用 push --force这很重要,否则您的同事的工作将被抹除,他/她将非常沮丧。 So in this case you must indeed pull first (I would recommend pull --rebase to not create a merge commit, and keep your history cleaner, but that's very subjective), then push (and after pulling, no --force will be needed). 因此,在这种情况下,您必须确实先pull (我建议使用pull --rebase来不创建合并提交,并使历史记录更整洁,但这是非常主观的),然后再进行push (并且在拉后不需要--force )。

Bottom line is, know what git push --force does, know when it is ok to overwrite the upstream with your local (you can then push force) and when it is not ok (you need to pull). 最重要的是,知道git push --force作用,知道何时可以用本地覆盖上游(然后可以用力推),什么时候不可以(需要拉)。

And to come back to your original case, you rebased your branch, so it diverged (by definition), so if you work alone on the branch or if you made sure that nobody pushed anything on it in the meantime, git push --force is what you need. 回到原始情况,您重新设置了分支的基础,因此分支(按照定义)有所不同,因此,如果您独自在分支上工作,或者确保同时没有人在分支上施加任何内容,则git push --force是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM