Multi-person collaboration

Source: Internet
Author: User

When you clone from a remote repository, git automatically maps the local master branch to the remote master branch, and the default name of the remote repository is origin.

To view the remote database information, useGit remote:

$ git remoteorigin

Or, useGit remote-VShow more details:

$ git remote -vorigin  [email protected]:GitLearnBling/learngit.git (fetch)origin  [email protected]:GitLearnBling/learngit.git (push)

The above shows the addresses of origins that can be crawled and pushed. If you do not have the push permission, you cannot see the push address.

Push Branch

The push branch refers to pushing all local commits on the branch to the remote database. When pushing, you must specify a local branch. In this way, git will push the branch to the remote branch corresponding to the remote Library:

$ git push origin master

If you want to push other branches, such as Dev, change:

$ git push origin dev

However, it is not necessary to push local branches to a remote device. Which branches need to be pushed and which do not?

  • The master branch is the master branch, so it must be synchronized from time to time;

  • The Dev branch is a development branch. All members of the team need to work on it, so they also need to be remotely synchronized;

  • Bug branches are only used to fix bugs locally, so there is no need to push them to the remote computer unless the boss wants to see if you have fixed several bugs each week;

  • Whether the feature branch is pushed remotely depends on whether you work with your partner to develop it.

In a word, in git, branches can be hidden locally and pushed or not, depending on your mood!

 

 

 

Capture Branch

When multiple users collaborate, they will push their modifications to the master and Dev branches.

Now, to simulate a friend, you can clone it in another computer (add the SSH key to GitHub) or in another directory on the same computer:

$ git clone [email protected]:GitLearnBling/learngit.gitCloning 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 clone from the remote database, by default, your friends can only see the local master branch. Do not believe it.Git BranchCommand to see:

$ git branch* master

Now, to develop your partner on the dev branch, you must create the dev branch of the remote origin to the local device. So he uses this command to create the local Dev Branch:

$ git checkout -b dev origin/dev

Now, he can continue the modification on Dev, and then push the dev branch to the remote from time to time:

$ git commit -m "add /usr/bin/env"[dev 291bea8] add /usr/bin/env 1 file changed, 1 insertion(+)$ git push origin devCounting 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 [email protected]:GitLearnBling/learngit.git   fc38031..291bea8  dev -> dev

 

 

 

Your partner has pushed his submission to the origin/dev branch, and it happens that you have modified the same file and tried to push it:

$ 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 devTo [email protected]:GitLearnBling/learngit.git ! [rejected]        dev -> dev (non-fast-forward)error: failed to push some refs to ‘[email protected]:GitLearnBling/learngit.git‘hint: Updates were rejected because the tip of your current branch is behindhint: 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 partner's latest submission conflicts with the commit you are trying to push, and the solution is very simple. Git has prompted us to first useGit pullCapture the latest commit from origin/dev, merge it locally, resolve the conflict, and then push:

$ git pullremote: 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:GitLearnBling/learngit   fc38031..291bea8  dev        -> origin/devThere 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 pullThe connection between the local Dev Branch and the remote origin/dev branch is not specified. Set the link between Dev and origin/dev as prompted:

$ git branch --set-upstream dev origin/devBranch dev set up to track remote branch dev from origin.

Then pull:

$ git pullAuto-merging hello.pyCONFLICT (content): Merge conflict in hello.pyAutomatic merge failed; fix conflicts and then commit the result.

This timeGit pullIf the merge operation succeeds, but there is a conflict, it must be resolved manually. The solution is exactly the same as "resolution conflict" in branch management. After solution, submit and then push:

$ git commit -m "merge & fix hello.py"[dev adca45d] merge & fix hello.py$ git push origin devCounting objects: 10, done.Delta compression using up to 4 threads.Compressing objects: 100% (5/5), done.Writing objects: 100% (6/6), 747 bytes, done.Total 6 (delta 0), reused 0 (delta 0)To [email protected]:GitLearnBling/learngit.git   291bea8..adca45d  dev -> dev

 

 

 

Therefore, the working mode of multi-person collaboration is usually as follows:

  1. First, you can try to useGit push origin branch-namePush your own changes;

  2. If the push fails, you must useGit pullTry to merge;

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

  4. After no conflict or resolution, useGit push origin branch-namePush is successful!

IfGit pullIf "no tracking information" is displayed, the link between the local branch and the remote branch is not created.Git Branch -- Set-upstream branch-name origin/branch-name.

This is the working mode of multi-person collaboration. Once you are familiar with it, it is very simple.

Summary
  • View the remote database information and useGit remote-V;

  • If a new local branch is not pushed to a remote server, it is invisible to others;

  • Push the branch from the local device, and useGit push origin branch-nameIf the push fails, useGit pullCaptures remote new submissions;

  • Create a local branch corresponding to the remote branch and useGit checkout-B branch-name origin/branch-name, The name of the local branch should be the same as that of the remote branch;

  • Create an association between a local branch and a remote branch.Git Branch -- Set-upstream branch-name origin/branch-name;

  • Capture a branch from a remote device and useGit pullIf a conflict exists, you must first handle the conflict.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.