标签云

微信群

扫码加入我们

WeChat QR Code


I found this well written article about git fetch and git pull it's worth the reading: longair.net/blog/2009/04/16/git-fetch-and-merge

2018年10月22日53分27秒

Our alternative approach has become git fetch; git reset --hard origin/master as part of our workflow. It blows away local changes, keeps you up to date with master BUT makes sure you don't just pull in new changes on top on current changes and make a mess. We've used it for a while and it basically feels a lot safer in practice. Just be sure to add/commit/stash any work-in-progress first !

2018年10月22日53分27秒

Make sure you know how to use git stash correctly. If you're asking about 'pull' and 'fetch' then maybe 'stash' will also need explaining...

2018年10月22日53分27秒

Lots of folks coming from Mercurial keep using "git pull", thinking it's an equivalent for "hg pull". Which it's not. Git's equivalent of "hg pull" is "git fetch".

2018年10月22日53分27秒

git fetch command fetching updated code with branch and also will get newly added branches in your local, git pull command fetch only updated code of current branch only

2018年10月22日53分27秒

"A "git pull" is what you would do to bring your repository up to date" <- isn't the repository update already done by fetch? don't you mean it brings your local branches up-to-date with the remote branches? To the merge: It merges the remote branches with your local copies of those branches, or what exactly does it merge here?

2018年10月22日53分27秒

Albert: Yeah, it's weirdly worded. git pull will always merge into the current branch. So you select which branch you want to pull from, and it pulls it into the current branch. The from branch can be local or remote; it can even be a remote branch that's not a registered git remote (meaning you pass a URL on the git pull command line).

2018年10月22日53分27秒

espertus: No. Pushing never automatically does a merge. The user is expected to pull, resolving any merge conflicts locally, then push back to the remote.

2018年10月22日53分27秒

If I am at /home/alice/ and do git fetch /home/bob, what parameters should I pass to the subsequent git merge ?

2018年10月22日53分27秒

Note to people learning Git: pull can't actually be emulated by a fetch plus a merge. I just fetched a change where only a remote branch pointer changes, and merge refuses to do anything. pull, on the other hand, fast-forwards my tracking branch.

2018年10月22日53分27秒

Agreed, great comment. Which is why I hate git pull. When would it ever make sense to let a revision tool make code edits for you? And isn't that what merging two files is doing? What if those two edits are physically separated in the file, but LOGICALLY at odds?

2018年10月22日53分27秒

elexhobby short put, git fetch only updates your .git/ directory (AKA: local repository) and nothing outside .git/ (AKA: working tree). It does not change your local branches, and it does not touch master either. It touches remotes/origin/master though (see git branch -avv). If you have more remotes, try git remote update. This is a git fetch for all remotes in one command.

2018年10月23日53分27秒

Tino yours is really the most important point. People may not know that "remote" branches are actually stored as a bunch of hashes in .git/refs/remotes/origin/.

2018年10月22日53分27秒

When you fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository - how do I see what was brought from the remote and how do I merge it into my local branches?

2018年10月22日53分27秒

Tino What I still don't understand is... what's the point? Why use fetch if it only updates .git? What is the intended benefit and what am I supposed to do after that?

2018年10月22日53分27秒

Technically, the local and remote repositories are really one and the same. In Git, a repository is a DAG of commits pointing to their parents. Branches are, technically, nothing more than meaningful names of commits. The only difference between local and remote branches is that remote ones are prefixed with remoteName/ Git from the ground up is a very good read. Once you get an understanding of how Git works - and it's beautifully simple, really - everything just makes sense.

2018年10月22日53分27秒

Thanks much for the explanation. I didn't really understand until now that Git was designed so you didn't have to have a central repository. Everyone always says "DVCS" when describing Git, but as a relatively new programmer, that means nothing to me. I've never seen a CVCS, and I've also never not worked with a cental remote repository when collaborating with others (i.e. Github), so until now I've yet to understand what made Git special.

2018年10月22日53分27秒

So, based on this, why ISN'T it a good idea to git-fetch with a cron job? Always keeping a copy of the remote you're working with on your local machine seems like a good idea. In fact, I feel like writing a script that checks to see if I've updated my remote in the past 24 hours and linking it up with a udev hook for internet connection.

2018年10月22日53分27秒

One reason why it ISN'T a good idea to have a cron job: often when working on either a new ticket, or on updates to a branch, I like to see the changes being fetched. If the changes don't come in during a fetch, I'll be more confident in asking my fellow programmer 'hey did you push?'. I also get a sense of how much 'churn' in the repository since I last fetched. This also help to give me sense of the amount and speed of changes currently being made to this repository.

