19

We are following this model of using git: http://nvie.com/posts/a-successful-git-branching-model/

I'm trying to learn to use git fetch and git merge on the specific merge instead of just pulling. For my question, what I did was made changes to a branch and pushed those changes to the branch. I see those changes on github. Then I switch to master to fetch those changes. So I

git checkout master
git fetch  // terminal shows nothing
git fetch origin  // terminal shows nothing

Am I using the commands correctly for fetching? I don't see anything on the console. But then when I use a tool like SourceTree, and I fetch, it updates their tree and I can see the changes.

Assuming I get this next step to work, and I see the different changes were made, do I just do a git merge <hash of the last commit or commit I want to merge in>? Thanks!

Crystal
  • 28,460
  • 62
  • 219
  • 393
  • 5
    If everything is up to date, then `git fetch` will output nothing. – dyng Jul 02 '13 at 06:27
  • @dyng I don't think master is up to date since when I build, I don't have my latest features that I had on that last branch. Does that make sense? – Crystal Jul 02 '13 at 06:32
  • By default, if you have set an upstream for `master`, `git fetch origin` will fetch the code to `origin/master`(*not* `master`), I mean `origin/master` up to date when I say up to date. You can check it by `git diff master origin/master`. If not, maybe you have not set the upstream. – dyng Jul 02 '13 at 06:46
  • 1
    fetching is to update internal references and objects (inside .git ) from the repote repository, but to see the latest features you have to merge them. If you are already in your local master branch, you can do that : `git merge origin/master` (this will merge all the modifications implied by the remote "origin/master" in your local "master" branch) – Asenar Jul 02 '13 at 08:08
  • if you get no output but the return code is 1, double check what `git config remote.origin.fetch` is. it may be invalid, which could cause that. – starwarswii Oct 28 '22 at 21:09

4 Answers4

23

git fetch or git fetch origin are fine, but if there is nothing to do, nothing is displayed.

You can use git fetch -v to be more verbose and display more informations

Asenar
  • 6,732
  • 3
  • 36
  • 49
8

Regarding the last part, as mentioned in "Fun with FETCH_HEAD", you can do:

$ git fetch git://repo.or.cz/stranger.git for-junio
$ git log -p ..FETCH_HEAD
$ git merge FETCH_HEAD

Because "pull" is "fetch + merge", we are explicitly decomposing the operation into two steps.

"fetch" leaves the information on the branch it found on the other side in FETCH_HEAD, which you can use as to name the commit object sitting at the tip of the branch.
"git log -p ..FETCH_HEAD" (notice the double dots) lets you view the commits he has but not in your branch with their changes in patch form to inspect what he did was sensible.
If you are satisfied, you can use FETCH_HEAD to merge the commit in to your branch.

(See for instance this answer)

As detailed in "How Do I 'git fetch' and 'git merge' from a Remote Tracking Branch (like 'git pull')", you don't have to use FETCH_HEAD if you have a upstream branch defined:

git fetch origin
git merge origin/an-other-branch

git fetch origin an-other-branch stores the fetched tip in FETCH_HEAD, but not origin/an-other-branch (i.e. the usual ‘remote tracking branch’).
So, one could do git fetch origin an-other-branch && git merge FETCH_HEAD.

For the first part, make sure your branch has an upstream branch set to your remote upstream repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

You might be using the Git repository HTTP URL and, depending on how the authentication and network are set up, you should use the Git URL instead.

If your Git-remote is called origin:

  • First check that the assumption is correct, via git remote get-url origin.
    If HTTP, it will return something like https://gitlab.yourdomain.net/repoGroupFoo/repoBar.
  • Go on the repository via Web, click on the "Clone" button and you'll see two URLs; copy the URL that uses Git protocol instead of HTTP.
    Or simply build it up starting from the HTTP URL components: like from https://gitlab.yourdomain.net/repoGroupFoo/repoBar ➡️ to git@gitlab.yourdomain.net:repoGroupFoo:repoBar.git.
  • Change protocol via git remote set-url origin git@gitlab.yourdomain.net:repoGroupFoo:repoBar.git

Try git fetch again.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
1

I found this because I was having a similar problem. I don't know the whys, but in my case I needed to explicitly name the branch which was updated:

$ git fetch [origin] [branch]

Let me explain--I use git for small scripts. I start the repo with master. After the script is deployed in production, and I still want to work on it, I will make a dev branch. But most projects just have one branch, or I just don't keep dev once merged.

Today, I updated remote master with a few minor changes, and wanted to update my local system with the changes. Out of habit, I just did $ git fetch. Nothing happened. Then I found your post. First, I added the project name (origin) and did $ git fetch [origin]. This updated both dev and master to the last time they shared a merge back in January. Then, OH! I ran $ git fetch [origin] master, and the changes were now downloaded.

xtian
  • 2,765
  • 7
  • 38
  • 65