Cooperation
Conclusion
View remote repository information
git remote
View remote repository details
git remote -v
Sync with remote repository code
git pull
# git pull = git fetch + git merge
Create a local branch corresponding to the remote branch
git checkout -b branch-name origin/branch-name
git switch -c branch-name origin/branch-name
Associate a local branch with a remote repository
git branch --set-upstream-to <branch-name> origin/<branch-name>
push local branch to remote repository
git push origin <branch-name>
Caption
The working mode of multi-person collaboration is usually as follows:
- First, you can try to push your own changes with
git push origin <branch-name>
; - If the push fails, because the remote branch is newer than your local, you need to use
git pull
to try to merge first; - If the merge has conflicts, resolve the conflicts and commit locally;
- If there is no conflict or the conflict is resolved, then push with
git push origin <branch-name>
and it will be successful! - If
git pull
promptsno tracking information
, it means that the link relationship between the local branch and the remote branch has not been created.
This is the working mode of multi-person collaboration, and once you are familiar with it, it is very simple.
Tips
- If the new branch created locally is not pushed to the remote, it will not be visible to others;
- Create a branch corresponding to the remote branch locally, and the names of the local and remote branches should be the same;
- Fetch the branch from the remote, and if there is a conflict, resolve the conflict first.
Problem
The local repository has files and the remote repository also has files, but the two repository files are inconsistent. At this time, associate the local repository with the remote repository, execute git branch --set-upstream-to <branch-name> origin/<branch-name>
, prompting an error: error: the requested upstream branch 'origin/ master' does not exist"
Solution
If you execute git pull
directly, it will prompt: refusing to merge unrelated histories
, the correct way:
git pull origin master --allow-unrelated-histories
git branch --set-upstream-to=origin/master master
git push origin master