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

本小妞迷上赌 提交于 2019-12-02 18:16:58

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!