git 从父分支更新当前分支

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

Updating the current branch from parent branch

git

提问by user482594

I created a new git branch Bfrom branch Awith tracking option.

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

Now, when Abranch gets updated by few commits, I want to pull the commits to Bas well, so I can keep track of it, and do not have to face big change sometimes later.

现在,当A分支被少数提交更新时,我也想将提交拉到B,这样我就可以跟踪它,并且以后有时不必面对大的变化。

How should I approach this? Is it automatically done in git?

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

回答by HackerGil

This is not made automatically. You have to manually merge your changes from A to B, which is pretty simple. Just switch to branch B and do

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

git merge A

Which will automatically merge your changes from A to B. As long as you don't have any conflicts, all of the changes in A will be marked as merged in B. A common best practices is to make daily merges, but that is dependent on the number of users/commits using your branch.

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

回答by eckes

Assuming your call to create Bwas git clone /path/to/server/A, you just have to do a git pulland you're done. That's how git pullworks: first it fetches the changes from the upstream (the tracked branch Ain your case), then it merges these changes into the branch that tracks the tracked branch (Bin your case).

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

The Git Bookand Pro Gitdiscuss that topic in depth, so they're quite worth reading (if you're not in a hurry, read the rest of them too).

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

回答by Andy

Another option is to do a git fetch Aand git merge A.

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

Blog post describing reason for doing it this way

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

回答by granadaCoder

Here is how I got it to work.

这是我如何让它工作的。

short version:

精简版:

git checkout  feature/mychildbranch

git branch

git checkout  feature/myparentbranch

git pull

git branch

git checkout feature/mychildbranch

git branch

git merge feature/myparentbranch

longer version (explained) I'll use /* as comments */

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

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

If there are no conflicts, you should be where you want to be. If there are conflicts, that's a whole new SOF question/answer IMHO.

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

回答by Aditya Rewari

at child branch B, we can do

在子分支 B,我们可以做

git merge origin A 

This would keep it in sync with parent's origin.

这将使其与父母的原点保持同步。