从父分支更新当前分支

时间:2011-07-26 20:51:45

标签: git

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

现在,当A分支通过少量提交更新时,我想将提交提交到B,因此我可以跟踪它,并且有时不必面对重大变化后面。

我该如何处理?它是在git中自动完成的吗?

6 个答案:

答案 0 :(得分:61)

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

git merge A

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

答案 1 :(得分:3)

另一种选择是git fetch Agit merge A

Blog post describing reason for doing it this way

答案 2 :(得分:3)

这就是我的工作方式。

简短版本:

git checkout  feature/mychildbranch

git branch

git checkout  feature/myparentbranch

git pull

git branch

git checkout feature/mychildbranch

git branch

git merge feature/myparentbranch

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

/* 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

如果没有冲突,那么您应该在想要的地方。如果存在冲突,那就是全新的SOF问题/答案恕我直言。

答案 3 :(得分:2)

假设您创建B的电话号码为git clone /path/to/server/A,您只需执行git pull即可。这就是git pull的工作原理:首先它fetch来自上游(您的案例中跟踪的分支A)的变化,然后它merge这些变化进入分支跟踪被跟踪的分支(在您的情况下为B)。

Git BookPro Git深入讨论这个话题,所以非常值得一读(如果你不赶时间,也要阅读其余内容)。

答案 4 :(得分:1)

与父母合并:

同时运行两个命令非常重要:

  1. git fetch [以提取与分支相关的所有元数据]
  2. git merge parentBranchName

仅供参考:现在在您的本地历史记录/日志中,您将看到提交列表,但这将提交您与本地而不是远程的父分支相关联的更改。

要在github之类的远程设备上查看所有更改,只需执行

  1. git push

答案 5 :(得分:0)

在子分支B,我们可以做

git merge origin A 

这将使其与父母的出身保持同步。