Author: joymufeng source: https://my.oschina.net/joymuf…

Unexpected things happen to you in your daily use of Git, and if you don’t handle them properly, it can give you the illusion of missing code. In this article, we will look at some of the scenarios in the daily development of Idea &Git, and give you some insight into common mistakes and their causes, so that you don’t have to worry about code conflicts or missing problems.

To simplify matters, this article assumes that all team members are developing on the same branch.


In this paper,
The update operationRefers to the
IDEAClick menu in
VCS
Update Project....

1. Common workflow

Usually when you get to work in the morning and turn on your computer, you first perform the Update operation (click on the IDEA menu VCS-Update Project…). “And started coding happily. After coding is complete, the following operations are usually performed:

  • The update operation
  • Create this submission
  • Push remote branch

1.1 Update Operation

To ensure that Git has a clean commit history, you need to perform an Update before committing, by clicking on the VCS-Update Project menu in the IDEA sequence. , or press Ctrl+T to bring up the following window:

Select Update Type on the left side of the window:

  • Merge: Merge operation is performed when updating. It’s equivalent to executinggit fetch && git mergeorgit pull --no-rebase.
  • Rebase: Executes when updatingrebaseOperation. It’s equivalent to executinggit fetch && git rebaseorgit pull --rebase.
  • Branch DefaultIn:.git/configFile to specify the type of update for the different branches.

On the right side of the window, select the cleaning method of Working Directory before updating:

  • Using StashUse:git stashStore local changes.
  • Using Shelve: Use IDEA built-inShelveFunction to store local modifications.

Usually select Merge and Using Stash. Once you click OK, IDEA is executed as follows:

  • Step 1: Usegit stashStore local changes
  • Step 2: Executegit fetch && git mergePull the remote branch and merge
  • Step 3: Executegit stash popRestore storage

Some students may prefer to create a local commit first and then perform the update operation, which will cause Git to automatically generate a merge commit, resulting in a less concise commit history.

1.2 Create this submission

When the update is complete, click on the VCS-Commit menu in IDEA… Create this submission.

1.3 Push remote branches

Then click vcs-git-push… Push to a remote branch.

2. Analysis of common problems

Among the above three steps, step 2 and step 3 have the highest risk of accidents. The two most common accidents are conflicts and file occupancy, which are discussed below respectively.

2.1 Merge remote branch conflicts

If your local branch has already created a commit before performing the update and has not yet pushed to the remote branch, then there is likely to be a conflict when you perform the Git merge at step 2.

At this point, close the above conflict window, and the Version Control tool window displays the following contents:

A Merging master is displayed in the bottom right corner of the window, indicating that the local branch master is currently in a Merging state. Click the Resolve button in the red box on the left to bring up the Conflict Handling window again. Once a conflict is manually resolved by the IDEA-based graphical interface, IDEA automatically adds the file to the staging area (the staging area indicates that the conflict is resolved), and a final commit completes the conflict.

2.2 Recovery of storage conflicts

When you perform the Git Stash Pop restore the store at step 3 of the update operation, the stored contents may conflict with the contents of the newly updated store.

A Merging collision occurs when you rejoin the cache. For one thing, the name of the branch in the lower right corner does not contain the word “Merging.” An extra window pops up in the lower right corner to indicate that the rejoin cache failed and that all changes are in the stash list and have not been lost. To view the stash list, click on the menu VCS-Git-Unstash Changes… :

Select the entry at the top of the list and click Apply Stash, and the previous changes will go back to the working directory. Returning to the conflict issue, we can manually resolve the conflict and then perform a commit. If an error occurs while resolving a conflict, right-click Default Changelist-Revert… Clear the contents of the current working directory, re-execute Apply Stash once, and then repeat the conflict resolution process.

2.3 File occupancy error

When you perform step 2 Git merge, you may fail because the file is occupied. For example, a project might introduce some JAR files that have been dynamically loaded by the JVM locally, and if someone else updates the JAR file and pushes it to a remote branch, you may encounter this problem when you update the JAR file.

The solution to this error is simple: first unhold the file, for example by terminating the local JVM process, and then click VCS-Update again.

When you perform step 3 of the Git Stash Pop, it also fails because the file is occupied. For example, when you update a JAR file, the recovery may fail because the JAR file is occupied when you restore the storage.

For this type of error, you need to first unload the file and then manually perform the unstash operation.

3. Submit first or update first? It’s a problem!

3.1 Problems caused by submission before update

3.1.1 Difficult to handle when conflicts occur

If you submit first and there is a conflict during the update, it means that there is a problem with the submission you just created, usually due to team communication or division of labor, but either way, someone else has already pushed ahead and your submission will be rejected. Even if a conflict is resolved manually, the commit will remain in the history as a problem. If someone else resets the commit and continues to work on it, the chance of a conflict occurring while merging other branches will be greatly increased, so it is best to undo the commit first, then update and resolve the conflict. Finally, create a new commit.

3.1.2 Wrong way to deal with conflicts

After a conflict, some students may think of the following ways to deal with it:

  • Clear the current workspace
  • Adjust the conflicting parts of the code
  • The update operation is then performed again

This is obviously not possible, because your tweaks are first stored by IDEA (stash), and then there is a conflict in the second step of the update, and when a conflict occurs, your changes are not restored to storage (unstash), which makes it look like your tweaks are missing, which is confusing.

3.1.3 Rebase overwrites the commit history

If you select the update type as “Rebase” in the IDEA update window, it is equivalent to manually executing the git fetch && git Rebase or git pull — Rebase command. This has the advantage of not generating an auto-merge commit, keeping the commit history concise. Note, however, that after rebasing, your local referrals are overwritten. The commit hash has changed, even though the commit message is the same, as shown in the following figure:

After executing the following Rebase command,

$ git checkout dev
$ git rebase master

The execution result is as follows:

Note that the V4 and V5 commits in the result have been overwritten.

3.2 It is recommended to update before submission

If you know in advance that there will be conflicts, you won’t commit the code first, but conflicts are inevitable, which requires us to develop good development habits. Instead of resolving post-commit conflicts, resolving conflicts as early as possible and committing them not only reduces the need for a meaningless auto-merge commit, but also simplifies the process when a conflict occurs.

3.3 Develop good habits

To avoid conflicts as much as possible, the following development habits are recommended:

  • Update before coding
  • Update before you submit
  • Check for compilation errors before committing
  • Keep the submission granularity as small as possible and the description as accurate as possible
  • Public files have been modified to inform other members of the updates as soon as possible
  • Finally, and most importantly, the division of labor should be clear

Recent hot article recommended:

1.1,000+ Java interview questions and answers (latest version of 2021)

2. I finally got the IntelliJ IDEA activation code through open source project. How nice!

3. Ali Mock tool officially open source, kill all the Mock tools in the market!

4.Spring Cloud 2020.0.0 is officially released, a new and disruptive version!

5. “Java development manual (Songshan edition)” the latest release, fast download!

Feel good, don’t forget with thumb up + forward oh!