user2636464
user2636464

Reputation: 735

How to avoid git merge conflicts?

I have setup a GitHub repo with two active branches master and development. Development happens on development branch cloned from remote GitHub and then pushed to the same. When work is complete , development is merged to master and tagged. Meanwhile someone makes new changes on his local clone and try to pull the updated code, he gets conflicts.

I worked this way in clear case before and we did not have so much conflicts. Is it something I am doing wrong here in the way of working?

I will try to explain this with an example. I have a build script which has some hard coded values say version . initially, on master and development branch, version is 1.1 . At this point , myself and another person have cloned the code. now, I change the version to 1.2 in development branch, commited and pushed to remote.I also merged the development branch to master and pushed master as well. The other person made changes to same file but not same line and tried to push and it fails. so, he does a git pull but gets a conflict. is anything wrong here ?

Upvotes: 0

Views: 1090

Answers (2)

Mark Bramnik
Mark Bramnik

Reputation: 42541

First of all it looks like the development branch is shared between many developers (otherwise there won't be conflicts)

It looks like the question is essentially about the development branch, the master branch is pretty irrelevant for this question: conflicts happen between local copy of the development branch and remote b during the pull, am I right? (I'll assume this for the answer)

If so... I haven't worked with ClearCase, but how conflicts are resolved if the changes are made on the same file, the same method, the same line? How does clear case is supposed to know which copy to pick during the "pull"? Obviously it should trigger some interactive process that will let the programmer to decide what to pick...

This is exactly the process of conflict resolution. When you say:

I worked this way in clearcase before and we did not have so much conflicts. is it something I am doing wrong here in the way of working?

I would answer that this pretty much depends on the software codebase and programmers and not on the tool itself, namely if people frequently modify the same files they'll get frequent conflicts. Alternatively if the codebase is big enough and developer's work usually doesn't "overlap", the conflict will be resolved automatically.

Now you should understand that conflict resolution is not something bad, its a part of the job. I can only give a couple of advices:

  • pull the changes frequently to the local branch. If the developer takes the change, starts the feature and doesn't pull for say, one month, the chances are that there will be a big conflict. Alternatively, if the developer pulls a couple of times a day, the chance of conflict is small, and the conflict if it exists is easy-to-resolve

  • Consider using git pull --rebase from development branch. This can help to clean up the history of commits. I suspect currently the commit log is "flooded" with numerous merge commits. In general do this is you understand the difference between merging and rebasing.

Upvotes: 1

0xedb
0xedb

Reputation: 364

Yes. In your case you should be fetching updates from remote. More on that here: fetching and pulling

Upvotes: 0

Related Questions