与主人保持最新的分支机构

lar*_*ryq 24 git git-merge git-branch

我有一个远程存储库,我已经提取并从中分支出来.我想让新分支保持最新状态,对master进行更改.我正在考虑下面的工作流程,它是否有意义,或者有更好的方法来做到这一点?

  1. 初始分支和结帐:

    git checkout master
    
    git pull
    
    git checkout -b my_branch
    
    Run Code Online (Sandbox Code Playgroud)
  2. 做一些工作my_branch,然后定期:

    git checkout master
    
    git pull
    
    git checkout my_branch
    
    git merge master --no-ff
    
    Run Code Online (Sandbox Code Playgroud)

根据需要重复步骤2,定期推送到遥控器my_branch.

然后准备好合并时:

git checkout master

git merge my_branch --no-ff
Run Code Online (Sandbox Code Playgroud)

听起来不错?

mic*_*has 17

您可以简化命令:

1.

git fetch
git checkout -b my_branch origin/master
Run Code Online (Sandbox Code Playgroud)

2.

git fetch
git merge origin/master
Run Code Online (Sandbox Code Playgroud)

git fetch 更新您的远程分支,当您不打算在此分支上工作时,通常不需要拥有分支的本地副本.

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

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).
Run Code Online (Sandbox Code Playgroud)

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

通常你只想要git pull --rebase哪个本质git fetch上是加号git rebase,并创造一个更清晰的历史.

您的"定期推送"有什么理由吗?如果没有其他人在同一个分支上工作,那就完全没问题了,只需在完成所有事情后再推动.

  • `git pull`和`git pull --rebase`都适用于配置的上游(参见`git branch -vv`).通常,本地分支"my_branch"将跟踪远程分支"origin/my_branch".但是在你第一次推动之前跟踪"原点/主人"是完全没问题的.`git pull --rebase`只是获取上游分支的当前版本,将您的分支重置为该版本并重放您所做的所有提交.因此,仅基于上游分支的当前版本导致相同的更改. (2认同)

Chr*_*oph 11

我建议使用rebase工作流程.所以不要使用git pull你应该使用git pull --rebase.

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

但说实话,我在一个小团队工作,如果我们需要共同完成一个功能分支,我们需要让它与主人保持同步,那么我们暂时暂停我们的工作(并清楚地传达这个过程) ,对master和force强制推送功能分支.但是,这对于更大的球队来说并不具备规模.但是,我发现使用在master上重新定义的功能分支更方便,而不必处理来自master的合并.

一定要读这个.

Git工作流程和rebase与合并问题