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开发者_如何学Python 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 Iadd
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 directoryI press the
commit
button in Git GUI, now there are no more files in any of the two side panels, noUnstaged Changes
and noStaged 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.
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 push
ing 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.
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.
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.
精彩评论