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
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
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)
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.
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.
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
There is an alarm
This 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.
At this time, it is necessary to discuss (tear) theory (b) among programmers. Whose is the subject? Here is the assumption. Keep the following
i + 1But it takes 1000 cycles. Therefore, it needs to be modified to
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
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
Then the remote file was modified to simulate other colleagues to join the new code
Similarly, without knowing, we want to add a getstr method and call it in the main function
After editing, add and commit normally,But don’t push, first pull the code of the remote warehouse
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
>>>>>>>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
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.
- 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
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
git stash save [save message]After temporarily storing the changes, you can successfully pull them down. Next, look at the status of GIT
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
The solution is also to deal with conflicts, which will not be repeated here
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.
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
Output the first few,
git log -p
Single line output,
git log --oneline
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 can
Git 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
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!