Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between merge and commit in Git

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.

like image 609
reedvoid Avatar asked Aug 10 '26 19:08

reedvoid


1 Answers

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.

like image 85
evgeny Avatar answered Aug 13 '26 19:08

evgeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!