git Multi person collaboration _ Power node Java College collation

  • 2020-06-19 12:26:56
  • OfStack

When you clone from a remote repository, Git actually automatically takes the local master Branch and remote master The branches correspond, and the default name for the remote repository is origin.

To view remote library information, use git remote:


$ git remote
origin

Or, in git remote -v Display more detailed information:


$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)

The address of origin that can be grabbed and pushed is shown above. Without push access, push's address is not visible.

Push the branch

Push branch pushes all local submissions on the branch to a remote library. When pushing, specify the local branch so that Git pushes the branch to the remote branch corresponding to the remote library:


$ git push origin master

If you want to push another branch, such as dev, change this to:


$ git push origin dev

However, it is not necessary to push local branches to remote locations. So, which branches need to be pushed and which do not?

master The branch is the main branch, so it should be synchronized remotely at all times. dev A branch is a development branch on which all members of the team need to work, so it also needs to be synchronized remotely; The bug branch is only used to fix bug locally, so there's no need to go remote unless your boss wants to see how many bug you fix each week. Whether or not the feature branch is pushed remotely depends on whether or not you are working with your partner on it.

In short, in Git, branches can be hidden and played locally, depending on your mood!

Fetching branches

When many people work together, everyone goes master and dev Push the respective changes on the branch.

Now, to simulate 1 of your friends, you can clone them on another computer (note that SSH Key is added to GitHub) or in another directory on the same computer:


$ git clone git@github.com:michaelliao/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 46, done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 46 (delta 16), reused 45 (delta 15)
Receiving objects: 100% (46/46), 15.69 KiB | 6 KiB/s, done.
Resolving deltas: 100% (16/16), done.

When your friends are from remote library clone, by default, your friends will only see local ones master Branch. Use it if you don't believe me git branch Look at the commands:


$ git branch
* master

Now, your friends are here dev To develop on a branch, you must create a remote master0 the dev Branch to the local, so he USES this command to create the local dev Branches:


$ git checkout -b dev origin/dev

Now, he can be here dev Go ahead and edit, then, from time to time dev branch push To the remote:


$ git commit -m "add /usr/bin/env"
[dev 291bea8] add /usr/bin/env
 1 file changed, 1 insertion(+)
$ git push origin dev
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:michaelliao/learngit.git
  fc38031..291bea8 dev -> dev

Your little friend has origin/dev The branch pushes his submission, and you happen to make changes to the same file and try to push:


$ git add hello.py 
$ git commit -m "add coding: utf-8"
[dev bd6ae48] add coding: utf-8
 1 file changed, 1 insertion(+)
$ git push origin dev
To git@github.com:michaelliao/learngit.git
 ! [rejected]    dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:michaelliao/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Push failed because your friend's latest submission is in conflict with the submission you are trying to push. The solution is also simple. Git has prompted us to use it first git pull Take the latest submission from origin/dev Grab it, then merge it locally, resolve the conflict, and push:


$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:michaelliao/learngit
  fc38031..291bea8 dev    -> origin/dev
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

  git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

  git branch --set-upstream dev origin/<branch>

git pull It also failed because no local was specified dev Branching and remote origin/dev Link to branch, as prompted, set dev and origin/dev The link:


$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)
0

pull again:


$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)
1

This time, git pull Successful, but the merge conflicts need to be resolved manually in exactly the same way as in branch management. After settlement, submit again push:


$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)
2

Therefore, the working mode of multi-person collaboration is usually like this:

1. First, you can try git push origin branch-name Push your own changes;

2. If the push fails, the remote branch needs to be used first because it is newer than your local git pull Attempt to merge;

3. If there is a conflict in the merge, the conflict is resolved and submitted locally;

4. Use when there is no conflict or when the conflict is resolved git push origin branch-name Push and it works!

if git pull Prompt "no tracking information" indicates that the link between the local branch and the remote branch has not been created. Use the command git branch --set-upstream branch-name origin/branch-name .

This is how multi-player collaboration works, and once you get used to it, it's very simple.

summary

View remote library information, use git remote -v ; If a branch is not pushed to a remote location, it will not be visible to others. Push the branch from the local, use git push origin branch-name If push fails, use first git pull Fetching remote new submissions; Create a branch that corresponds to a remote branch locally, using git checkout -b branch-name origin/branch-name , local and remote branch names preferably 1 to; Establish associations between local and remote branches git branch --set-upstream branch-name origin/branch-name ; Grab the branch from the remote and use it git pull, If there is a conflict, deal with it first.

Related articles: