开发者

How two people, concurrently editing the same file is handled?

开发者 https://www.devze.com 2023-01-21 22:43 出处:网络
I believe the title says it. I\'m new to source control thingy. So, let\'s say I have two developers working on the same project and they started editing the same f开发者_开发知识库ile(s) at the same

I believe the title says it. I'm new to source control thingy.

So, let's say I have two developers working on the same project and they started editing the same f开发者_开发知识库ile(s) at the same time then everyone of them send the new version at a slightly different time. From what I understand the one who sends the changes last will have his changes kept, the other one's code will be in the archive only!!!

Is that correct?

Please clarify. Thanks.


No, that's not quite correct. It depends somewhat on which version control software you're using, but I like Git so I'll talk about that.

Suppose we have a file Foo.java:

class Foo {
    public void printAWittyMessage() {
        // TODO: Be witty
    }
}

Alice and Bob both modify the file. Alice does this:

class Foo {
    public void printAWittyMessage() {
        System.out.println("Alice is the coolest");
    }
}

and Bob does this:

class Foo {
    public void printAWittyMessage() {
        System.out.println("Alice is teh suk");
    }
}

Alice checks her version in first. When Bob attempts to check his in, Git will warn him that there is a conflict and won't allow the commit to be pushed into the main repository. Bob has to update his local repository and fix the conflict. He'll get something like this:

class Foo {
    public void printAWittyMessage() {
<<<<< HEAD:<some git nonsense>
        System.out.println("Alice is the coolest");
=====
        System.out.println("Alice is teh suk");
>>>>> blahdeblahdeblah:<some more git nonsense>
    }
}

The <<<<<, ===== and >>>>> markers show which lines were changed simultaneously. Bob must resolve the conflict in some sensible way, remove the markers, and commit the result.

So what eventually lives in the repository is:

Original version -> Alice's version -> Bob's conflict-fixed version.

To summarise: the first to commit gets in without any problems, the second to commit must resolve the conflict before getting into the repository. You should never end up with someone's changes being clobbered automatically. Obviously Bob can resolve the conflict incorrectly but the beauty of version control is that you can roll back the incorrect fix and repair it.


Much depends on the system you're using.

However in the common case is: who commits his changes second would have to perform a "merge" operation. Meaning s/he would need to compare the two files and come up with a merged version. However (!) many popular system (including IDE) comes with smart tools to assist you doing that.

Here are some tools like that compared: http://en.wikipedia.org/wiki/Comparison_of_file_comparison_tools

0

精彩评论

暂无评论...
验证码 换一张
取 消