Solve Git conflict problems and merge code disappearance problems in Idea [git common tips]

Solve Git conflict problems and merge code disappearance problems in Idea

Full series of Git commands

1 Small problems and skills of using git in Idea

  • We can pull code directly from platforms such as GitLab or GitHub through Idea
File - New - Project from Version Control

Insert image description here

输入对应项目的URL即可

Insert image description here

  • If you can’t pull it out with the above tips, try checking the option in the picture below.
    Insert image description here

2 Idea to resolve conflicts

2.1 Demo conflicts (GitLab)

①First create your own warehouse in GitLab or any code hosting platform

git clone 仓库的URL

Clone the repository with the above command

②In your own project, create a class arbitrarily
Insert image description here

③ Submit it to the local warehouse and then push it to the remote warehouse

Insert image description here
④Then modify the code arbitrarily in the remote library

I added a sentence here

Insert image description here

⑤Modify the local code and try to push it to the remote warehouse

At this time, version conflicts will occur because our local code is not the latest version of the remote library.

Insert image description here

⑥ Conflict
Insert image description here

2.2 Resolve Git version conflicts

①Choose rebase at this time (must choose rebase, as required by corporate standards, direct merge may cause a series of problems)

Because I used rebase as the default option before, I skipped the selection here.

Insert image description here

② Operate according to your own requirements
Insert image description here

Accept Yours 就是直接选取本地的代码,覆盖掉远程仓库的
Accept Theirs是直接选取远程仓库的,覆盖掉本地的
Merge  自己手动进行选择,修改

③Under normal circumstances, we will manually Mege
Insert image description here

The left part here is the code of our local warehouse, the right part is the code of the remote warehouse, and the result in the middle is what we modified.

As a result, the AcceptLeft and Accept Right in the lower left corner are actually equivalent to the previous Accept Yours and Accept.

Theirs, Apply in the lower right corner confirms the merge, and Abort cancels the merge.

After we modify the code we want to merge in the result, just click Apply. At this point, the conflict is resolved.

Detailed documentation:
https://www.cnblogs.com/newAndHui/p/10851807.html

2.3 rebase failed

If our rebase fails:
Insert image description here
Solution:

$ git add .(只要有修改都需要git add . 或者git add 具体的文件)
$ git rebase --continue
Applying: 【HCF】*******************
$ git push origin ******************************
git rebase --continue 就相当于 git commit 

3 Git error

When Git conflicts, I accidentally clicked the merge operation, causing the codes in the local and remote warehouses to disappear out of thin air.
Here, my src folder was deleted.

3.1 When Git operates merge, the code disappears

① Use git log to find the commit that modified the specified file
. The current project file has been deleted, but according to the code structure of the project, it can be inferred that the src folder originally existed

Try to detect the processing of this file in all historical records, the command used is as follows:

git log --stat --full-history --simplify-merges -- src

Insert image description here
The above command will display the commits involved in the changes to the folder. From the output, we can see that in the commit ending with 857, we accidentally deleted 11 lines of code.

② By switching to this version

git checkout 982918cd36668686c2644decbf0a0e4988283857

Insert image description here
Then go back to the project, you can find that our previous code has been restored

Why is there such a situation?
Analysis: https://cloud.tencent.com/developer/article/2033888

3.2 Git pull --rebase error report

git pull --rebase报错

error: cannot pull with rebase: Your index contains uncommitted changes.
error: please commit or stash them.

This is because we have local changes that have not been submitted.
If we need to submit, just git add, git commit; submit.
If we do not need to submit the changes, just git stash, temporary
storage. Solution steps:

Follow the prompts to perform the following operations

  • git stash
  • git pull --rebase
  • git stash pop

or:

  • git add *
  • git commit
  • git pull -rebase

Then we can submit
Insert image description here

3.3 git clone or commit error: errno 10054 or errno 10054

Error log:

  1. fatal: unable to access ‘https://github.com/AliyunContainerService/k8s-for-docker-desktop.git/’: OpenSSL SSL_read: Connection was reset, errno 10054

or

  1. fatal: unable to access ‘https://github.com/xxx/autowrite.git/’:
    Failed to connect to github.com port 443: Timed out

reason:

  • Because when git pulls or submits a project, there will be git's http and https proxies in the middle, but our local environment itself has an SSL protocol, so just cancel the git's https proxy. If that doesn't work, cancel the http proxy.
  • Another reason is that the current proxy network speed is too slow, so it will succeed occasionally and fail occasionally.

Solution:

  1. , Execute the following command to cancel the https proxy of git itself, and use your own local proxy. If not, git is still used by default;
//取消http代理
git config --global --unset http.proxy
//取消https代理 
git config --global --unset https.proxy
//重新执行git clone 或git commit
  1. Use the Internet scientifically to solve network problems

4 Expansion:

4.1 The difference between git clone and git pull

git clone does not have a repository locally, and downloads the entire remote repository

git pull is a local repository, download the new commit data (if any) in the remote warehouse, and merge with the local code

