preface

A secret book into the universe, so what is this secret book? Git add., git commit -m ‘commit message’, git push origin main There are plenty of git options out there to explore! Let’s master this secret book and go out into the universe!

Git commands: Git General Flow chart

The core of Git secrets – multi-player collaborative conflict

1. What is Git collaborative development

When developing a large project is usually a lot of people, each person is responsible for part of the project, Git is realize the coordinated development of many of the most common tool, but for new to Git, collaborative development will meet many problems, the main content of this blog is to give people the correct order for the steps of coordinated development, And explain and solve the problems encountered in this process.

How do you avoid code conflicts when so many people are working together?

Let’s first understand the process of multi-person collaborative development!

2. Preparation for multi-party collaborative development

  • Build a new warehouse
  • Create the master branch, that is, upload the initial content of the project to the Master branch
  • Division of labor among team members (there should be no conflict among members)

3. Development phase

  • Each member clones the contents of the master branch of the remote repository
  • Work according to the division of labor

4. Commit phase (conflict generation phase)

(1) After completing their own work, each member should pay attention to the changes of the remote warehouse first

Method 1: Pull remote branch (Git pull instruction)

The Git pull directive can be understood as two steps:

  • Get remote branches
  • Merge the acquired remote branch with the local branch

Git Fetch fetch

Git fetch

  • Get the latest version of the remote branch to the local (that is, create a new branch locally with the latest version of the remote branch)
  • After acquiring the branch, you can compare and view the contents of the remote branch. If you want to push the branch, you can merge and push the acquired branch.

(2) Obtain the latest version of the remote warehouse and merge it locally

Merge conflict problem: Merge conflict

Generation situation (3 kinds) :

  • Two people made changes to different files on the same project
  • Two people made changes to different areas of the same file for the same project
  • Different changes were made to the same area of the same file for the same project

Consider: Which of the above three situations will cause conflict?

Here’s the answer:

The first two cases can be merged automatically by Git, while the third case cannot be merged automatically and requires manual merge (the third case causes conflicts).

Question 1: What is auto merge?

A merge is essentially a combination of two people (branches) making changes to the basics of the project, note the changes to the project. In the first two cases, two people make changes to different areas of the project without interfering with each other, so Git can automatically integrate the changes made by two people

Question 2: What is a manual merge?

When Git doesn’t know which of the two changes to keep, a human is required to make the decision, either to keep either change, or to keep both changes. Completing these decisions is the purpose of manual merging.

Question 3: Why do I need to merge manually?

When can be automatically merged, shows the change won’t conflict of two people, but when two people to modify the same area of the same file as the two changes can produce conflict, Git will not be able to integrate these two changes, because the Git don’t know what to keep it either of the two modified (or save) along with all the, this is you need artificial merged manually.

After the merge, you can upload it to your own branch of the remote repository

5. Review phase

  • Administrators review the code and merge it with the master branch if no problems are found. Administrators should complete this process as soon as possible to ensure that members are pulling the latest versions.

  • Conflicts may occur during administrator merge. You need to contact the administrator to merge the administrator.

  • Functions of administrators:

Maintains the master branch of the remote warehouse, including the following:

  • Check the code of each member branch for problems
  • Merge the member branch code into the master branch
  • If a merge conflict occurs, perform a manual merge

6. Conflict resolution

1. Types of conflicts

Logic conflict

Git automatically handles (merge/apply patches) successfully, but logically there is a problem.

For example, someone else has changed the file name, but I still use the old file name. In this case, automatic processing can be successful, but it is actually problematic.

For example, if the function returns a value with a different meaning, but I still use the old meaning, this situation is handled automatically, but there may be a significant BUG hidden.

This problem is mainly guaranteed through automated testing. Therefore, it is better to be able to write relatively complete automated test cases.

The solution to this conflict is to make a BUG fix. Not really resolving git reported conflicts.

Content of the conflict

Git reports content conflicts when two users modify the same area of the same file. This is the kind of conflict we often see, and the later solutions are also focused on this kind of conflict.

A tree conflict

Conflicts caused by file name changes are called tree conflicts. For example, if user A changes the name of the file to A.C and user B changes the name of the same file to B.C, a conflict will occur when user B merges the two commit files. If the final decision is to use B.C, the solution is as follows:

git rm a.c
git rm origin-name.c
git add b.c
git commit
Copy the code

When you run the preceding two git rm commands, file-name: needs merge is reported.

Git merge differs from Git rebase when merging

When the merge operation encounters a conflict, the current merge operation cannot continue. After manually modifying the conflicting content, add and commit will do the job. A rebase operation interrupts rebase and prompts you to resolve the conflict. After the conflict is resolved, modify add and run git rebase -continue to continue, or git rebase -skip to ignore the conflict

2. Rebase

Rebasing should be used on branches pulled out of the local repository. Do not rebase on branches that have copies outside the local repository

Git pull versus git pull –rebase

  • git pull = git fetch + git merge

  • git pull –rebase = git fetch + git rebase

Git pull –rebase — uncommit every commit in your local branch Save them temporarily as patches (put them in the “.git/rebase” directory), update the current local branch to the latest “Origin” branch, and apply the saved patches to the current local branch

