有一个Git命令可以从branch_x中获取最新内容并合并到当前分支吗?

huangapple go评论79阅读模式
英文:

Is there a Git command that pulls latest from branch_x and merges into my current branch?

问题

一个我通常使用的与Git一起工作的典型方式是从dev分支中创建feat1分支。然后,我提交代码到feat1。在通过GitLab UI提交合并请求之前,我想确保feat1dev保持最新(也许我的团队中的另一位开发者在此期间合并了一些内容到dev中)。

通过本地git客户端我所做的是:

git checkout dev
git pull
git checkout -
git merge dev

我认为我可能做错了,有一个更简单的方法来做这个。是否有一条命令,看起来像(当我当前已检出feat1分支时)git do_some_stuff dev,具有我想要的效果?

英文:

A typical way of working with Git for me is to branch off feat1 from dev. Then I commit code into feat1. Before submitting a merge request via the GitLab UI, I want to ensure that feat1 is up to date with dev (maybe another developer on my team merged something into dev in the mean time).

What I do via the local git client is:

git checkout dev
git pull
git checkout -
git merge dev

I presume I am doing this wrong and that there's an easier way to do this. Is there a command that looks like (when I have currently checked out branch feat1) git do_some_stuff dev that has the effect I want?

答案1

得分: 3

你绝对想要使用 git pull

  • git pull origin dev 将会 fetch 引用,然后将 origin/dev 合并到你当前的分支上(假设是 feat1 分支)
  • git pull --rebase origin dev 将会 fetch 引用,然后将本地的 feat1 分支在 origin/dev 之上进行 rebase。请注意,重新基于操作会重写你的历史并且删除/重新创建你的本地提交

以下的ASCII图示将会更清晰地展示这两种方法之间的区别。

初始本地历史:

-D1-D2                  < dev
      `F1-F2            < feat1

远程历史:

      ,D3-D4            < origin/dev
-D1-D2

使用 pull(使用合并策略):

      ,D3-D4            < origin/dev
-D1-D2      \           < dev
      `F1-F2-F3         < feat1

在执行 pull --rebase 后:

      ,D3-D4            < origin/dev
-D1-D2      \           < dev
             F1&#39;-F2&#39;    < feat1

或者,你也可以手动明确地运行 fetch,然后是 mergerebase。最终的历史记录将会相同,但本地分支也会被更新以匹配其远程跟踪的对应分支:

  • git fetch origin dev:dev && git merge dev
  • git fetch origin dev:dev && git rebase dev

无需来回切换/检出分支。

英文:

You most definitely want git pull:

  • git pull origin dev will fetch refs and then merge origin/dev into your current branch (assuming feat1)
  • git pull --rebase origin dev will fetch refs and then rebase local feat1 on top of origin/dev. Note that rebasing will rewrite your history and drop/recreate your local commits

The following ASCII diagrams should make the difference between the two approaches clearer.

Initial local history:

-D1-D2                  &lt; dev
      `F1-F2            &lt; feat1

Remote history:

      ,D3-D4            &lt; origin/dev
-D1-D2

Using pull (with merge strategy):

      ,D3-D4            &lt; origin/dev
-D1-D2      \           &lt; dev
      `F1-F2-F3         &lt; feat1

After pull --rebase:

      ,D3-D4            &lt; origin/dev
-D1-D2      \           &lt; dev
             F1&#39;-F2&#39;    &lt; feat1

Alternatively, you can manually and explicitly run fetch followed by merge or rebase. The resulting history will be identical, but the local branches will also be updated to match their remote-tracking counterparts:

  • git fetch origin dev:dev &amp;&amp; git merge dev
  • git fetch origin dev:dev &amp;&amp; git rebase dev

No need to switch/checkout branches back and forth.

答案2

得分: 1

git pull是不好的,因为它有很多不确定的方面(取决于您的git config设置),而且它没有任何您不能以其他方式完成的功能。此外,如果您永远不打算手动合并到dev,那么您不需要一个本地的dev,也不应该有一个,因为它只会让您感到困惑。

相反,只需保持在您所在的分支,并执行以下命令:

git fetch; git merge origin/dev
英文:

git pull is evil because it has so many indeterminate aspects (things depend on your git config setup), and it does nothing you can't do some other way. Moreover, if you are never going to merge manually into dev, you don't need a local dev and you shouldn't have one, as it is just confusing you.

Instead, simply stay in the branch you are in, and say

git fetch; git merge origin/dev

huangapple
  • 本文由 发表于 2023年8月10日 16:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定