3

I'm a complete newbie to Git, not really sure what's going on. My buddy and I are working on a project together.

I fetched all the files from a remote server git fetch, so I now have a whole bunch of files.

  • I edit some of them, he edits some of them, etc.

  • I keep doing fetch everyday, and he begins to tell me that he has modified some of the files and updated them, but these changes do not show up on my end.

  • I open up Git GUI in Windows, on the left side there are two panels.

    One says Unstaged Changes - I'm taking this to mean these are things I changed which will not be updated to a local repository unless I add them.

    The other one says Staged Changes (Will Commit). Inside this window, when I click on some of the files, I do see the updates my friends have made which are NOT showing up in the files I'm editing, and I think I also see changes I've made.

  • I add all the files with git add . in my directory

  • I press the commit button in Git GUI, now there are no more files in any of the two side panels, no Unstaged Changes and no Staged Changes (Will Commit).

  • I check all the files and it seems like the changes from both my end and my friend's end have been merged into one file.

I'm still not 100% sure what happened.

  • Question 1: Did I do this right?

  • Question 2: What exactly does merge do?

Because I keep merging with git merge origin/master and merging doesn't seem to do a damn thing. I thought commit just writes a record down of your current version into some hash codes, but seems like commit is actually doing what I thought merge does - it's merging changes.

Sorry for the long-winded question, just very confused.

Barn Monkey
  • 245
  • 3
  • 11
reedvoid
  • 1,203
  • 3
  • 18
  • 34
  • 1
    You might want git pull instead of git fetch. – beatgammit Sep 17 '11 at 02:19
  • 4
    Don't worry, git is confusing to everyone at first. Then one day it will 'click' and you'll wonder how you ever lived without it :) I went through a period of curiosity, then acceptance, then hatred, then back to acceptance, and love. Its just the git way :) – CodingWithSpike Sep 17 '11 at 02:27

3 Answers3

6

I believe you're misunderstanding git fetch.

It only fetches the changes made to a remote repository, in your case the master branch in the origin repo, but it does not apply them to your tree (it only stores them locally in the .git directory). git merge, on the other hand, applies the remote changes onto your repository.

You have to use git pull origin master to pull your friend's changes and merge them into your working tree. Essentially, a git pull is the same as git fetch followed by git merge.

Check this out: Git Fetch vs Pull


ALTERNATIVELY

Your friend/you are not pushing to the origin repository. Is your friend doing git push origin master after he commits? Commits are local, and to "share" them with the rest of the world, you must git push.

The only exception is when he is working directly on the origin repository. Then he does not need to push as all his commits are already on origin in his branch.

Community
  • 1
  • 1
evgeny
  • 2,564
  • 17
  • 27
  • But this still doesn't solve my problem: merge doesn't seem to do anything. Every time I do merge it keeps saying "Already up-to-date". But I can see things inside the Git GUI that there are changes that haven't been "merged". – reedvoid Sep 17 '11 at 02:29
  • Please post a screenshot, I am curious as to what you mean. – evgeny Sep 17 '11 at 02:33
  • So you guys seem to imply that "merge" actually DOES something? Because I've never seen it do anything on my end. – reedvoid Sep 17 '11 at 02:33
  • Also, is your friend pushing? You can check it by cloning the repository afresh and seeing if the changes he made are there. – evgeny Sep 17 '11 at 02:34
  • Ohh... I need to commit before I can merge, is that right? i.e. I need to record my changes first before it can merge changes from other people?? Maybe that's why merging doesn't work but commit seems to work. – reedvoid Sep 17 '11 at 03:32
  • I think I know what the problem is. I've been using Aptana Studio 3.0 to import via Git, and it has created some weird local branch which is screwing everything up (I don't quite understand why or how it's screwing it up, but it is). Not gonna import using Aptana anymore. – reedvoid Sep 17 '11 at 04:05
  • 1
    Yeah, committing is required before a merge. Or you could `stash` your changes, merge, and replay the changes. And I have no idea about Aptana, merging/pulling/pushing from/to a specified repo should work. – evgeny Sep 17 '11 at 04:21
  • 1
    @evgeny: a couple of mistakes in your (otherwise good) answer - it should be `git pull origin master` and `git push origin master`. Also, when you say "in your case `origin/master`" that rather blurs the difference between `master` in the `origin` repository, and the remote-tracking branch `origin/master` - I think it's important for people to realize that `origin/master` is just a branch that points to where `master` was in `origin` the last time there was a successful `git fetch`, more or less :) – Mark Longair Sep 17 '11 at 07:25
  • I think I know too little to even understand these answers... time to read up some more. Thanks to everyone who tried to help! – reedvoid Sep 19 '11 at 01:06
1

That would take a long while to explain, and I've already done it in a series of blog articles. Take a look at them. They walk you through the basic stuff like this from a beginner's perspective.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
0

If you value a good reading, I would suggest you the book Version Control with Git to cement your knowledge of Git. Articles surely are more immediate and cheaper, but Git is a pretty complex beast and I believe that a good basis pays off in the long term.

My two cents.

Luca Geretti
  • 10,206
  • 7
  • 48
  • 58