简体   繁体   English

Git Workflow Pull vs Fetch

[英]Git Workflow Pull vs Fetch

I know this question has been asked before but it seems to me that the answer has been changed with time so I'm quite confused. 我知道之前已经问过这个问题,但在我看来答案已经随着时间的推移而改变,所以我很困惑。 So once and for all in 2012 december 22 after domesday, (those of us that survived) what is the recommended way of working with git when you want to pull in the latest change from remote branch say "develop". 所以在2012年12月22日圆满之后,(我们这些幸存下来的人)一劳永逸,当你想从远程分支中获取最新变化时,建议使用git的方式是“开发”。

I use pull and to be honest never used fetch. 我使用拉,说实话从来没用过fetch。 I just feel alarmed that I might be getting my self in to some strange situation done the line. 我只是感到震惊,我可能会让自己陷入一些奇怪的境地。

Here is an example of my workflow: 以下是我的工作流程示例:

git pull origin develop
git checkout -b story-001
...do some work
git commit -am "fixed utests"
.. do some more work
git commit -am "fixed impl for service x"
git rebase develop
git checkout develop
git merge --squash story-001
git commit -m "Story 001 completed <testinfo>"
git push origin develop
..error.. master is head..
git pull origin develop
..maybe merge issue
git mergetool
..resolved problem
git commit -am "resolved merge for story 001"
git push origin develop
git branch -D story-001
....
... and so on
... after a while some changes on remote <develop>
... 
git pull origin develop

As you see no fetch in my world, why should there be? 正如你在我的世界中看不到的那样,为什么会这样?

Well, git pull does git fetch followed by git merge . 好吧, git pull执行git fetch然后是git merge So, if you wish to automatically merge the pull ed repository, then you have no need for git fetch . 因此,如果您希望自动合并pull ed存储库,那么您不需要git fetch

If you want your current (local) work stay on top you do 如果您希望当前(本地)的工作保持领先,那么您就可以了

git fetch
git rebase origin/develop

or 要么

git pull --rebase

Merging in the code ie pull when remote and local has changed, can have the effect that your local work is placed in the wrong order and is overwritten by remote changes. 代码中的合并,即远程和本地更改时的拉动,可能会导致本地工作的顺序错误,并被远程更改覆盖。

So if you want to just grab the remote you should do pull and fetch if you have done some work on your local. 因此,如果你想抓住遥控器,你应该做拉动和获取,如果你已经在你的本地做了一些工作。 Just to keep it simple. 只是为了保持简单。 But most of time the pull will work if you are lucky. 但是大多数时候,如果你运气好的话,拉动会起作用。

This is what I have understood by doing some more research. 这是我通过做更多研究所理解的。 However I'm not sure until I try a little experiment to verify. 但是,在我尝试进行一些实验验证之前,我不确定。

I think the best solution is to use git pull --rebase most of the time, makes more sense somehow. 我认为最好的解决方案是大多数时候使用git pull --rebase,不知何故更有意义。 You could change the default behavior of git pull as I understand it but that can be dangerous as you might forget about it and confuse yourself even more. 您可以根据我的理解更改git pull的默认行为,但这可能很危险,因为您可能会忘记它并使自己更加困惑。

I usually work like this: 我通常这样工作:

git pull origin develop
git checkout -b story-001
git commit -am "..."
# more commits, squash the commits, reorganize

While at this stage, I often do git fetch to get the latest changes. 在这个阶段,我经常进行git fetch来获取最新的更改。 If there are any new changes, I will do git rebase origin/develop to move my work on the feature branch on top of the new changes. 如果有任何新的更改,我将执行git rebase origin/develop以在新更改之上移动我在功能分支上的工作。

# work some more
git checkout develop
git merge # defaults to origin
git merge story-001
git push

With this workflow, I have no merge commits, the history is pretty clean. 有了这个工作流程,我没有合并提交,历史很干净。 Also I don't usually write these commands, try something like git-smart . 此外,我通常不会编写这些命令,尝试像git-smart这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM