sts解决解决git冲突_如何解决Git冲突

sts解决解决git冲突

Note: This the sixth video in the Git for beginners series. Watch the first video here.

注意:这是Git初学者系列的第六部视频。 在这里观看第一个视频

Let’s say a friend of made a change to your repository and pushed the changes to the Git remote. At the same time, you also made a change to the same line of code.

假设有一位朋友对您的存储库进行了更改,然后将更改推送到了Git远程服务器。 同时,您还更改了同一行代码。

When you pull their changes into your local repository, you’ll notice that there is a conflict.

当将它们的更改放入本地存储库时,您会注意到存在冲突。

This happens because Git no idea whether your friend’s version is the updated version or your version is the updated version.

发生这种情况是因为Git不知道您朋友的版本是更新版本还是您的版本是更新版本。

This is what we call a Git conflict.

这就是我们所说的Git冲突。

You’ll learn how to resolve a Git conflict today.

您将立即学习如何解决Git冲突。

First, let’s produce a Git conflict so you see what happens.

首先,让我们产生一个Git冲突,以便您了解会发生什么。

产生冲突 (Producing a conflict)

To produce a Git conflict, we need two sets of code. For the first set, we need someone to push code into the remote.

要产生Git冲突,我们需要两组代码。 对于第一组,我们需要有人将代码推送到遥控器中。

In our case, we edit the files on GitHub to simulate a change.

在我们的案例中,我们在GitHub上编辑文件以模拟更改。

Let’s say we change the text in README.md from “Hello world, this is my first Github repo” to “Hello world, this is my second Github repo”.

假设我们将README.md的文本从“ Hello world,这是我的第一个Github存储库”更改为“ Hello world,这是我的第二个Github存储库”。

We’re also going make a commit message that says “Changed first to second” to see the effects in our Git History later.

我们还将发出一条“先更改为第二”的提交消息,以便稍后在Git历史记录中查看其效果。

For the second set of code, you can change the same README.md file on your local repository. Instead of “second Github repository”, we’re going to say “third Github repository”.

对于第二组代码,您可以在本地存储库中更改相同的README.md文件。 代替“第二个Github存储库”,我们将说“第三个Github存储库”。

Hello world! This is my third Github repo!

We’re going to commit this file and set the commit message to “Change first to third.”

我们将提交此文件,并将提交消息设置为“从第一到第三更改”。

You can check for an update in your Git Client with the “Fetch” button. Once the fetch is completed, you can see that origin/master is on a different fork compared to master.

您可以使用“获取”按钮在Git客户端中检查更新。 提取完成后,您可以看到与master相比, origin/master位于不同的fork上。

This happens because there are changes on the Git remote and on our local repository at the same time.

发生这种情况是因为同时在Git远程我们的本地存储库上进行了更改。

If you look at the branch on the left, you can see that the master branch says one down one up. This tells us there is one commit in the Git remote that is ahead of our master. At the same time, our master branch has one commit ahead of the remote.

如果您查看左侧的分支,可以看到主分支说的是一个向下一个向上。 这告诉我们没有在远程Git是领先于我们的大师之一提交。 同时,我们的master分支在远程对象之前有一个提交。

We need to pull our changes onto our local branch to consolidate the changes. When you pull the changes, you’ll see an error message.

我们需要将更改拉到我们的本地分支机构以合并更改。 进行更改时,您会看到一条错误消息。

This error message may be slightly different depending on the Git client you’re using. In Fork, it says “Merging branch origin/master into master. Fix 1 conflict and then continue”.

根据您使用的Git客户端,此错误消息可能会略有不同。 在Fork中,它说“将分支origin/master合并为master 。 解决1个冲突,然后继续”。

What this means is you need to fix the conflict before you continue.

这意味着您需要先解决冲突,然后再继续。

To see the conflict, you can go back to the changes section. Here, you’ll see the files that contain conflicts. In this case, it is the README.md file.

要查看冲突 ,您可以返回更改部分。 在这里,您将看到包含冲突的文件。 在这种情况下,它是README.md文件。

First, let’s talk about why you need to merge.

首先,让我们谈谈为什么需要合并。

为什么要合并? (Why merge?)

When you pull changes from the remote branch to the local branch, the change from the remote branch is merged into the local branch.

当您将更改从远程分支拉到本地分支时,来自远程分支的更改将合并到本地分支中。

Git helps us do the merge automatically if it knows what is changed first, and what is changed later, and there are no ambiguities.

如果Git知道先更改了什么,后来更改了什么并且没有歧义,它会帮助我们自动进行合并。

But, when there is a conflict, Git doesn’t know which version is correct — so you have to merge the code yourself.

但是,当发生冲突时,Git不知道哪个版本是正确的-因此您必须自己合并代码。

