从父分支更新当前分支


71

BA具有跟踪选项的分支创建了一个新的git分支。

现在,当A分支通过很少的提交进行更新时,我也希望将提交也移至该提交B,因此我可以对其进行跟踪,并且以后不必再面对较大的更改。

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


除了说分支A来自分支A之外,跟踪分支是否真的添加了其他东西?
菲利普P.

5
你应该变基与A的支路B
比尔博·巴金斯

Answers:


88

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

git merge A

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


如果您说的是A是远程分支而B是本地分支,那么git pull就足够了
HackerGil 2011年

1
当我从分支B进行git merge A时,至少有1个文件冲突,更改中出现了一些文件(暂存)!然后,在解决冲突之后,我被迫提交已经提交的更改。怎么了?
罗曼·文森特

git merge origin A 应该进一步增强这一功能
Aditya Rewari

5

假设您的呼叫建立Bgit clone /path/to/server/A,你就必须做git pull,你就大功告成了。这是这样git pull工作的:首先,它从上游(在您的情况下为跟踪的分支)获取更改A,然后这些更改合并到跟踪该分支(B在您的情况下)的分支中。

Git书》和《Pro Git》对此主题进行了深入讨论,因此它们非常值得一读(如果您不着急,还可以阅读其余内容)。


嘿,只是想知道。我不确定是否真的通过从A克隆来创建分支B。我只是创建了一个git branch A带有一些随机选项的分支。因此,如果我只是进行git pull,则git会抱怨You asked me to pull without telling me which branch you want to merge with...
user482594

@ user482594:pull仅当您要从远程获取数据时才需要。如果您A刚上过并且刚刚做过git checkout -b B,则可以简单地merge(在上时B,键入git merge A)。
eckes 2011年


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问题/答案恕我直言。


git merge origin A这是否应该使我免于回到父分支,在那里拉,然后又回到孩子去执行其他事情
Aditya Rewari

1

与父母合并:

运行两个命令非常重要:

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

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

要在github之类的遥控器上查看所有更改,只需执行

  1. git推

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.