git pull 从 master 进入开发分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20101994/
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
git pull from master into the development branch
提问by Matthew Colley
I have a branch called dmgr2 (development) and I want to pull from the master branch (live site) and incorporate all the changes into my development branch. is there a better way to do this? here is what I had planned on doing, after committing changes:
我有一个名为 dmgr2(开发)的分支,我想从主分支(实时站点)中提取并将所有更改合并到我的开发分支中。有一个更好的方法吗?这是我在提交更改后计划做的事情:
git checkout dmgr2
git pull origin master
this should pull the live changes into my development branch, or do I have this wrong?
这应该将实时更改拉入我的开发分支,还是我有这个错误?
回答by torek
The steps you listed will work, but there's a longer way that gives you more options:
您列出的步骤将起作用,但有一种更长的方法可以为您提供更多选择:
git checkout dmgr2 # gets you "on branch dmgr2"
git fetch origin # gets you up to date with origin
git merge origin/master
The fetch
command can be done at any point before the merge
, i.e., you can swap the order of the fetch and the checkout, because fetch
just goes over to the named remote (origin
) and says to it: "gimme everything you have that I don't", i.e., all commits on all branches. They get copied to your repository, but named origin/branch
for any branch named branch
on the remote.
该fetch
命令可以在 之前的任何时候完成merge
,即,您可以交换获取和结帐的顺序,因为fetch
只需转到命名的远程 ( origin
) 并对其说:“给我你拥有的所有我没有的东西",即所有分支上的所有提交。它们被复制到您的存储库,但以远程命名origin/branch
的任何分支命名branch
。
At this point you can use any viewer (git log
, gitk
, etc) to see "what they have" that you don't, and vice versa. Sometimes this is only useful for Warm Fuzzy Feelings ("ah, yes, that is in fact what I want") and sometimes it is useful for changing strategies entirely ("whoa, I don't want THAT stuff yet").
此时,您可以使用任何查看器(git log
、gitk
等)来查看您没有的“他们拥有的东西”,反之亦然。有时这仅对温暖的模糊感觉有用(“啊,是的,这实际上就是我想要的”),有时它对完全改变策略很有用(“哇,我还不想要那种东西”)。
Finally, the merge
command takes the given commit, which you can name as origin/master
, and does whatever it takes to bring in that commit and its ancestors, to whatever branch you are on when you run the merge
. You can insert --no-ff
or --ff-only
to prevent a fast-forward, or merge only if the result is a fast-forward, if you like.
最后,该merge
命令接受给定的提交(您可以将其命名为 )origin/master
,并尽一切努力将该提交及其祖先带到您运行merge
. 如果您愿意,您可以插入--no-ff
或--ff-only
阻止快进,或仅当结果是快进时才合并。
When you use the sequence:
使用序列时:
git checkout dmgr2
git pull origin master
the pull
command instructs git to run git fetch
, and then the moral equivalent of git merge origin/master
. So this is almostthe same as doing the two steps by hand, but there are some subtle differences that probably are not too concerning to you. (In particular the fetch
step run by pull
brings over onlyorigin/master
, and it does not update the ref in your repo:1any new commits winds up referred-to only by the special FETCH_HEAD
reference.)
该pull
命令指示 git 运行git fetch
,然后是git merge origin/master
. 因此,这与手动执行这两个步骤几乎相同,但存在一些您可能不太关心的细微差别。(特别是fetch
run by的步骤只pull
带来了,它不会更新你的 repo 中的 ref:1任何新的提交都只被特殊引用引用。)origin/master
FETCH_HEAD
If you use the more-explicit git fetch origin
(then optionally look around) and then git merge origin/master
sequence, you can also bring your own local master
up to date with the remote, with only one fetch
run across the network:
如果您使用更明确的git fetch origin
(然后可选地环顾四周)然后进行git merge origin/master
排序,您还可以使您自己的本地master
与远程保持同步,只需fetch
在网络上运行一个:
git fetch origin
git checkout master
git merge --ff-only origin/master
git checkout dmgr2
git merge --no-ff origin/master
for instance.
例如。
1This second part has been changed—I say "fixed"—in git 1.8.4, which now updates "remote branch" references opportunistically. (It was, as the release notes say, a deliberate design decision to skip the update, but it turns out that more people prefer that git update it. If you want the old remote-branch SHA-1, it defaults to being saved in, and thus recoverable from, the reflog. This also enables a new git 1.9/2.0 feature for finding upstream rebases.)
1第二部分在 git 1.8.4 中已更改——我说“已修复”,它现在机会性地更新“远程分支”引用。(正如发行说明所说,故意设计决定跳过更新,但事实证明,更多的人更喜欢 git update 它。如果你想要旧的远程分支 SHA-1,它默认保存在,因此可以从 reflog 中恢复。这也启用了一个新的 git 1.9/2.0 功能来查找上游 rebase。)
回答by greenridinghood
Situation: Working in my local branch, but I love to keep-up updates in the development branch named dev
.
情况:在我的本地分支工作,但我喜欢在名为dev
.
Solution: Usually, I prefer to do :
解决方案:通常,我更喜欢这样做:
git fetch
git rebase origin/dev
回答by Pratik Gaurav
This Worked for me. For getting the latest code from master to my branch
这对我有用。用于从 master 获取最新代码到我的分支
git rebase origin/master
git rebase origin/master
回答by user10556443
Scenario:
场景:
I have master updating and my branch updating, I want my branch to keep track of master with rebasing, to keep all history tracked properly, let's call my branch Mybranch
我有主更新和我的分支更新,我希望我的分支通过变基跟踪主,以正确跟踪所有历史记录,让我们称我的分支为 Mybranch
Solution:
解决方案:
git checkout master
git pull --rebase
git checkout Mybranch
git rebase master
git push -f origin Mybranch
- need to resolve all conflicts with git mergetool &, git rebase --continue, git rebase --skip, git add -u, according to situation and git hints, till all is solved
- 需要使用 git mergetool &, git rebase --continue, git rebase --skip, git add -u 解决所有冲突,根据情况和 git 提示,直到全部解决
(correction to last stage, in courtesy of Tzachi Cohen, using "-f" forces git to "update history" at server)
(对最后阶段的更正,由 Tzachi Cohen 提供,使用“-f”强制 git 在服务器上“更新历史记录”)
now branch should be aligned with master and rebased, also with remote updated, so at git log there are no "behind" or "ahead", just need to remove all local conflict *.orig files to keep folder "clean"
现在分支应该与 master 和 rebase 对齐,并且远程更新,所以在 git log 没有“behind”或“ahead”,只需要删除所有本地冲突 *.orig 文件以保持文件夹“干净”