git 保持分支与 master 保持同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19758915/
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-10 17:14:44  来源:igfitidea点击:

Keeping a branch up to date with master

gitgit-branchgit-merge

提问by larryq

I have a remote repository that I've pulled and am branching from. I want to keep the new branch up to date with changes done to master. I'm thinking about the workflow below, does it make sense or are there are better ways to do this?

我有一个远程存储库,我已经拉取并从中分支。我想通过对 master 所做的更改使新分支保持最新。我正在考虑下面的工作流程,它有意义还是有更好的方法来做到这一点?

  1. Initial branching and checkout:

    git checkout master
    
    git pull
    
    git checkout -b my_branch
    
  2. Do some work in my_branch, then periodically:

    git checkout master
    
    git pull
    
    git checkout my_branch
    
    git merge master --no-ff
    
  1. 初始分支和结帐:

    git checkout master
    
    git pull
    
    git checkout -b my_branch
    
  2. 做一些工作my_branch,然后定期:

    git checkout master
    
    git pull
    
    git checkout my_branch
    
    git merge master --no-ff
    

Repeat step 2 as needed, with periodic pushes to the remote my_branch.

根据需要重复步骤 2,定期推送到远程my_branch.

Then when ready for a merge back:

然后当准备好合并回来时:

git checkout master

git merge my_branch --no-ff

Sound ok?

听起来好吗?

采纳答案by michas

You can simplify your commands:

您可以简化您的命令:

1.

1.

git fetch
git checkout -b my_branch origin/master

2.

2.

git fetch
git merge origin/master

git fetchupdates your remote branches, there usually is no need to have a local copy of a branch when your are not planning to work on this branch.

git fetch更新你的远程分支,当你不打算在这个分支上工作时,通常不需要有一个分支的本地副本。

You can omit the --no-ffafter setting git config --global merge.ff false.

您可以省略--no-ffafter 设置git config --global merge.ff false

git help configsays:

git help config说:

   merge.ff
       By default, Git does not create an extra merge commit when merging
       a commit that is a descendant of the current commit. Instead, the
       tip of the current branch is fast-forwarded. When set to false,
       this variable tells Git to create an extra merge commit in such a
       case (equivalent to giving the --no-ff option from the command
       line). When set to only, only such fast-forward merges are allowed
       (equivalent to giving the --ff-only option from the command line).

Be aware that git pullis just a combination of git fetchand git merge.

要知道,git pull仅仅是一个组合git fetchgit merge

Usually you just want git pull --rebasewhich is essentially git fetchplus git rebase, and creates a much cleaner history.

通常你只想要git pull --rebasewhich 本质git fetch上是plus git rebase,并创建一个更清晰的历史。

Is there any reason for your "periodic pushes"? If no one else is working on the same branch it would be perfectly fine, just to push after finishing everything.

你的“定期推送”有什么原因吗?如果没有其他人在同一个分支上工作,那就完全没问题了,只需在完成所有操作后推送即可。

回答by Christoph

I would advise to use a rebase workflow. So instead of using git pullyou should use git pull --rebase.

我建议使用 rebase 工作流程。因此,git pull您应该使用git pull --rebase.

I would do the same with the feature branch. So instead of doing a git merge master --no-ffI would use a git rebase master. However, if the feature branch is meant to be shared with co-workers during development, then you are better off to merge the master branch periodically into the feature branch.

我会对功能分支做同样的事情。因此,git merge master --no-ff我会使用git rebase master. 但是,如果要在开发期间与同事共享功能分支,那么最好定期将主分支合并到功能分支中。

But to be honest, I work on a small team and if we need to work on a feature branch together and we need to get it up to date with master then we just suspend our work for a short moment (and communicate the process clearly), rebase on master and force push the feature branch. But yep, that doesn't scale for bigger teams. However, I find it much more convenient to work with a feature branch that is rebased on master instead of having to deal with merges from master.

但老实说,我在一个小团队中工作,如果我们需要一起在一个功能分支上工作并且我们需要让它与 master 保持同步,那么我们只需暂停我们的工作一小会(并清楚地传达过程) ,基于 master 并强制推送功能分支。但是,是的,这不适用于更大的团队。但是,我发现使用基于 master 的功能分支更方便,而不必处理来自 master 的合并。

Make sure to read this.

请务必阅读此内容。

Git workflow and rebase vs merge questions

Git 工作流程和变基与合并问题