38

What I want:

Update all new commits from server with my local repository in all branch but do not merge any branch (just join the history lines).

I am trying this command

git fetch --force --progress --verbose  name@host:/path/to/repository.git 

I thought it will work fine, because it show:

From host:/path/to/repository
  * branch            HEAD       -> FETCH_HEAD

But, what does this output mean? If I see the log, it wasn't updated. If I do a clone from server, all new commits are there. So... The command does not work. Then I try with a branch that exist in server but not in my local repository

git fetch --force --progress --verbose  name@host:/path/to/repository.git my_branch

The result is:

From host:/path/to/repository
  * branch            my_branch       -> FETCH_HEAD

And any success... Even if I not know all branches and my branch was update, I want to fetch this changes and can see in my log.

Any idea to do it work?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Rodrigo
  • 11,909
  • 23
  • 68
  • 101
  • Can you explain what means *join the history lines* for you? – CharlesB Mar 23 '12 at 15:03
  • 1
    I can say: do the merge in history... But not from 2 branches. In other words, I just want update all the history, and only this. – Rodrigo Mar 23 '12 at 16:40
  • 1
    What you want is called fast-forward merge. It only applies when histories have not diverged. If it is the case `git pull master` (equivalent to `git fetch` + `git merge origin/master`) will automatically do this if histories have not diverged. – CharlesB Mar 23 '12 at 16:52
  • @CharlesB you are correct. And what I want is `git pull --no-commit`. Now I do not understand the why of git fetch. It just download from server, so I can do other task in offline mode? – Rodrigo Mar 23 '12 at 19:40

2 Answers2

47

When you fetch you get the remote branches, but you still need to merge the changes from the remote branch into your local branch to see those changes.

After fetching, try this:

git log origin/yourbranchname | head
git log yourbranchname | head

Do you see the difference?

Now do:

git checkout origin/yourbranchname -b newbranchname
git log newbranchname

You should see remote changes in the newbranchname.

You can also merge those changes into your branch with

git checkout yourbranchname
git merge origin/yourbranchname
George Skoptsov
  • 3,831
  • 1
  • 26
  • 44
  • Ok, git fetch do not update the history, but the "data", so I can set off-line and do this merges. This is what fetch do? – Rodrigo Mar 23 '12 at 19:43
  • 1
    Fetch downloads **remote** branches, but it doesn't update **local** branches. When you **merge** remote branches into your local ones, you update local branches. You can see "history" on both local and remote branches. Try `gitk yourbranchname` and `gitk origin/yourbranchname`. – George Skoptsov Mar 23 '12 at 19:45
  • I do it. But I just see the local changes from that branch. I can have make something wrong, but now I understand what stash do. What I want to do is just `git pull --no-commit` this was what I thought fetch do :) – Rodrigo Mar 23 '12 at 19:52
31

I faced this issue before, the main reason is that you didn't config the remote.origin.fetch in your git local config.

use below command to fix your issue:

git config --local --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

after that, run 'git fetch origin', i think you will get the expected output.

0xTang
  • 776
  • 7
  • 11