简体   繁体   English

在分支的git rebase之后推送新的提交

[英]Push new commits after git rebase of branch

I have one master branch and a new branch. 我有一个master分支和一个new分支。 When I started working on the new branch I forgot to pull the latest commits, and was 20 commits behind master . 当我开始在new分支上工作时,我忘记进行最新的提交,而比master落后20个提交。 I also committed a couple of changes in the new branch. 我还对new分支进行了一些更改。

However, I was able to update my new branch with the other commits from the master branch by doing: 但是,通过执行以下操作,我可以使用master分支中的其他提交来更新new分支:

git checkout master
git pull --rebase
git rebase master newBranch

Then I solved the conflicts and my new branch was updated with the previous commits. 然后,我解决了冲突,并使用先前的提交更新了我的new分支。

The problem is that these earlier commits are only reflected in my local version of the new branch. 问题在于这些较早的提交仅反映在new分支的本地版本中。 I see this when typing git log . 我在输入git log时看到了这一点。 When I check out the remote version, it still has the old version because I haven't commited/pushed these old commits. 当我签出远程版本时,它仍然具有旧版本,因为我尚未提交/推送这些旧提交。 When I check git status , there is nothing to commit. 当我检查git status ,没有任何要提交的内容。

How can I get the new branch to get these old commits/changes remotely and not just locally? 如何获得新分支以远程(而不是仅本地)获得这些旧提交/更改? In other words, how can the remote version have same log/history as my local version of the new branch? 换句话说,远程版本如何与新分支的本地版本具有相同的日志/历史记录?

Assuming you have a master and new branch(Where you have all your commits) is created out of master branch.Now if the master branch doesn't have all the commits which are there in the central repository then we need to bring the changes from server to master branch and from master to new. 假设您有一个master分支,并且在master分支之外创建了一个新分支(您拥有所有提交)。现在,如果master分支没有中央存储库中的所有提交,那么我们需要从中进行更改服务器到master分支以及从master到new。 Then we need to bring the local commits on new to master and then do a push from there. 然后,我们需要将本地提交提交到master上,然后从那里进行推送。 The below steps does what is said above: 以下步骤执行上述操作:

git checkout master
git pull
git checkout new
git rebase master
git checkout master
git merge new
git push

What you're trying to accomplish can be a bit "dangerous". 您要完成的工作可能有点“危险”。 You have remote branches: 您有远程分支机构:

  • origin/master 起源/主人
  • origin/new 来源/新

and local branches 和当地分支机构

  • master
  • new

When rebasing the local new branch onto the newest origin/master commit, you will be rewriting history. 将本地新分支重新部署到最新的源/主提交时,您将在重写历史记录。 For local work, this is not a problem. 对于本地工作,这不是问题。 If your commits have previously been pushed to a remote, and other developers might have work derived from it, it can cause a lot of head scratching because you now have at least two versions of the same changes. 如果您的提交以前已被推送到远程,并且其他开发人员可能已经从该远程派生了工作,则这可能会引起很多麻烦,因为您现在至少具有相同更改的两个版本。

If you are 100% sure that nobody has any derived work, you can push your local new branch to the server 如果您100%确保没有人从事任何派生工作,则可以将本地新分支推送到服务器

git push origin new

this will, however, fail because the commit you're trying to push is not a fast-forward merge from the previous origin/new commit. 但是,这将失败,因为您要推送的提交不是从先前的原始提交/新提交进行的快速合并。 You will need to force the push: 您将需要强制执行以下操作:

git push origin new -f

The server might prevent you from performing this push, though. 但是,服务器可能会阻止您执行此推送。

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

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