0
  1. What I'm trying to do:

I work with two friends on a university project, they made some changes to the code. I've also made a lot of changes to the code, most of which I want to keep.

I'm new to git and don't know where to start. My friend suggested I use:

git pull origin

When I do that I get:

error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge

I read somewhere on stackoverflow: How do you git fetch then merge? "Error: Your local changes to the following files would be overwritten by merge"

That if I don't commit changes to local repository, they'll be lost?

  1. How do you commit locally?
  2. How do I merge changes(Is there a way I could use beyond compare please)?
  3. How do I upload the changes?

Thanks

This is what it looks like now:

Arthur Wulf@SUPERWOLF-PC /c/Current Project/study-wise (master|MERGING)
$ git pull origin master
M       .pydevproject
U       src/app.yaml
M       src/getters/__init__.pyc
M       src/index.yaml
M       src/model/ClassM.pyc
M       src/model/CourseM.py
M       src/model/CourseM.pyc
M       src/model/GeneralM.pyc
M       src/model/LectureM.py
M       src/model/LectureM.pyc
M       src/model/PostClassM.py
A       src/model/PostClassM.pyc
M       src/model/QuestionM.py
M       src/model/QuestionM.pyc
M       src/model/StudentM.py
M       src/model/StudentM.pyc
M       src/model/TopicM.py
M       src/model/TopicM.pyc
M       src/model/__init__.pyc
M       src/setters/__init__.pyc
A       src/setters/setQuestionStats.py
D       src/setters/setRemoveOldData.py
A       src/setters/setStartNewClass.py
A       src/setters/setStudentAnswer.py
D       src/setters/setTopicChanged.py
A       src/setters/setUpdateTopicStats.py
M       src/view/allCourses.html
M       src/view/lecture.html
U       src/view/prof.html
M       src/view/question.html
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.

What I do not understand is how do I beyond compare the files on my local side with the ones on the online origin repository side and merge them?

Community
  • 1
  • 1
AturSams
  • 7,568
  • 18
  • 64
  • 98

4 Answers4

4

The right thing to do is to commit all your changes locally, and then try pulling again. That error is there to protect you from losing local changes that haven't been committed. Creating a commit (committing) is like creating a new snapshot of your code, after which that state is safely recorded.

To answer your questions:

1) How do you commit locally?

Look at the output of git status. If there are any files listed as "Untracked files" that you want to keep in git, use git add <filename>... to indicate that you want them in the next commit. Then look at the files listed as "Changes not staged for commit" - these are files that were already in the repository that you've changed. You can again do git add <filename>... for each of those to indicate that you want exactly that version of that file in the commit that you're preparing. (If you make a further modification to a file, you would have to git add that file again to stage the new contents of the file for the commit.)

Finally, you should run git commit. That will open up an editor where you should enter a helpful commit message - when you save that file and exit the editor, your commit should be created. Then you can run git pull origin master, as you originally wanted to.

(As a shortcut, if you do git commit -a, all the changes to files that were already in the repository will be staged for the commit without you having to git add them.)

2) How do I merge changes(Is there a way I could use beyond compare please)?

The git pull origin master command that you tried actually does two things:

  1. Fetches the branch master from the remote repository called origin
  2. Merges the state of that fetched branch into your current branch.

git is good at avoiding complaining about spurious conflicts, but if you do need help dealing with them, look into git mergetool.

3) How do I upload the changes?

You can do that with git push origin master.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • But how do I do the 'beyond compare' style thing where I can see the differences between the 'online' version and my version and decide which lines of code to keep and discard between them? Is that git mergetool? – AturSams Jun 23 '12 at 11:42
  • Wow, thanks, you really helped me out and saved me a lot of time! I am a little confused about the "enter a helpful message part" but I guess no harm done, I will do that next time. :) – AturSams Jun 23 '12 at 12:03
  • @Arthur: To see the differences between your version and the remote one, you can do: `git fetch origin` (to update your local snapshots of the remote branches) and then use `git diff master origin/master` to see the differences. Typically you wouldn't do a completely manual merge, as you're suggesting, just use some tool (launched with `git mergetool`) to sort out the conflicts. I'm afraid I don't know about "Beyond Compare", being a Linux user, but I'm sure you can find something about `git mergetool` and BeyondCompare with Google. – Mark Longair Jun 23 '12 at 12:56
  • Honestly, your help was priceless on this. It saved me hours of work learning this tool and in crucial time, when the project is due tomorrow. I can fix the conflicts manually.:) It looks like CVS which I am more familiar with. – AturSams Jun 23 '12 at 19:57
  • @ArthurWulfWhite: great, I'm glad to hear that was of help – Mark Longair Jun 24 '12 at 16:59