4.2 The difference between merge and rebase

Detailed explanation: https://zhuanlan.zhihu.com/p/75499871
https://segmentfault.com/a/1190000038547167

rebase与merge实现,版本提交数风格会呈现不同的效果

  • Rebase will put the commit of your current branch at the end of the public branch, so it is called rebase. It's as if you pulled this branch from the public branch again.
  • For example: If you pull a prod branch from master, and then you submit a few commits, and someone happens to merge the things he developed into master, then master will have several more commits than when you pulled the branch. , If you rebase master at this time, your current commits will be placed behind that person's commits.
  • The specific effects are as follows:
master 初始状态为123.在此基础上新建一个prod分支
master 提交了45.  prod分支提交了67.
此时分支状态:master->1-2-3-4-5
            prod ->1-2-3-6-7
在prod上使用rebase master,prod分支状态变成1-2-3-4-5-6-7            
如果merge master,prod分支状态变成1-2-3-6-7-8
        这里的8提交的是4-5合起来的提交
merge之后想回退到你分支上的某个提交就会很麻烦!

4.3 Submit code locally to the remote end

Usually, during development, we will commit before committing, then pull the remote warehouse to ensure that the current version is the latest version, and then push to the remote warehouse.

  • Select merge operation
    Insert image description here

  • Select rebase operation

Insert image description here

4.4 Part of the temporary code is not submitted to the remote warehouse (git stash)

也可以参考4.9的方法:change list

During development, we will inevitably encounter colleagues who need us to merge code, but at this time we have also written some locally, and due to some reasons (no testing completed), we do not want to submit these codes to the remote library .

So what should we do at this time? git stash works
① Right-click the project name, select git, select stash changes (store)
Insert image description here
② Then git pull, pull the latest code from the remote warehouse for merge (merge)
③ After getting the latest code, unstash, get the previous code The code we write locally
Insert image description here

4.5 git pull origin master --allow-unrelated-histories

POST git-upload-pack (327 bytes) From https://gitee.com/Zifasdfa/graduation-music * branch master -> FETCH_HEAD = [up to date] master -> origin/master refusing to merge unrelated histories

The main reason for this problem is that the local warehouse and the remote warehouse are actually two independent warehouses. If I had directly cloned the local repository of the remote github repository locally, this problem would not have occurred.

git pull origin master --allow-unrelated-histories

will solve the problem

4.6 git removes the files that have been added

git removes files that have been added

  1. Does not delete the physical file, just clears the file from cache
git rm --cached "文件路径"

[Others] What is the difference between git rm --cache and git reset HEAD?
If you want to delete a file, it is best to use git rm file_name instead of rm file_name directly in the workspace.
If a file has been added to the staging area and has not been committed yet, if you no longer want the file, there are two methods:
1. Clear the staging area with the contents of the repository, git reset HEAD but use it with caution
. 2. Only use To delete specific files from the staging area, git rm --cached xxx

  1. Not only will the file be removed from the cache, but the physical file will also be deleted (not recycled to the trash)
git rm --f "文件路径"

Insert image description here
Insert image description here

  • For friends who use IDEA:

You can choose to put it on hold temporarily

When committing, select the file you want to shelve, right-click and select Shelve Changes
Insert image description here

You can choose stash changes

Select the project, right-click to select git, and find stash changes
Insert image description here

  • Add directly to the ignore file
右击项目 - git - .git/info/exclude

Insert image description here
3. It can also be achieved by rolling back

As shown in the picture: I accidentally added README.md to the git local repository

Insert image description here
通过rollback解决:
Insert image description here

Select the file you want to rollback and click rollback

Insert image description here

You can see that README.md has turned red, indicating that it has not been added to the local warehouse.

Insert image description here

4.7 Idea rolls back the local code to the specified version and updates it to the remote at the same time

You may encounter such a problem during development, that is, you have submitted the wrong code to the remote warehouse and want to roll back the remote and local code at the same time.

有两种方法:1Revert操作  2、利用IDEAReset Head指针
  • The Revert operation of method 1 will be treated as a new submission record and appended to the submission log, thus retaining the original submission record. (recommend)
  • The Reset Head pointer in method 2 will discard the original submission record and force the Head pointer to point to the specified version.

After modifying the content based on version 1 and submitting it to the local and remote warehouses, I found that the submitted content was not what I wanted, or it was completely wrong, and I needed to roll back to version 1.

① Currently, the local and remote branches are at the location of the second submission.
Insert image description here

  • Right-click on the historical version you want to roll back and select "Revert" (see the picture below)
    Insert image description here
    ② At this time, the conflict needs to be resolved

A conflict dialog box will pop up. Double-click the conflict file to resolve the conflict. (See below)

Insert image description here

注意:If the rollback fails, it may be that you have other local modifications that have not been submitted, which can be temporarily stored through stash.

your local changes would be overwritten by revert.
hint: commit your changes or stash them to proceed.
revert failed

stash operation:
Insert image description here

