简体   繁体   English

具有基准的Git分支

[英]Git branch with rebase

This is what I usually do at my work: 这是我通常在工作中要做的:

git checkout -b my_branch
git commit -m 'fixed something #8852'
git rebase -i master
git push origin my_branch

Then after some time I needed to work again on the same branch. 然后一段时间后,我需要在同一分支上再次工作。 Since then new commits appeared on master branch. 从那时起,新的提交出现在master分支上。 I checked out my branch, made changes, commited and did 'git rebase -i master'. 我签出了我的分支,进行了更改,提交并做了'git rebase -i master'。 But when I tried to do 'git push origin my_branch' it gave me an error: 但是,当我尝试执行“ git push origin my_branch”时,出现了一个错误:

! [rejected]        my_branch -> my_branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/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.

My master was up to date. 我的主人是最新的。 So from my_branch I did 所以从my_branch我做了

git pull --rebase origin my_branch
git push origin my_branch

I think it rebased all new commits to my branch. 我认为它将所有新提交重新分配到我的分支。 Now on my_branch I have commits that don't belong to it. 现在在my_branch上,我有一些不属于它的提交。 Note: my team leader asked me to do always rebase to avoid merging. 注意:我的团队负责人要求我始终进行基础调整以避免合并。 How I should've solved this error so my_branch would contain only my commits? 我应该如何解决此错误,以便my_branch仅包含我的提交?

When you are on master, and you do git checkout -b my_branch you are up to date at that moment. 当您在master上并且执行git checkout -b my_branch您是最新的。

Later, after some work on your branch, you will want to update those things from master, so you do a git pull master This is OK. 稍后,在分支上进行一些工作之后,您将需要从master更新这些内容,因此您可以执行git pull master这是可以的。

Once you are good and everything is working, then you would normally have the code review process happen, and approval on that. 一旦一切顺利,一切正常,那么通常您将进行代码审查过程,并获得批准。

The manager can be on master and do a git rebase my_branch if that is the policy. 如果这是策略,管理器可以处于主状态,并执行git rebase my_branch

So, the problem presents itself in rebasing and reordering the history in both directions. 因此,问题出现在两个方向的历史重新编排和重新排序上。

What the error is saying is that your remote branch is more advanced (in the tree) than your local branch. 错误的意思是,远程分支(在树中)比本地分支更高级。 This tends to happen when you are collaborating and rebasing. 当您进行协作和调整基础时,往往会发生这种情况。 It is recommended that if your are following a methodology that you need to collaborate with other people branch you should merge. 建议如果您遵循的是需要与其他人协作的方法,则应该合并。 If you do a quick google search you will find lots of good git tutorials that can help you get better with this! 如果您进行了快速的Google搜索,您会发现很多不错的git教程,可以帮助您更好地做到这一点!

Now, for fixing your problem there are few solutions. 现在,解决问题的方法很少。 What I think is the best one will be: 我认为最好的是:

  • git log and write down the hashes for the commits you want to have (you can copy and paste them somewhere) git log并写下您想要的提交的哈希值(您可以将它们复制并粘贴到某处)
  • Go to a safe starting point and create a new branch ( git checkout -b branch_name ) 转到一个安全的起点并创建一个新分支( git checkout -b branch_name
  • Cherry-pick the commits you want to have to your new branch ( git cherry-pick commit-hash ), if they are a lot of commits you could do a interactive rebase with your old branch and just choose the commits you want. Cherry-pick您想要到新分支的git cherry-pick commit-hashgit cherry-pick commit-hash ),如果它们包含很多提交,则可以与旧分支进行交互式重新设置,只需选择所需的提交即可。
  • Be happy with your new branch that have all the info you want to have and nothing else. 对拥有所有您想要拥有的所有信息的新分支感到满意。

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

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