3

I can never seem to figure out how to use git. I just do random commands and it works eventually. This is what I usually do when I want to push:

  • Fetch from upstream

  • Commit

  • Push to upstream

However, the above steps don't work sometimes. For example, when I do fetch from upstream, my local branch doesn't get updated.

How do I merge the remote branch into my local branch? Is there a difference between pull and fetch?

Dao Lam
  • 2,493
  • 10
  • 33
  • 41

3 Answers3

4

"git pull" is equivalent to "git fetch" followed by "git merge"

What you want to do is:

git commit -am "your message here"

This commits your current changes locally, and only locally. Now, assuming branch "foo":

git pull origin foo

This does a "git fetch" followed by a "git merge". Everything in this step is done to your local branch, and still has no effect on the remote branch. Depending on how your git is setup, you might need to specify the branch.

If you have a merge conflict, you will have to fix the files listed, then add them back:

git add path/to/resolved.file

When you are done, push everything you have from your local changes, to the remote server:

git push origin foo

If you pull before committing, that might cause your local branch to not be updated.

tl;dr, always commit before pulling!

Phil
  • 5,381
  • 2
  • 15
  • 22
2

Well to understand differences betwene git fetch and git pull look here.

In the simplest terms, "git pull" does a "git fetch" followed by a "git merge".

You can do a "git fetch" at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy. I have even heard of people running "git fetch" periodically in a cron job in the background (although I wouldn't recommend doing this).

Working with git in eclipse might be a little bit tricky, but if you understand git basics, you should cope with it. I recommend you to read one of git tutorials, so you will be able to understand basic git operations.

Community
  • 1
  • 1
offlinehacker
  • 712
  • 8
  • 13
1

The first step to learn git: Don't use egit.

Now, you are fetching and pulling from upstream. That makes me think that several people have write-access to the same repository, so we probably don't want to be doing a whole lot of merges to complicate history. It would be best to do this. I'm assuming you have already committed a changes or a set of changes to your local master branch that you want to place on the upstream repository which has been pushed to by someone else while you were making your commits.

First, we fetch the new changes but don't use them yet. This updates upstream/master to the new head of the upstream master branch.

git fetch upstream master

Now, we need to pull in these changes. This command rewrites history, and we are assuming that you have not published your changes anywhere yet.

git rebase upstream/master master

If you have published your changes, this messier command is the one you should use (do not use both, just use this one!)

git merge upstream/master master

Now, we can push:

git push upstream master

The first two steps can be shorted to git pull --rebase and git pull for the rebase and merge versions respectively.

If you are already on the master branch, most of those second arguments are superfluous but I wrote them in for clarity. Notably, giving a second argument to git-rebase or git-merge will simply check out that branch before doing the operation. Supplying master to fetch and push is only necessary if you don't have the refs set up to automatically fetch and push master.

alternative
  • 12,098
  • 5
  • 39
  • 41
  • You really should not be on master right? That's no longer a good dev pattern I thought. – Gray May 26 '17 at 19:05