简体   繁体   English

Git:如何与远程仓库合并?

[英]Git: how do you merge with remote repo?

Please help me understand how git works. 请帮助我了解git的工作原理。

I clone my remote repository on two different machines. 我将远程存储库克隆到两台不同的计算机上。

I edit the same file on both machines. 我在两台计算机上编辑相同的文件。

I successfully commit and push the update from the first machine to the remote repository. 我已成功提交并将更新从第一台计算机推送到远程存储库。 I then try to push the update on the second machine, but get an error: 然后,我尝试在第二台计算机上推送更新,但出现错误:

! [rejected]        master -> master (non-fast-forward)

I understand why I received the error. 我了解为什么收到错误消息。 How can I merge my changes into the remote repo? 如何将更改合并到远程存储库中? Do I need to pull the remote repo first? 我需要先拉远程回购吗?

Yes, after remote repository rejected push as non fast-forward, you need to pull from remote repository (this would do a merge, which means that you might need to resolve merge conflict -- don't forget to commit merge conflict resolution in that case), and then push again. 是的,在远程存储库将推送拒绝为非快进之后,您需要从远程存储库中拉取(这将进行合并,这意味着您可能需要解决合并冲突-不要忘记在其中提交合并冲突解决方案大小写),然后再次按下。


  1. After comitting changes on machines A and B you have the following situation: 在机器A和B上进行更改之后,您将遇到以下情况:

    • ''remote repository'' ``远程存储库''

      \n*---*---*---C * - -* - -* - -C\n

    • ''machine A'' 机器A

      \n*---*---*---C---A * --- * --- * --- C --- A\n

    • ''machine B'' 机器B

      \n*---*---*---C---B * --- * --- * --- C --- B\n

  2. After pushing from machine A to remote repository you have: 从机器A推送到远程存储库后,您将具有:

    • ''remote repository'' ``远程存储库''

      \n*---*---*---C---A * --- * --- * --- C --- A\n

    • ''machine A'' 机器A

      \n*---*---*---C---A * --- * --- * --- C --- A\n

    • ''machine B'' 机器B

      \n*---*---*---C---B * --- * --- * --- C --- B\n
      Push from machine B would correctly refuse to "overwrite" commit A with commit B. 从计算机B进行的推送将正确地拒绝用提交B“覆盖”提交A。

  3. After pulling from remote repository on machine B you have: 从机器B上的远程存储库中提取后,您将具有:

    • ''remote repository'' ``远程存储库''

      \n*---*---*---C * - -* - -* - -C\n

    • ''machine A'' 机器A

      \n*---*---*---C---A * --- * --- * --- C --- A\n

    • ''machine B'' 机器B

      \n*---*---*---C---B----M * --- * --- * --- C --- B ---- M\n            \\ / \\ /\n             \\--A--/ \\ - 一种 - /\n
      Just in case this ASCII-art gets mangled: there is form from commit C, and two branches are merged as commit M. 以防万一这种ASCII艺术被弄乱了:提交C提供了一种形式,并且两个分支合并为提交M。

    Now the push would be fast-forward 现在推动将是快速的

  4. After pushing from machine B to remote repository 从机器B推送到远程存储库后

    • ''remote repository'' ``远程存储库''

      \n*---*---*---C---B----M * --- * --- * --- C --- B ---- M\n            \\ / \\ /\n             \\--A--/ \\ - 一种 - /\n

    • ''machine A'' 机器A

      \n*---*---*---C---A * --- * --- * --- C --- A\n

    • ''machine B'' 机器B

      \n*---*---*---C---B----M * --- * --- * --- C --- B ---- M\n            \\ / \\ /\n             \\--A--/ \\ - 一种 - /\n

  5. Now on machine A you need to pull from repository before starting any new work, to be up to date. 现在,在计算机A上,您需要在开始任何新工作之前从存储库中提取最新信息。 Pull would fast-forward, which means that there wouldn't be created any new merge commit. Pull将快速前进,这意味着将不会创建任何新的合并提交。 Now all three machines have the same state of repository 现在,所有三台计算机都具有相同的存储库状态

    • ''remote repository'' ``远程存储库''
      ''machine A'' 机器A
      ''machine B'' 机器B
      \n*---*---*---C---B----M * --- * --- * --- C --- B ---- M\n            \\ / \\ /\n             \\--A--/ \\ - 一种 - /\n

HTH 高温超导

Yes. 是。

Merging only happens on a pull; 合并只发生在拉上; a push will only perform a fast-forward (unless you force it, but that loses work). 推送只会执行快进(除非您强行执行,否则会丢失工作)。 Once you've pulled and successfully merged, the push will be a fast-forward. 合并并成功合并后,推送将是快速的。

git-push has some nice illustrations. git-push有一些很好的插图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM