Git eleventh lecture Git remote warehouse operation

Remote repositories are the key to sharing code with others and developing collaboratively. Git provides a wealth of features to manage remote repositories, including operations such as pulling, pushing, and resolving conflicts. This chapter describes how to effectively operate remote warehouses.

pull and push

Pulling and pushing are common operations for data synchronization with remote repositories. With pull, you fetch the latest changes from a remote repository and merge them into your local repository. With push, you upload changes from your local repository to a remote repository.

Pull the remote warehouse

To pull changes from a remote repository, you can use the following command:

git pull <remote> <branch>

This will <remote>pull <branch>the latest changes from the specified branch ( ) from the specified remote repository ( ).

For example, to originpull mainchanges from branch from a remote repository named , you can run the following command:

git pull origin main

Git will automatically fetch the latest changes from the remote repository and try to merge them into the current branch.

Push to remote warehouse

To push changes from your local repository to a remote repository, you can use the following command:

git push <remote> <branch>

This will push changes from the current branch to the specified branch ( <remote>) of the specified remote repository ( <branch>).

For example, to push changes from the current branch to the branch originof the remote repository named main, run the following command:

git push origin main

Git will upload the changes from the local branch to the remote repository.

Branch Tracking and Setup

Branch tracking refers to the association relationship between local branches and remote branches. By setting up branch tracking, you can easily pull and push code while tracking the status of synchronization with remote branches.

Set up branch tracking

To set up a local branch to track a remote branch, you can use the following command:

git branch --set-upstream-to=<remote>/<branch>

This will associate the current branch with the specified remote branch.

mainFor example, to associate the current branch with the remote repository's branch, run the following command:

git branch --set-upstream-to=origin/main

View branch tracking

To view the tracking relationship between the local branch and the remote branch, you can use the following command:

git branch -vv

This will show the local branches and their associated remote branches.

resolve push conflicts

In multi-person collaborative development, you may encounter push conflicts, that is, multiple people modify the same file or the same line of code at the same time. Resolving push conflicts is important to ensure code integrity and consistency.

view conflicts

When a push conflict occurs, Git will prompt you for the conflicting files and locations. You can view conflicting files with the following command:

git status

This will show the status of the conflicting files.

resolve conflict

There are several ways to resolve push conflicts, the common way is to manually edit the conflicting files and resolve the conflicts. Open the conflict file and you'll see something like this:

<<<<<<< HEAD
// 本地更改内容
=======
// 远程仓库更改内容
>>>>>>> <commit-hash>

You need to carefully check the conflicting content, and modify and retain the corresponding code fragments according to your needs. Save the file after modification, and commit the conflicting changes.

Commit to resolve conflicts

Once the conflicts are resolved, the conflict-resolving changes can be committed with the following command:

git commit -m "Resolve merge conflict"

This will commit the changes that resolve the conflict and end the merge operation.

Guess you like

Origin blog.csdn.net/huanglu0314/article/details/131157523