git merge不会引发冲突

时间:2016-07-27 07:10:05

标签: git github merge

我和我的一位同事在同一个项目上工作 每次我们单独进行更改时,我们都没有使用git 但现在我们想把我们的项目合并在一起。

我做的是,我在项目文件夹中初始化了git 我承诺了所有的改变 我做了另一个,我们称之为“A”。我“checkout”分支并手动将所有文件夹和文件添加到项目文件夹中 然后我添加并提交了更改。

然后我检查(编辑)“master”分支并输入:“git merge A”。它合并了一切,但没有引起任何冲突 它基本上做了什么是自动合并它,删除我的原始文件的一半,并添加他的。

我不希望它以这种方式发生。我们想要的是,git应该合并所有内容并引发一切冲突。一切。
让我手动完成所有更改 有没有办法做到这一点?

编辑: 这一行

git checkout @~

给了我这个:

$ git checkout @~
Note: checking out '@~'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 9a3e723... Asc

这会再次删除我的一半文件。但是,当我'再次结帐主'时。我的档案又回来了。

1 个答案:

答案 0 :(得分:3)

  

它基本上做了什么是自动合并它,删除了我的原始文件的一半,并添加了他的

考虑到你在主人身上录制了A,这是有道理的:

--x (master)
   \
    y (A, where some folders are missing)

git merge报告来自A master的修改,作为快进合并。

 git checkout master
 git merge A

-- x--y (master, A)

你想要做的是从空提交开始的2个分支:

git init .
git commit --allow-empty -m "First initial empty commit"
git add .
git commit -m "My code"
git checkout @~
git checkout -b A
# copy other code
git add .
git commit -m "other code"

然后你可以合并:

--E--x (master)
   \
    y (A)

这会导致冲突,因为这次共同的祖先是'E'(空提交),这意味着在masterA中记录了并发更改初始提交。

git checkout master
git merge A

--E--x--M (master)
   \   /
    y-- (A)

测试

初始化新仓库

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git init .
> git commit --allow-empty -m "empty"
[master (root-commit) ba36e82] empty

添加我的代码'm'和公共文件夹'c'

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> mkdir m
> touch m\m.txt
> mkdir c
> echo c>c\c.txt
> touch c\cm.txt

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git add .

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git st
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   c/c.txt
        new file:   c/cm.txt
        new file:   m/m.txt


vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git commit -m "m head"
[master 7c1a82d] m head
 3 files changed, 1 insertion(+)
 create mode 100644 c/c.txt
 create mode 100644 c/cm.txt
 create mode 100644 m/m.txt

从空提交创建分支A

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git checkout @~
Note: checking out '@~'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at ba36e82... empty

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> dir
 Volume in drive C is OSDisk
 Volume Serial Number is xxxx-yyyy

 Directory of C:\Users\vonc\prog\git\tests\merge

27/07/2016  13:37    <DIR>          .
27/07/2016  13:37    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  325 054 046 208 bytes free

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git checkout -b A
Switched to a new branch 'A'

在分支A中,添加其他代码(更新公共文件夹)

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> mkdir a
> touch a\a.txt

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> mkdir c
> echo e>c\c.txt
> touch c\f.txt

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git add .

> git st
On branch A
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   a/a.txt
        new file:   c/c.txt
        new file:   c/f.txt


vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git commit -m "a head"
[A 3168bae] a head
 3 files changed, 1 insertion(+)
 create mode 100644 a/a.txt
 create mode 100644 c/c.txt
 create mode 100644 c/f.txt

切换回master,并将A合并到master

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git checkout master
Switched to branch 'master'

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> glab
* 3168bae - (A) a head (80 seconds ago) <VonC>
| * 7c1a82d - (HEAD -> master) m head (3 minutes ago) <VonC>
|/
* ba36e82 - empty (4 minutes ago) <VonC>

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> dir
 Volume in drive C is OSDisk
 Volume Serial Number is xxxx-yyyy

 Directory of C:\Users\vonc\prog\git\tests\merge

27/07/2016  13:41    <DIR>          .
27/07/2016  13:41    <DIR>          ..
27/07/2016  13:41    <DIR>          c
27/07/2016  13:41    <DIR>          m
               0 File(s)              0 bytes
               4 Dir(s)  325 051 240 448 bytes free

合并确实会产生并发更改的冲突

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git merge A
Auto-merging c/c.txt
CONFLICT (add/add): Merge conflict in c/c.txt
Automatic merge failed; fix conflicts and then commit the result.

vonc@voncm C:\Users\vonc\prog\git\tests\merge
> git st
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

        new file:   a/a.txt
        new file:   c/f.txt

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both added:      c/c.txt

未删除任何文件夹:

vonc@WSV00320606 C:\Users\vonc\prog\git\tests\merge
> dir
 Volume in drive C is OSDisk
 Volume Serial Number is xxxx-yyyy

 Directory of C:\Users\vonc\prog\git\tests\merge

27/07/2016  13:41    <DIR>          .
27/07/2016  13:41    <DIR>          ..
27/07/2016  13:41    <DIR>          a
27/07/2016  13:41    <DIR>          c
27/07/2016  13:41    <DIR>          m
               0 File(s)              0 bytes
               5 Dir(s)  325 005 889 536 bytes free
相关问题