2018年10月22日53分27秒

Nabheet Thing is that, Git is content-oriented. It stores data only once, and points to it multiple times. That's why in Git, even multiple commits on top of an original don't affect the size of the repo much, since most of the objects are the same.

2018年10月22日53分27秒

An updated image with git clone and git merge would be very helpful!

2018年10月22日53分27秒

Yes, please add git merge - it should clearly show that merge called separately is NOT the same as calling pull because pull is merging from remote only and ignores your local commits in your local branch which is tracking the remote branch being pulled from.

2018年10月22日53分27秒

A picture is worth a thousand words! Is the updated image with clone and merge data flow ready somewhere? Any other data flow besides what's already in the diagram?

2018年10月22日53分27秒

Contango please add clone and merge. Would be helpful for newbies like me.

2018年10月22日53分27秒

There are two diagrams showing clone and merge in other answers (below) by th3sly and thedarkpassenger.

2018年10月22日53分27秒

Great addition! I was confused by the dots, isn't it: git diff origin

2018年10月22日53分27秒

why not git diff ..origin?

2018年10月22日53分27秒

git diff origin and git diff ..origin seem to work but not this weird ... stuff

2018年10月22日53分27秒

Compustretch There was not supposed to be a space. git diff ...origin is equivalent to git diff $(git-merge-base HEAD origin) origin (see the git diff [--options] <commit>...<commit> [--] [<path>…] section of kernel.org/pub/software/scm/git/docs/git-diff.html#_description), which is different from git diff origin; git diff ...origin is conceptually the changes made in origin since the current branch branched from origin, while git diff origin includes also the reverse of the changes made in the current branch since it branched from origin.

2018年10月22日53分27秒

none of the .. commands worked for me (on Windows), but git diff origin/master works, as mentioned below

2018年10月22日53分27秒

origin/master is a local branch that is a COPY of master on origin. When you fetch, you update local:/origin/master. Once you really grok that everything in git is a branch, this makes a lot of sense and is a very powerful way to maintain different changesets, make quick local branches, merge and rebase, and generally get a lot of value out of the cheap branching model.

2018年10月22日53分27秒

Still confusing. I thought git fetch was to literally download changes on the remote repo into your local repo, but NOT commit them - ie, they still need to be added/committed to your local repo.

2018年10月22日53分27秒

fetch only pulls from remote/origin (github) to your local origin. But it doesn't merge it to your actual working files. if you do a pull it will fetch and the merge to your current working files

2018年10月22日53分27秒

I think the picture got to show that it affects the local repo too. That's is, Git pull is a combination of affecting the local repo and working copy. Right now it seems it just affect the working copy.

2018年10月22日53分27秒

太極者無極而生 Agreed -- this image is pretty misleading, because it makes it look like git pull is skipping the fetch, which of course is inaccurate.

2018年10月22日53分27秒

thedarkpassenger Why don't you update your image. Then, it will be best snap shot for newbies. ^^

2018年10月22日53分27秒

whats a difference between a 'Local Repository' and a 'Working Copy'? Aren't they both local on the computer?

2018年10月22日53分27秒

Sounds to me like if someone just wants the local code to reflect "the tip", they should use git clone. I put the tip in quotes, as I assume it would mean whatever master is and what someone would "Download as zip" from github.com

2018年10月22日53分27秒

what if you're not happy with the changes after you git fetch? what to do next?

2018年10月22日53分27秒

Your paragraph on rebase was just what I was looking for. The whole idea about zeroing out of everything, updating from remote, then replaying your changes on top of previous commits that happened while you were working. Perfect explanation assuming it's correct. ;)

2018年10月22日53分27秒

Very interesting, but I can't really see a use case where you want "just the code". Et what happen with your code when you fetch? Is it erased? What happen whith the remote changes? How does it goes into your repo whithout erasing your code if you don't merge?

2018年10月23日53分27秒

e-satis: The remote branch is also stored locally on your machine. So when you do git fetch it fetches changes from the repository and updates your local remote branch. It does not affect your local branch which tracks the local remote branch, so does not affect your working copy. Now, when you do a merge it will merge the fetched changes with your local branch.

2018年10月22日53分27秒

A simple use case for the fetch command: perform time consuming operations involving other people's recent commits, such as a merge or a code review, accessing only your up-to-date local repository without network connectivity requirements, because you previously used fetch to download everything you need quickly (e.g. while you are visiting some other developer and connected to some other repository's network). The pull command would download the same commits, but the merging it performs can be undesirable.

