Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Time:2021-11-30

In the previous article, I used some commands that are most commonly used, but they are basically under normal conditions. Next, I will talk about commands under abnormal conditions.

First of all, our most direct contact in the local area is the remote warehouse. As for the remote warehouse of others (the warehouse where we modify files together, hereinafter referred to as the remote warehouse), it is mainly through the remote warehouse.

So how can we ensure that there are other people’s modifications in our warehouse

pull

You can see that there is an others.txt file in the remote warehouse, but there is no local file. In order to synchronize the local and remote warehouses, you need to use pull

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

It corresponds to push. Generally, it will be followed by the address of the remote warehouse (the previous article briefly described how to set the addresses of different remote warehouses)

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Mr time conficts

It can be found that new files have been added locally. Note that the files we modify are also in the remote warehouse. If others think our files are not written well enough and then modify them.

For example, in the HelloWorld submitted last time, I thought it was too simple. I only printed it once, then added a for loop to print it 100 times, and added the serial number after each print.

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

At the same time, we also feel that the code is not good enough. We need to repeat it 1000 times, and want to have another classic line after output, so we changed the local code.

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Then, I happily pushed the code and mentioned the merge request. Some platforms are also called pull request. At this time, I will find that

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

There is an alarmThis pull request cannot be merged automatically. You should merge it manuallyIn fact, you and others modify the unified line in the file at the same time, which conflicts. After that, we have to solve the conflict. The conflict resolution interface of different platforms should be different, but the essence is GIT.

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

At this time, it is necessary to discuss (tear) theory (b) among programmers. Whose is the subject? Here is the assumption. Keep the followingi + 1But it takes 1000 cycles. Therefore, it needs to be modified to

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

In fact, it doesn’t matter if you delete the code of this file in principle, because git has told you that there is a conflict in this file. As for how to choose and what to keep, GIT thinks we will deal with it. When all this is done, it will be consistent with the previous Mr, and it can be combined after passing the audit

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

However, it is always a little troublesome to resolve the conflict during MR, so the common practice in general work is to pull the latest code after commit

Conflicts during pull

You can see that the remote warehouse is like this at this time

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Then the remote file was modified to simulate other colleagues to join the new code

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Similarly, without knowing, we want to add a getstr method and call it in the main function

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

After editing, add and commit normally,But don’t push, first pull the code of the remote warehouse

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Finally, we can see such a message, that is, let’s solve the conflict

Auto-merging hello.java
CONFLICT (content): Merge conflict in hello.java
Automatic merge failed; fix conflicts and then commit the result.

How to solve this problem is to edit the conflicting file. Use VIM to see what the conflicting file looks like at this time

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Just put<<<<<<<=======and>>>>>>>Also, delete the characters we don’t want, such as head, and then keep the required code. Finally, add commit push Mr normally.

At this time, some new friends will ask, what if someone else adds code to the warehouse during the change

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

I’m kidding, but 99.99% of this situation will only exist in theory and will not appear in real work. If it happens to happen, solve the conflict.

stash

  • We modified several files, such as the 26 files ABCDEF… Z
  • The supervisor came and said that there was a module that was very urgent and needed to close the four files ABCD first
  • Someone in the remote warehouse has combined the two LX files
  • Then remember to form good habits,After commit, pull the remote code base

Summary is when you do not want to submit all modified documents and there are conflicts in the modified documents

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

This means that some local files may be overwritten during closing. There are two options at this time

  • Commit and resolve the conflict
  • Stage and then pull down. If there is a conflict in the stage pop, resolve the conflict

Stash is to put the local modifications into a git “stack” first. The local to remote states are unmodified, and then they can be pulled normally

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

usegit stash save [save message]After temporarily storing the changes, you can successfully pull them down. Next, look at the status of GIT

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Since it can be saved, it can certainly be taken out. As mentioned above, if the file you save is the same as the file modified by the remote warehouse, there will be conflicts when pop comes out

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

The solution is also to deal with conflicts, which will not be repeated here

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

reset

In the first article, it was said that according to the prompt in status, both add and commit can be retracted, and there must be corresponding operations after push.

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

You can see that at least two files are pushed this time. If you want to withdraw the push, you must first know what is pushed and what is pushed

Check through git log, but git log has many operations (beautification). The most commonly used are the following

  • Nothing is added, and the output is full and lengthy

    Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

  • Output the first few,git log -p

    Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

  • Single line output,git log --oneline

    Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

    Of course, it can be combined,git log --oneline -2Naturally, the first two records are output in a single line.

It is not difficult to see that each submission subject is a string of numbers (version number) and a message at the time of commit. When we want to go back to other versions, we canGit reset [version number]Just. I know there are many operations about reset on the Internet. How many times before fallback, but it’s better to check the version number and fallback. If you have to use it, just check it.

Join me to go back to the previous version, just do it

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

You can see that the two files originally pushed have now become to be submitted.

Creation is not easy. If it helps you, you are welcome to like, collect and share!

Below is the personal official account. If you are interested, you can pay attention to it. Maybe your treasure official account!

Gan, when there is a conflict, my colleagues have to say that he is right. What should I do

Recommended Today

Game case | application evolution and practice of service mesh in happy games

author Chen Zhiwei, Tencent level 12 background expert engineer, is now responsible for the public background technology research and development and team management of happy game studio. Rich experience in micro service distributed architecture and game background operation and maintenance research and development. preface The background of happy game studio is a distributed micro service […]