git 如何将更改从主干转移到分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3646057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:33:29  来源:igfitidea点击:

How do I get changes from my trunk into a branch?

gitdvcstortoisegit

提问by Rupert Madden-Abbott

I've just started using Git and find that whilst I am implementing a feature in a branch, I will encounter some bug that really needs to be pushed to the trunk as soon as possible. In order to do so, I use checkout to switch back to the trunk, make my changes and commit them. I now have a trunk that is bug free.

刚开始使用Git,发现在一个分支中实现一个特性的时候,会遇到一些bug,确实需要尽快推送到trunk。为此,我使用 checkout 切换回主干,进行更改并提交。我现在有一个没有错误的后备箱。

Unfortunately, my branch needs to have this bug fixed as well. As the feature is not complete, I can't just merge the branch back into the trunk. How can I alter my branch so that it receives the changes I made to the trunk?

不幸的是,我的分支也需要修复这个错误。由于功能不完整,我不能只是将分支合并回主干。如何更改我的分支,以便它接收我对主干所做的更改?

If it matters, I am developing on my own and so only have a single repository to worry about.

如果重要的话,我正在自己开发,所以只有一个存储库需要担心。

I am using TortoiseGit so instructions specific to that would be helpful but not necessary.

我正在使用 TortoiseGit,因此特定于它的说明会有所帮助但不是必需的。

回答by alternative

Make sure you have your branch checked out (git checkout branch-name) and run

确保您已检出分支 ( git checkout branch-name) 并运行

git rebase master

git rebase master

And resolve any conflicts that arrive.

并解决出现的任何冲突。

If you aren't sure what these commands do, try not using TortoiseGit and use the terminal. It will help you really understand the commands.

如果您不确定这些命令的作用,请尝试不使用 TortoiseGit 并使用终端。它将帮助您真正理解命令。

WARNING: This assumes a local branch. If you have shared the branch, do not run the rebase (because it modifies history). Run

警告:这假设有一个本地分支。如果您共享了分支,请不要运行变基(因为它会修改历史记录)。跑

git merge master

git merge master

while you are on your other branch. This has less clean history, but can be used.

当你在你的另一个分支时。这具有较少的干净历史记录,但可以使用。

The difference is:

区别在于:

  • Rebase - rewrites the branch ontop of master, replaying all the changes
  • Merge - a normal merge, creating a commit with two parents
  • Rebase - 在 master 上重写分支,重放所有更改
  • 合并 - 一个正常的合并,创建一个有两个父项的提交

回答by wadesworld

If your repository is not available to anyone else, then git rebase masteras suggested above will work.

如果其他人无法使用您的存储库,则git rebase master按照上面的建议可以使用。

If your repository is available publicly, you really don't want to rebase since it may screw up others who have cloned your repository. In that case, you want to use git merge masterto merge the changes from trunk into your feature branch.

如果您的存储库是公开可用的,您真的不想变基,因为它可能会搞砸克隆了您的存储库的其他人。在这种情况下,您希望使用git merge master将主干中的更改合并到您的功能分支中。