解决冲突 (Resolving conflicts)

The easiest way to resolve a conflict is to change the file on your computer. If you open README.md now, you’ll see lines that say this:

解决冲突的最简单方法是更改​​计算机上的文件。 如果现在打开README.md ,您将看到显示以下内容的行:

<<<<<< HEAD Hello world! This is my third Github repo!====== Hello world! This is my second Github repo! >>>&gt;>> snt2h1s3n4tnthd9au8d3324

The code between <<<;<<<; HEAD and ======= is the code in our local repository (our code).

之间的代码<<< ; < << ; HEAD和=======是我们本地存储库中的代码(我们的代码)。

The code between ====== and >&gt;>>>> is the code from the remote repository (their code).

之间的代码======>&g吨; >>>>是从远程存储库(其代码)的代码。

These two lines of code are conflicting. We need to choose between the “second Github repo” or the “third Github repo”.

这两行代码是冲突的。 我们需要在“第二个Github存储库”或“第三个Github存储库”之间进行选择。

To fix the conflict, you choose the correct line of code. Then you delete everything else.

要解决冲突,请选择正确的代码行。 然后删除其他所有内容。

In this case, let’s say “third” is the correct version. What you’ll do is delete everything else that’s incorrect.

在这种情况下,假设“第三”是正确的版本。 您要做的就是删除所有其他不正确的内容。

Hello world! This is my third Github repo!

提交合并 (Committing the merge)

When you head back to Fork, you’ll see that the changes are updated. In our case, the change is the one on our local, so we don’t see any files that need to be staged.

当您回到Fork时,您会看到更改已更新。 在我们的情况下,更改是在本地进行的,因此我们看不到任何需要暂存的文件。

If the change is different, you will need to stage the files.

如果更改不同,则需要暂存文件。

After staging, you need to commit the merge.

暂存后,您需要提交合并。

If you look at the commit message area, you’ll see that Fork has filled in a commit message for you automatically. You can use the commit message directly.

如果查看提交消息区域,则会看到Fork自动为您填写了一条提交消息。 您可以直接使用提交消息。

Click on commit to commit the changes.

单击提交以提交更改。

When you commit the changes, you’ll see that the master branch in the sidebar says 2 up. This means our local branch is two commits ahead of the remote.

提交更改后,您会看到侧边栏中的master分支显示2 up。 这意味着我们的本地分支是远程的两个提交。

If you look at the Git history, You can see that the remote master branch has a link back to the local master branch. This shows a merge.

如果查看Git历史记录,则可以看到远程master分支具有指向本地master分支的链接。 这显示了合并。

What you need to do next is to push the changes up to the Git remote.

接下来,您需要将更改推送到Git远程服务器。

And this is how you resolve a Git conflict.

这是你如何解决一个Git冲突。

防止发生冲突 (Preventing conflicts from happening)

Conflicts happen when two or more people work on the same file at the same time.

当两个或两个以上的人同时处理同一文件时,就会发生冲突。

There are two ways to prevent conflicts.

有两种防止冲突的方法。

The first way is to reduce the size of your commits. That means you make a commit for every little thing you do. Commits are free, so just make more commits.

第一种方法是减小提交的大小。 这意味着您要为所做的每件事做一个承诺。 提交是免费的,因此只需进行更多提交即可。

This helps because it is easy to resolve a small conflict like the one we just resolved. If you encounter a conflict that is hundreds of lines long, it’ll be hard to resolve.

这很有帮助,因为很容易解决我们刚刚解决的小冲突。 如果遇到长达数百行的冲突,将很难解决。

The second way involves branches. Here, different people work on the code in different branches. They don’t interact with each other. They only merge the code into the main branch when they’re ready.

第二种方法涉及分支。 在这里,不同的人在不同分支中的代码上工作。 他们不会互相影响。 它们仅在准备好后才将代码合并到主分支中。

Branches are slightly more advanced. We’ll talk about branches and how to use branches in the next video.

分支稍微更高级。 在下一个视频中,我们将讨论分支以及如何使用分支。

Thanks for reading. Did this article help you in any way? If you did, I hope you consider sharing it. You might help someone out. Thank you!

谢谢阅读。 本文对您有任何帮助吗? 如果这样做, 希望您考虑共享它 。 您可能会帮助某人。 谢谢!

This article was originally posted at my blog. Sign up for my newsletter if you want more articles to help you become a better frontend developer.

本文最初发布在我的博客上 如果您想获得更多文章来帮助您成为更好的前端开发人员,请注册我的时事通讯

翻译自: https://www.freecodecamp.org/news/resolving-git-conflicts-62bd357ed5f/

sts解决解决git冲突

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值