SoFunction
Updated on 2025-04-12

How to solve the problem of Git pull and merge code process and multi-player collaborative development

Git pull merge code process and problem solution for multi-person collaborative development

In projects that are collaboratively developed by multiple people, it is crucial to use Git effectively. Git provides a powerful set of tools that enable team members to develop and merge code in parallel. However, in this process, some problems may be encountered, such as different branches modifying the same file at the same time, resulting in a merge conflict.

Git operation process

1. Pull code (Pull)

Before you start working, you first need to pull the latest code from the remote repository locally.

The following commands can be used:

git pull origin master

This will update the local code base and merge the latest changes to the remote repository.

2. Check the code status (Status)

Before making any changes, you can use the following command to view the status of the current code:

git status

This will show which files have been modified, which files have been temporarily saved, and whether there are untracked files.

3. Submit to the local cache area

Add the modified file to the local cache and prepare to submit:

git add .

4. Submit to the local repository

Submit changes to the local cache area to the local repository, with the corresponding description information:

git commit -m 'Description Information'

5. Submit to the remote repository

Push changes to the local repository to the remote repository, usually to the master branch:

git push origin master

6. Create a branch

If you need to create new features or fix bugs in your project, you can create a new branch using the following command:

git checkout -b New branch name

When multiple developers modify the same file on different branches, Git may conflict when trying to merge those branches.

Merge conflict means that Git cannot automatically decide how to merge two modifications, so manual intervention is required.

Here are more detailed steps to resolve merge conflicts

1. Merge branches

First, switch to the target branch, which is usually the main branch you want to merge the modifications.

For example, merge to the main branch (usuallymaster):

git checkout master
git pull origin master  # Make sure the local master branch is up to dategit merge Branches to merge

2. Resolve conflicts

After executing the merge command, if a conflict occurs, Git marks the conflicting file.

Open the conflict file and you will see a tag like the following:

<<<<<<< HEAD
// The code comes from the target branch=======
// The code comes from the branch to be merged>>>>>>> Branch name

<<<<<<< HEADand>>>>>>>>> Branch NameThe part between is the conflicting code.

You need to manually select the code you want to keep.

Edit the file, delete the conflict markup, so that the file will present the final state you expect.

For example:

// The code comes from the target branch// Keep the modification of the target branch// ...

// The code comes from the branch to be merged// Keep the modifications to merge the branches// ...

3. Mark the file as resolved

Once you resolve the conflict, tell the Git file that it is ready to continue merging:

git add Conflict files

4. Continue to merge

Continue to execute the merge command. If you use a newer version of Git, you can use it--continueOptions:

git merge --continue

Or, if you use an older version of Git:

git commit -m 'Resolve the conflict'

5. Complete the merger

Finally, push the conflict-resolved changes to the remote repository:

git push origin master

Other issues in git development

Forgot to pull the latest code:

  • question:Before starting work, the latest code was not pulled, resulting in development based on outdated versions.
  • Solution:usegit pullTo get the latest code, make sure your work is based on the latest remote branch.

Submit sensitive information:

  • question:Files containing sensitive information (such as passwords, keys, etc.) have been submitted.
  • Solution:use.gitignoreto exclude sensitive files, or usegit rm --cachedRemoves the file from version control.

Delete files by mistake:

  • question:Important files were deleted by mistake.
  • Solution:usegit checkoutorgit restoreRecover deleted files or usegit resetRevoke uncommitted changes.

Branch chaos:

  • question:Too many branches or confusing, difficult to manage.
  • Solution:Regularly clean unwanted branches, use meaningful branch names, and avoid creating too many temporary branches.

Merge conflict not resolved:

  • question:A conflict occurred during merge but was not resolved correctly.
  • Solution:Resolve conflicts manually to ensure that the correct code is preserved and continue to merge.

The remote branch does not exist:

  • question:Try pulling or pushing to a remote branch that does not exist.
  • Solution:usegit branch -rCheck the remote branch to make sure the correct remote branch exists. Availablegit push -u origin branch nameTo push the newly created local branch to the remote.

Forced push:

  • question:usegit push --forceForced push may cause other people's work to be lost.
  • Solution:Try to avoid forced pushing, especially on shared branches. If you really need it, make sure to communicate and coordinate with team members.

Error global/local configuration:

  • question:An error in configuration can cause unnecessary problems.
  • Solution:usegit configTo check and modify global and local Git configurations to ensure they are set correctly.

Large Files and Git LFS:

  • question:Incorporating large files (such as binary files) into version control can cause storage and performance issues.
  • Solution:Use Git LFS (Large File Storage) to process large files and separate them from the Git repository to reduce the repository size.

Network issues:

  • question:Have network problems while pushing or pulling.
  • Solution:Check network connections to ensure access to remote repositories. usegit remote -vCheck that the URL of the remote repository is correct.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.