Git Multi-person Collaboration working mode

Source: Internet
Author: User

Multi-person Collaboration 148 reads

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

To view information for a remote library, use git remote :

$ git remoteorigin

Or, use git remote -v the display for more detailed information:

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

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

Push Branch

The push branch is the push of all local commits on that branch to the remote library. When pushing, specify the local branch so that git pushes the branch to the remote branch of the remote library:

$ git push origin master

If you want to push other branches, for example dev , change to:

$ git push origin dev

However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which do not?

    • masterThe branch is the main branch, so synchronize with the remote at all times;

    • devBranch is the development branch, all members of the team need to work on it, so also need to synchronize with the remote;

    • Bug branches are only used to fix bugs locally, there is no need to push remote, unless the boss wants to see how many bugs you have fixed every week;

    • Whether or not the feature branch is pushed to the remote depends on whether you are working with your little partner to develop it.

In short, is in git, branch can completely in the local hide to play, whether to push, depending on your mood!

Crawl Branch

When people collaborate, everyone master dev pushes their own changes to and from the branch.

Now, simulate a small partner that you can clone on another computer (note to add SSH key to GitHub) or another directory on the same computer:

$ git clone git@github.Com:michaelliao/learngit.gitCloning into' Learngit ' ...RemoteCountingObjects46, 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 partner clones from the remote repository, by default, your small partner sees only the local master branch. Don't believe you can use the git branch command to see:

$ git branch* master

Now that your little partner is developing on the dev branch, you have to create a origin remote dev branch to local, and he uses this command to create a local dev branch:

$ git checkout -b dev origin/dev

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

$ gitCommit-m "add/usr/bin/env" [Dev 291bea8] add/usr/bin/env Span class= "number" >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]:michaelliao/learngit.git fc38031. .291bea8 Dev, dev            

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

$ git add hello.py $ gitCommit-m"Add Coding:utf-8" [Dev Bd6ae48]add coding:utf-8 1 file changed, 1 insertion (+) $ Git push origin devto [email protected]:michaelliao/ Learngit.git! [Rejected] Dev, Dev (non-fast-forward) error:failed to push some refs span class= "keyword" >to  ' [Email protected]:michaelliao/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 the latest submission of your little partner conflicts with the submission you are trying to push, and the workaround is simple, Git has prompted us to first use the git pull latest commit from origin/dev scratch, then merge 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:michaelliao/learngit fc38031. .291bea8 Dev-origin/devthere is no tracking information for the current Branch. Please specify which branch your want to merge with. See Git-pull (1) for details git pull <remote> < span class= "tag" ><branch>if You wish to set tracking information for the branch you can does so W Ith:git Branch--set-upstream Dev origin/<BRANCH>    

git pullAlso failed because there is a link to the remote branch that does not specify a local dev branch origin/dev , depending on the prompt, the setting dev and origin/dev the link:

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

Pull again:

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

This time git pull succeeds, but there is a merge conflict that needs to be resolved manually, and the workaround is exactly the same as the conflict resolution in branch management. After resolving, commit, then push:

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]:michaelliao/learngit.git 291bea8..adca45d dev -> dev

As a result, the working mode of multi-person collaboration is usually this:

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

    2. If the push fails, because the remote branch is newer than your local, you need git pull to first try to merge;

    3. If there is a conflict in the merge, resolve the conflict and submit it locally;

    4. No conflict or conflict resolution, and then git push origin branch-name push to Succeed!

If git pull "No tracking information" is indicated, then the link relationship between the local branch and the remote branch is not created, using the command git branch --set-upstream branch-name origin/branch-name .

This is a multi-person collaborative mode of work, once familiar with, it is very simple.

Summary
    • View Remote library information, use git remote -v ;

    • Local new branches are not visible to others if they are not pushed to the remote;

    • Push branches from local, use git push origin branch-name , if push fails, first use git pull crawl remote new commit;

    • Local and remote branches corresponding to the branch, use git checkout -b branch-name origin/branch-name , local and remote branch names are best consistent;

    • Establishing the Association of local and remote branches, using git branch --set-upstream branch-name origin/branch-name ;

    • From the remote crawl branch, use git pull , if there is a conflict, you must first handle the conflict.

Git Multi-person Collaboration working mode

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.