1

There is a great online book that explains git: Pro Git

So I prefer teaching fishing over giving a fish

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • I have to turn in the project tomorrow, I don't have time to master git nor will I ever use it again for anything. I'm just trying to play along with these guys and upload my changes. I thought it only takes two or three commands and that asking would save me the time of learning from a big general book about git. thanks – AturSams Jun 23 '12 at 11:17
  • @Arthur Wulf White: if you're on windows - the best way for you to do now - is to use TortoiseGit. If not - there is a lot of ways to break everything. Let's see if someone will pass you through this – zerkms Jun 23 '12 at 11:19
  • I have MINGW32? Is that like Tortoise? – AturSams Jun 23 '12 at 11:20
  • @Arthur Wulf White: http://code.google.com/p/tortoisegit/ - it is native win32 application – zerkms Jun 23 '12 at 11:22
  • I thought it was a very simple matter, that is why I asked.. I just don't want to bother with it cause I am unlikely to use it again. (we use cvs where I work) – AturSams Jun 23 '12 at 11:22
  • hmm ok. it looks good, I will definitely try it out and consult the book you recommended as well unless someone is kind enough to guide me through the process. :) – AturSams Jun 23 '12 at 11:24
  • I just own a paid for version of beyond compare and enjoy using that for small projects, I was hoping to use something I own and understand – AturSams Jun 23 '12 at 11:26
  • @Arthur Wulf White: install tortoise and look briefly in book about fetch, commit, merge and push operations. – zerkms Jun 23 '12 at 11:28
  • I installed tortoise, thanks for pointing me in the right direction. – AturSams Jun 23 '12 at 11:29
  • @ArthurWulfWhite I think you are being overly optimistic about never using git again for anything. You will be using some sort of source control at work, and given the popularity and general shift towards Git and Mercurial, it is highly likely you will have to work with one or both of those systems. – Roman Jun 23 '12 at 19:15
1
git pull origin

pull (fetch + merge) retrieves the latest from the remote (origin) and merges it with your code. You cannot merge in new code if the branch you work in has uncommited changes. You can check the branch status using:

git status

to commit locally, usually I do

git add .                          //stage all files
git commit -a -m "local commit"    //commit all staged

this is a local commit to your local repo. Then you can do the pull which will try to merge your recent commit with the remote changes.

The pull is like a commit in a sense that the event of the pull is recorded in the history of the repo. It WILL try to merge changes (I seldom see overwrites) on the files in your branch but like I said, like a commit, the event is recorded and you can just revert.

To upload changes, you can do a push, where remote is the alias of the server to push as stored in your GIT and branch is the branch to push.

git push [remote] [branch]

And seeing that you have discussed with @zerkms with GIT clients, I personally use msysGit which is GIT for Windows. It has a GUI as well as a command line.

Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • now it says: Pull is not possible because you have unmerged files. Please, fix them up in the work tree, and then use 'git add/rm ' as appropriate to mark resolution, or use 'git commit -a'. – AturSams Jun 23 '12 at 11:37
  • @ArthurWulfWhite then there are conflicts during merge. do a `git status` as to what files are in conflict. I think [Mark Longair's answer](http://stackoverflow.com/users/223092/mark-longair) should help you with that. – Joseph Jun 23 '12 at 11:40
1

While you are feeling your way around you should avoid git pull because it does both a fetch (which you want) and a merge (which you probably don't want).

You will need to have a chat with your friends as to what workflow will fit you best - have a look at the git tutorial [git help tutorial works on Git for Windows] and gitworkflows [git help workflows], and of course Nvie's A successful Git branching model (with pictures, though it may be more advanced than you need).

The key is to avoid using a common branch ("our_work") for development hacking when separate "my_branch", "his_branch", and "her_branch", etc. will give each of you the freedom you need, and at the same time you can also have branches for "feature_1", etc., that you use as a team as the features mature in a coordinated manner.

In addition, watch out for the distinction between the branches which are pushed automatically, and the branches that are fetched automatically. They probably are not what you think.

Philip Oakley
  • 13,333
  • 9
  • 48
  • 71