英文:
Is there a Git command that pulls latest from branch_x and merges into my current branch?
问题 {#heading}
一个我通常使用的与Git一起工作的典型方式是从dev分支中创建feat1分支。然后,我提交代码到feat1。在通过GitLab UI提交合并请求之前,我想确保feat1与dev保持最新(也许我的团队中的另一位开发者在此期间合并了一些内容到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 {#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'-F2' < feat1
或者,你也可以手动明确地运行 fetch,然后是 merge 或 rebase。最终的历史记录将会相同,但本地分支也会被更新以匹配其远程跟踪的对应分支:
git fetch origin dev:dev && git merge devgit fetch origin dev:dev && git rebase dev
无需来回切换/检出分支。 英文:
You most definitely want git pull:
git pull origin devwillfetchrefs and thenmergeorigin/devinto your current branch (assumingfeat1)git pull --rebase origin devwillfetchrefs and thenrebaselocalfeat1on top oforigin/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 < dev
`F1-F2 < feat1
Remote history:
,D3-D4 < origin/dev
-D1-D2
Using pull (with merge strategy):
,D3-D4 < origin/dev
-D1-D2 \ < dev
`F1-F2-F3 < feat1
After pull --rebase:
,D3-D4 < origin/dev
-D1-D2 \ < dev
F1'-F2' < 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 && git merge devgit fetch origin dev:dev && git rebase dev
No need to switch/checkout branches back and forth.
答案2 {#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
51工具盒子