How to merge code in Eclipse

2019-07-15 20:29发布

问题:

How do I dot the following in Eclipse?

I have created a Android app project in Eclipse and I have put this under version control using eGit and BitBucket (again I don't know if this is the best way to describe it as I'm new to version control). I have written some code and made several commits so far. I have invited a friend who has joined the project. Last night they changed some of the code.

Today I fire up Eclipse and want to continue working on the project, but want to start from where they left off. I right click on the project and go to Team -> Synchronize (simply because this seemed the most logical step to me). Nothing happened as I feared.

How to I get the latest version of the working code onto my local machine? And when I am finished my work, do I just commit as I have been doing by right clicking project and then going to: Team -> Commit followed by Team -> Push to Upstream?

回答1:

Within Eclipse, start using the Git Repository Explorer perspective, and the Git Staging View.

The repository explorer shows your clone, allowing you to see your branches, your change history etc. You can right mouse on your clone's name and choose pull. This will fetch changes from the Bitbucket server and then merge them to your local branch.

Note you should not pull until you have either stashed any locally modified files, or have committed them to your branch. If the pull conflicts with any local changes the action will be rejected.

As you are sharing a branch with other people you should consider modifying your local (master?) branch via Configure Branch... to enable the Rebase option. This means when you merge remote changes over your local branch, any outgoing commits are replayed over the top of the incoming ones. It means when you push you avoid merge points which can clutter up the history. Encourage other people working in their own repos to do likewise.

Secondly, when you are committing changes, learn to use the Git Staging view. This makes it easy to drag and drop changes and commit them in a single go. You can also do this from Team | Add and Team | Commit... but IMO this is far more clumsy.

When you are ready for others to see your committed changes you must push them. Normally you would either right mouse on a project and choose Team | Push to upstream or go to the repository explorer, right mouse on the clone and choose Push to Upstream. If necessary you may have to pull before you can push because the server will reject changes if they do not satisfy merge

This is the basic workflow of using Git within Eclipse. As a general point, also ensure your version of the EGit plugin is at least 2.3.1 or 3.0.1 depending on your version of Eclipse.



回答2:

you can not get the newest code that be changed by your friend is maybe because after he changed but he didn't commit..and now although you cannnot get the changed code but you can also commit your changed code.because when he commit...maybe errors shows then your friends will change his code base the code you have commited.



回答3:

I would recommend against using eGit as I commented here.

If on windows download and install msysgit

Then Right click on your eclipse project folder - select Git Bash here and issue

$ git pull

The problem with fetch is that it only fetches - pull fetches but also merges the commits it fetched