简体   繁体   English

如何从另一个分支获取更改

[英]How to get changes from another branch

I am currently working on featurex branch.我目前正在研究featurex分支。 Our master branch is named our-team .我们的主分支名为our-team Since I started working on featurex , more changes have been made to branch our-team .自从我开始研究featurex ,已经对分支our-team进行了更多更改。

I have done this locally to get all the latest changes from our-team :我已在本地完成此操作,以从our-team获取所有最新更改:

git checkout our-team
git pull

Before I push featurex for merging, I would locally like to get all changes from our-team branch into featurex so that I can ensure everything works as expected.在我推送featurex进行合并之前,我希望在本地将所有更改从our-team分支转移到featurex中,以便我可以确保一切按预期工作。

How can I do that?我怎样才能做到这一点?

Before following this instructions keep in mind that featurex is the branch where changes are being merged and pushed在遵循此说明之前,请记住, featurex是正在合并和推送更改的分支

  1. go to the master branch our-team转到主分支our-team
  • git checkout our-team git checkout 我们的团队
  1. pull all the new changes from our-team branchour-team分支中提取所有新更改
  • git pull
  1. go to your branch featurex去你的分支featurex
  • git checkout featurex git 结帐featurex
  1. merge the changes of our-team branch into featurex branchour-team分支的更改合并到featurex分支中
  • git merge our-team git 合并our-team
  • or git cherry-pick {commit-hash} if you want to merge specific commits或者git cherry-pick {commit-hash}如果你想合并特定的提交
  1. push your changes with the changes of our-team branch使用our-team分支的更改推送您的更改
  • git push

Note : probably you will have to fix conflicts after merging our-team branch into featurex branch before pushing注意:在推送之前,您可能需要在将our-team分支合并到featurex分支后修复冲突

You can use rebase , for instance, git rebase our-team when you are on your branch featurex你可以使用rebase ,例如,当你在你的分支featurex上时, git rebase our-team

It will move the start point of the branch at the end of your our-team branch, merging all changes in your featurex branch.它将在our-team分支的末尾移动分支的起点,合并您的featurex分支中的所有更改。

git fetch origin our-team

or

git pull origin our-team

but first you should make sure that you already on the branch you want to update to (featurex).但首先你应该确保你已经在你想要更新的分支上(featurex)。

For other people coming upon this post on google.对于在谷歌上看到这篇文章的其他人。 There are 2 options, either merging or rebasing your branch.有 2 个选项,合并重新设置分支。 Both works differently, but have similar outcomes.两者的工作方式不同,但结果相似。

The accepted answer is a rebase .接受的答案rebase This will take all the commits done to our-team and then apply the commits done to featurex , prompting you to merge them as needed.这会将所有提交给our-team ,然后将完成的提交应用到featurex ,提示您根据需要合并它们。

One bit caveat of rebasing is that you lose/rewrite your branch history, essentially telling git that your branch did not began at commit 123abc but at commit 456cde.重新定位的一点警告是你丢失/重写了你的分支历史,本质上告诉 git 你的分支不是在提交 123abc 开始,而是在提交 456cde 开始。 This will cause problems for other people working on the branch, and some remote tools will complain about it.会给在分支上工作的其他人带来问题,并且一些远程工具会抱怨它。 If you are sure about what you are doing though, that's what the --force flag is for.如果您确定自己在做什么,这就是--force标志的用途。

What other posters are suggesting is a merge .其他海报建议的是合并 This will take the featurex branch, with whatever state it has and try to merge it with the current state of our-team , prompting you to do one, big, merge commit and fix all the merge errors before pushing to our-team .这将采用具有任何状态的featurex分支,并尝试将其与our-team的当前状态合并,提示您在推送到our-team之前做一个,大的,合并提交并修复所有合并错误。 The difference is that you are applying your featurex commits before the our-team new commits and then fixing the differences.不同之处在于您在our-team新提交之前应用您的featurex提交,然后修复差异。 You also do not rewrite history, instead adding one commit to it instead of rewriting those that came before.你也不会重写历史,而是向它添加一个提交,而不是重写之前的那些。

Both options are valid and can work in tandem.这两个选项都有效并且可以协同工作。 What is usually (by that I mean, if you are using widespread tools and methodology such as git-flow ) done for a feature branch is to merge it into the main branch, often going through a merge-request, and solve all the conflicts that arise into one (or multiple) merge commits.通常(我的意思是,如果您使用广泛的工具和方法,例如git-flow )为功能分支所做的是将其合并到主分支中,通常通过合并请求,并解决所有冲突出现在一个(或多个)合并提交中。

Rebasing is an interesting option, that may help you fix your branch before eventually going through a merge, and ease the pain of having to do one big merge commit. Rebase 是一个有趣的选项,它可以帮助您在最终进行合并之前修复您的分支,并减轻必须进行一次大合并提交的痛苦。

You are almost there :)你快到了:)

All that is left is to剩下的就是

git checkout featurex
git merge our-team

This will merge our-team into featurex.这会将我们的团队合并到 featurex 中。

The above assumes you have already committed/stashed your changes in featurex, if that is not the case you will need to do this first.以上假设您已经在 featurex 中提交/隐藏了您的更改,如果不是这种情况,您需要先执行此操作。

incase someone stumbles upon this asking themselves how to pull in changes without using git pull - git pull will first run git fetch then git merge as it makes up the combination of two sequences ie fetch + merge. incase someone stumbles upon this asking themselves how to pull in changes without using git pull - git pull will first run git fetch then git merge as it makes up the combination of two sequences ie fetch + merge. For those that need to just pull changes from another branch without doing so:对于那些只需要从另一个分支中提取更改而不这样做的人:

git checkout <branch with changes wanted> 
git checkout -b <new branch> 

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

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