从父分支更新当前分支

use*_*594 56 git

我使用跟踪选项B从分支创建了一个新的git分支A.

现在,当A分支通过少量提交更新时,我也想提交提交B,所以我可以跟踪它,并且有时候不必面对大的改变.

我该怎么做呢?它是在git中自动完成的吗?

Hac*_*Gil 67

这不是自动生成的.您必须手动将更改从A合并到B,这非常简单.只需切换到分支B即可

git merge A
Run Code Online (Sandbox Code Playgroud)

这将自动将您的更改从A合并到B.只要您没有任何冲突,A中的所有更改都将标记为在B中合并.常见的最佳做法是进行每日合并,但这取决于使用您的分支的用户/提交数量.

  • ```git merge origin A ``` 应​​该进一步增强这一点 (3认同)
  • 'git rebase A' 怎么样? (2认同)

Adi*_*ari 9

在子分支 B,我们可以做

git merge origin/A 
Run Code Online (Sandbox Code Playgroud)

这将使其与父母的起源保持同步。


gra*_*der 6

这是我如何让它工作的。

我的方法是我将创建一个新文件夹,然后将以下调用放入(在同一个新文件夹中)..所以我知道我在本地只有“新鲜”代码(但“新鲜”(尽可能)来自远程服务器),而不是任何意外的本地更改。Aka,一种“新文件夹清理”方法。它将检索感兴趣的 2 个分支(新鲜/新检索)到本地......并进行合并(本地)。解决后,可以推送(未显示)。

精简版:

git checkout  feature/mychildbranch

git branch

git checkout  feature/myparentbranch

git pull

git branch

git checkout feature/mychildbranch

git branch

git merge feature/myparentbranch
Run Code Online (Sandbox Code Playgroud)

更长的版本(已解释)我将使用 /* 作为注释 */

/* first, make sure you at least have the child branch */
git checkout feature/mychildbranch


/* ok, just show the branches.  make sure at least feature/mychildbranch exists  note the "*" below says "this is the branch i am on" */
git branch
    * feature/mychildbranch
      feature/myparentbranch


/* now checkout the parent branch...note the "switched" happens automatically with the checkout */    
git checkout feature/myparentbranch
    Switched to branch 'feature/myparentbranch'
    Your branch is up to date with 'origin/feature/myparentbranch'.

/* not pull, the pull will occur on the branch you are currently on, which should be feature/myparentbranch at this point */    
git pull
    remote: Enumerating objects: 69, done.
    remote: Counting objects: 100% (55/55), done.
    remote: Compressing objects: 100% (22/22), done.
    remote: Total 22 (delta 17), reused 0 (delta 0)
    Unpacking objects: 100% (22/22), done.
    From https://mygit.hub.com
       96ae0e9..6eb0a03  feature/myparentbranch -> origin/feature/myparentbranch
        * [new branch]      feature/blah blah blah (specific to my stuff only)
       xb99638..x86db6f  master                  -> origin/master
    Updating x6ae0e9..xeb0a03
    Fast-forward
     .../somefileone.txt | 30 ++++++++++++--------
     .../somefiletwo.txt       |  7 +++--
     .../somefilethree.txt  |  6 ++--
     X files changed, Y insertions(+), Z deletions(-)
     create mode 100644 somenewfileone.txt

/* do a git branch just to show that you're on feature/myparentbranch */    
git branch
  feature/mychildbranch
* feature/myparentbranch

/* ok, now (above) you have the latest-greatest feature/myparent, lets do a checkout on the child to switch to the child */    
git checkout feature/mychildbranch
Switched to branch 'feature/mychildbranch'
Your branch is up to date with 'origin/feature/mychildbranch'.

/* another sanity check, show you're on feature/mychildbranch */
git branch
* feature/mychildbranch
  feature/myparentbranch

/* finally, the magic.  do a merge from feature/myparentbranch (which you know is local and up to date because of the voodoo above */    
git merge feature/myparentbranch
Merge made by the 'recursive' strategy.
 .../somefileone.txt | 30 ++++++++++++--------
 .../somefiletwo.txt       |  7 +++--
 .../somefilethree.txt  |  6 ++--
 X files changed, Y insertions(+), Z deletions(-)
 create mode 100644 somenewfileone.txt
Run Code Online (Sandbox Code Playgroud)

如果没有冲突,你应该在你想去的地方。如果有冲突,恕我直言,这是一个全新的 SOF 问题/答案。


eck*_*kes 5

假设您对 create 的调用Bgit clone /path/to/server/A,您只需要执行 agit pull就完成了。这就是git pull工作原理:首先它从上游(在您的情况下是被跟踪的分支)获取更改A,然后这些更改合并到跟踪被跟踪分支(B在您的情况下)的分支中。

Git的书临Git的讨论话题的深入开展,所以他们很值得一读(如果你在忙乱之中没有,看他们休息过)。


And*_*ndy 5

另一种选择是执行 agit fetch Agit merge A

描述这样做的原因的博客文章