2018年10月22日53分27秒

You probably want to skip the pull and just do a "git rebase origin" as the last step since you already fetched the changes. The reason is that someone could have pushed changes in the time since you did the fetch and these would not have been in fetch that you did the diff review on.

2018年10月22日53分27秒

JustinOhms If git pull --rebase is not the right thing in the given situation, is it right if it is done in two steps? If it is the right thing to do, what is extra the benefit to doing it in two steps?

2018年10月22日53分27秒

Kaz - because the rebase is not automatic. Fetching the changes first allows you to make the judgement call. It doesn't fix the problem with rebasing history you've already pushed. It will allow you to see if it is safe to rebase changes you have not already pushed.

2018年10月22日53分27秒

JustinOhms How would you decide whether it is safe to rebase changes? I would just try git rebase, and backtrack if it made a mess, in which case I might as well do git pull --rebase. But maybe you have some other way?

2018年10月22日53分27秒

KaZ gitk allows you to see the branch structure visually. It will show your the position of your local head, remotes, and your branch structures in relation to what you have fetched. This way you can ensure that you are not rebasing fetched changes that are based on an ancestor that is prior to what you have already pushed to your remote(s).

2018年10月22日53分27秒

Use rebase when you are working on a local branch not already pushed. If you are working on a branch that exists in the remote, rebase can result in some nasty issues so you should prefer a regular merge.

2018年10月22日53分27秒

Nice tip, though it's worth mentioning to new git users that rebase modifies commit hashes (I found that surprising coming from subversion).

2018年10月22日53分27秒

Can you explain what is difference between git pull and git pull --rebase ?

2018年10月22日53分27秒

People, click on the link to interact with the different columns. This cheatsheet is the best resource I've seen to fully understand the differences between each command.

2018年10月22日53分27秒

Doesn't a git pull also perform a merge, i.e. going all the way to the working copy?

2018年10月22日53分27秒

Good point, yes it will put all the changes in your working copy, and then you can commit it yourself into the local repo. I will update the visual.

2018年10月22日53分27秒

JustusRomijn Doesn't a pull also update the local repository? Shouldn't there be an asterisk between the origin and working copy asterisks?

2018年10月22日53分27秒

user764754 When you pull, your working copy get the changes (there can also be some conflicts which you might need to resolve). You still have to commit it into your local repository.

2018年10月22日53分27秒

JustusRomijn:Thanks for the illustration.It would be great if you can make the diagram more comprehensive by illustrating the effects of operations such as reset ,cherry pick on the repository states.

2018年10月22日53分27秒

OK, Vishal, it's meaning it will change both, I update the image to avoid confusion... Hope this can help more people...

2018年10月22日53分27秒

that image is great

2018年10月22日53分27秒

If you like the image then take a look at the git cheat sheet, which is the same sort of things for all git commands... ndpsoftware.com/git-cheatsheet.html

2018年10月23日53分27秒

Doesn't clone also affect the local repository (copying all the history from remote)?

2018年10月22日53分27秒

Unfortunately, I have no idea what this diagram is supposed to show...

2018年10月22日53分27秒

Not that hard to read: the boxes represent the status of a repo, that in each row changes in time left-to-right after the reported operation in row 2 of the box. The labels R0n are tags in git, and a tag with a + is yet uncommited stuff. Sanbox is used for your working folder, which is different from the repo folder, where commited stuff is stored.

2018年10月22日53分27秒

very good answer

2018年10月23日53分27秒

if you could change 'Main Branch' to 'Remote Repo', it would be a great answer.

2018年10月23日53分27秒

I'd rather say git pull == git fetch + git merge :)

1970年01月01日00分03秒

But git pull --rebase = git fetch + git rebase

2018年10月22日53分27秒

And neither help if git thinks you are behind by commits and can "fast-forward", which upon I ended up rm -rfing the whole thing and starting over. Stupid Git, please just let me get current so I can go back to work?

2018年10月22日53分27秒

Guys, I still don't understand it. So can we skip git fetch and use git pull directly? The reason is that git pull already includes git fetch.

2018年10月22日53分27秒

Very nice explanation.

2018年10月23日53分27秒

The figure presents a great representation to explain the differences. Congrats!

2018年10月22日53分27秒

Nice graph, but you might want to explain why you use "rebase" when the graph says "merge".

2018年10月22日53分27秒

merge will represent another branch commit and produce new commit which contains commits as a reference. but rebase will replicate commits from another branch it wont create new commit rather than it replicate

2018年10月22日53分27秒