③ After the conflict is resolved, the local code returns to the previous correct code
Insert image description here
④ Commit locally again, you can find that a rollback record has been added to the log, and push to the remote at the same time, you can find that both the remote and local are synchronized [ ] originRemote
Insert image description here
:
Insert image description here

The advantage of this kind of rollback is that if you regret the "rollback" operation, you can also roll back to the version before the rollback. Because history also keeps commit records.

4.8 Usage of cherry pick in git

cherry pick: best choice

For multi-branch code, it is common to move code from one branch to another.
There are two situations at this time:

  1. All code changes that require another branch ( git merge 合并)
  2. Some code changes, some commits ( cherry pick )

For example, the code repository has two branches: master and feature.

    a - b - c - d   Master
         \
           e - f - g Feature

Now apply commit f to the master branch.

# 切换到 master 分支
$ git checkout master

# Cherry pick 操作
$ git cherry-pick f

After the above operations are completed, the code base will look like the following.

    a - b - c - d - f   Master
         \
           e - f - g Feature

As you can see from the above, a commit f is added to the end of the master branch.

  • The parameters of the git cherry-pick command are not necessarily the hash value of the commit. The branch name is also acceptable, indicating that the latest commit of the branch is transferred.
$ git cherry-pick feature

The above code means transferring the latest commit of the feature branch to the current branch.

4.9 Create changelist locally (submit specified file)

Sometimes when we pull multiple projects, we often encounter this problem:
修改了多个项目,但是只想提交一个项目中的代码, then you can create a changelist

Insert image description here

For example, here we only want to submit DynamicLinkDTO

Then we can locally create achangelist

  1. Right mouse button, directlynew changelist
    Insert image description here
  2. Move the code we want to submit to the one we just created changelist, such as: default_change
    Insert image description here
  3. Select our default_change, right-click, then select git and push
    Insert image description here

4.10 There is a git warehouse locally and a git warehouse on gitee. How to merge it?

git pull origin master --allow-unrelated-histories

or

# 添加远程地址
git remote add origin https://gitee.com/Zifasdfa/ziyi-app.git
git push -u origin "master"
  • git pull origin master --allow-unrelated-histories: This command is used to merge the master branch of the remote warehouse into the current branch of the local warehouse.
    • The –allow-unrelated-histories parameter is used to allow merging two unrelated history branches. This command is suitable for merging operations between two warehouses.
  • git push -u origin "master": This command is used to push the master branch of the local warehouse to the remote warehouse.
    • The -u parameter is used to set the upstream branch so that git push can be used directly the next time you push. This command is suitable for push operations to a single warehouse.

4.11 Git commit is too slow

Find the git settings and close itanalyze code

Insert image description here

4.12 git smart checkout and force checkout

When IDEA modifies content on one branch without committing it, and then switches to another branch, conflicts may occur.

At this time, IDEA will pop up a prompt asking you to choose Smart Checkout or Force Checkout:

  • If you want to keep your modifications on the original branch, then choose Smart Checkout,

  • Force Checkout will not retain your modifications. If you switch to another branch, the contents will disappear, and if you switch back to the original branch, you will not be able to retrieve them. It is a waste of time.

Principle: Select Smart Checkout, IDEA will first execute the stash command to store these uncommitted modifications, and then checkout to branch B. After switching to branch B, it will unstash these modifications, so these local modifications in branch A will be brought to branch B. .

4.13 Email inconsistency problem

Sometimes when we are developing, we will switch back and forth between the email on our github or gitee and the one on the company's gitlab.

If we encounter the following problems when modifying the company code and pushing, then:

remote: Push exception: This submission a84887a5d29a8643fd6ef45904b47f2067dbcc35 detects that the email address set by your local client ([email protected]) is not the email address you set in GitLab ([email protected]). Please ensure that your local and remote The email address is the same!

At this point we should check whether our local git mailbox configuration is consistent with the one set on gitlab:

# 查看git全局邮箱配置
git config --global user.email

# 修改git全局邮箱配置
git config --gloabl user.email yyyss@xxx.com.cn

# 修改私有配置(某个git文件的)
git config user.email ziyi@163.com

Then we push again and find that if an error is still reported, it may be because our commit still uses the previous email address. We did not generate a new commit, so the new email address was not successfully used.修改注释或者增加空行,然后commit即可解决

4.14 Updates were rejected because the tip of your current branch is behind

This error usually occurs when the commit history of your local branch and the remote branch are inconsistent. The solution is to pull the latest commit from the remote branch before pushing to your local branch.

# 1. 切到本地分支
git checkout main
# 2. 拉取远程分支
git pull origin main
# 如果有冲突产生,需要解决冲突后再继续

Reference articles:
https://blog.csdn.net/Torey_Li/article/details/87442355
https://blog.csdn.net/woshi1226a/article/details/86664159
https://blog.csdn.net/Deronn/article /details/106574498
https://blog.csdn.net/good_good_xiu/article/details/118567249

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/126926514