Conflict 1

Git pull-rebase conflicts occur during git pull-rebase after commit.

  • 1 Find the conflict file and resolve the conflict
  • 2 Run git add XXX (XXX indicates the full path of conflicting files).
  • Git rebase — continue
  • 4 Run the git pull -rebase command
  • 5 Run git push

Conflict between 2

When you have a local change, you implement the Git stash and then pull the latest code from the server (git pull-rebase). There is a conflict, please do as follows:

  • 1 Locate the conflict file and resolve the conflict
  • 2 Run git add XXX (XXX indicates the full path of conflicting files).
  • 3 git commit
  • 4 the git pull – rebase
  • 5 git push

Merge Commit: Merge Commit: Merge Commit: Merge Commit: Merge Commit: Merge COMMIT: Merge COMMIT: Merge COMMIT: Merge COMMIT

// Git Stash stores all uncommitted changes (workspaces and staging areas) on the stack for subsequent restoration of the current working directory.
git stash
git pull --rebase 
git push
// Git stash pop retrieves the most recently saved contents from the git stack and restores the workspace. Since there can be multiple Stash contents, a stack is used to manage the contents, and pop reads from the nearest Stash and restores the contents. Pull out the staging file and compare it with the pull file
git stash pop
Copy the code

3. Rollback (reset and Revert)

Git reset

Git reset –hard and git reset –soft

  • Git reset — soft: rollback to a version (git added). Only the commit information is rolled back, but the index file level is not restored. If you want to commit, simply commit;

  • Git reset – — hard: the local source code will be changed to the previous version, and the changes contained in the revoked commit will be washed out.

Where: A and B are normal commits, while C and D are error commits. Now, we want to return the C and D. At this point, the HEAD pointer points to the D commit (5lk4er). We can do this by simply moving the HEAD pointer to B commit (a0FvF8).

So what is the order? That is the git reset command

1.git reset –hard
//git reset --hard <commit_id>

git reset --hard a0fvf8 
Copy the code

After the command is executed, the HEAD pointer will move under B commit

And at this point, the remote warehouseHEAD The needle is still there. It’s still thereD To submit. So, if you just use git pushCommand will not be able to push changes to the remote repository. In this case, you can only use -fOption forces the submission to the remote repository

git push -f 
Copy the code

The downside of retracting code in this way is that it moves the HEAD pointer backwards and loses subsequent commits. In the future, if you suddenly discover how wonderful C and D are, they’re long gone.

2. The git reset – soft

//git reset --soft <commit_id>
git reset --soft a0fvf8 
Copy the code

Example:

Git reset changes the location of the HEAD to a previous version, as shown in the following figure.

Application scenario: This method can be used if you want to revert to a previously committed version and we don’t want any later versions.

Git Revert

Git Revert works by doing the reverse to create a new version that has the same content as the target version you want to fall back to, but the HEAD pointer points to the newly generated version, not the target version.

To implement the above example using git Revert, you can revert D and then REVERT C (if there are multiple commits that need to be reverted) :

// 1 revert D
git revert 5lk4er
// 2 revert C
git revert 76sdeb
Copy the code

Then two new commits are generated:D'andC' There are only two submissions required hererevertWe can go back one by one. But what if there were dozens? Backtracking would be inefficient and error-prone. We can use the following methods to perform batch rollback:

git revert OLDER_COMMIT^.. NEWER_COMMITCopy the code

At this point, the incorrect submissions C and D are still in place and can be relied upon for future dumping. Also, the HEAD pointer is moved backwards and can be pushed to a remote repository using git push. And this is exactly what the enterprise encourages.

Git revert is used to “reverse” a version in order to undo changes made to that version. For example, we commit three releases (version 1, version 2, and version 3) and suddenly discover that version 2 doesn’t work (e.g. If you want to undo version 2 without affecting a commit to undo version 3, you can use git revert to reverse version 2 and create a new version 4 that retains version 3 but undoes version 2. As shown below:

Application scenario: If we want to undo a previous version, but we want to keep the later version of the target version and record the entire version change process, we can use this method.

Git can be used to revert previous versions of Git.

Git instruction practice site

Can I know what happened after each command was executed? Learn Git shoot, practice Git command

Web page display For example: Practice a problem

Introduce the git rebase Git rebase main not executed

Git rebase main Git rebase bugFix not executed

Execute git rebase bugFix

Questions Operation tips Introduction to answer screen The answerBy entering the command, we can observe that the tree on the right will change, which will give us a better understanding of git commands

Below you can according to the answer to enter git command to try, I will not answer for you! If you have any questions, leave them in the comments!

// Create and switch to the bugFix branch
git checkout -b bugFix 
// Commit once
git commit
// Switch to main branch
git checkout main
// Commit once
git commit
// Switch to the bugFix branch again
git checkout bugFix
/ / rebase to main
git rebase main
Copy the code

conclusion

Git multi-person collaboration is also the first time to understand, after all, students have not really encountered these problems in the work. If there is any problem, please kindly point it out and I will revise it in time. Thank you in advance!

Some of the above drawings are stolen, if there is infringement please contact me!

Oneself big three is learning front end!