GIT Branch Management: Multi-person collaboration

Source: Internet
Author: User

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  https://github.com/wangmingshun/studygit.git (fetch)origin  https://github.com/wangmingshun/studygit.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
Username for ' https://github.com ': Wangmingshun
Counting objects:27, done.
Delta compression using up to 2 threads.
Compressing objects:100% (27/27), done.
Writing objects:100% (27/27), 2.39 KiB | 0 bytes/s, done.
Total (delta), reused 0 (Delta 0)
To Https://github.com/wangmingshun/studygit.git
90bc1f7. 9565f59 Master, Master

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

$ git push origin dev
Username for ' https://github.com ': Wangmingshun
Counting Objects:3, done.
Delta compression using up to 2 threads.
Compressing objects:100% (3/3), done.
Writing objects:100% (3/3), 297 bytes | 0 bytes/s, done.
Total 3 (Delta 1), reused 0 (Delta 0)
To Https://github.com/wangmingshun/studygit.git
* [New branch] Dev-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 [email protected]:wangmingshun/studygit.gitcloning into'Studygit'... Warning:permanently added the RSA host key forIP Address'192.30.252.131'To the list of known hosts.remote:Counting objects: +, Done. remote:compressing objects: -% ( -/ -), Done. Remote:total +(Delta6), reused +(Delta6), pack-reused0Receiving objects: -% ( +/ +), Done. Resolving deltas: -% (6/6), Done. Checking Connectivity ... 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

First pull or fetch, or error

$ git pullremote:counting objects:3, Done. remote:compressing objects: -% (2/2), Done. Remote:total3(Delta1), reused3(Delta1), pack-reused0Unpacking objects: -% (3/3), Done. From Github.com:wangmingshun/Studygit* [New branch] Dev-and origin/devupdating 90bc1f7. 9565f59fast-forward Readme.txt|6++++++1 fileChanged6Insertions (+)

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:

$ git Add. $ git commit-M"Test remote Dev"[Dev 36cc09a] test remote dev2Files changed,6Insertions (+),2Deletions (-) $ Git push Origin devwarning:permanently added the RSA host key forIP Address'192.30.252.129'To the list of known hosts. Counting objects:4, Done. Delta compression using up to2threads.compressing objects: -% (3/3), Done. Writing objects: -% (4/4),336bytes |0BYTES/S, Done. Total4(Delta1), reused0(Delta0) to [email Protected]:wangmingshun/studygit.git b7c506b. 36cc09a DevDev

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. $ git commit-M"dddd"[Dev D824e44] dddd1 fileChanged2Insertions (+),1Deletion (-) $ Git push origin devusername for 'https://github.com': Wangmingshunto https://Github.com/wangmingshun/studygit.git! [Rejected] Dev-Dev (fetch first) error:failed to push some refs to'Https://github.com/wangmingshun/studygit.git'Hint:updates were rejected because the remote contains work DoHint:not has locally. This was usually caused by another repository pushinghint:to the same ref. Want to first integrate the remote changeshint: (e.g.,'git pull ...') before pushing Again.hint:See the'Note about Fast-forwards' inch 'git push--help'  forDetails.

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:4, Done. remote:compressing objects: -% (2/2), Done. Remote:total4(Delta1), reused4(Delta1), pack-reused0Unpacking objects: -% (4/4), Done. From https://Github.com/wangmingshun/studygitb7c506b. 36cc09a Dev-and origin/Devthere is no tracking information forThe current branch. Please specifywhichbranch you want to the merge with. See Git-pull (1) fordetails. Git Pull<remote> <branch>If you wish to set tracking information forThis branch can DoSo with:git branch--set-upstream-to=origin/<branch> Dev

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:

$ git branch--set-upstream-to=origin/devbranch Dev set up to track remote branch dev from origin.

Pull again:

$ git pullauto- in then commit the result.

This is git pull successful, but there are conflicts in the merge, which need to be resolved manually, and the workaround is exactly the same as the resolution conflict in branch management. After resolving, submit, and then push:

$ git Add. $ git commit-M"Multi-person Remote Repository submission conflict resolution"[Dev8737740] Multi-person Remote Repository Submission conflict resolution $ GIT push origin devusername for 'https://github.com': wangmingshuncounting objects:6, Done. Delta compression using up to2threads.compressing objects: -% (6/6), Done. Writing objects: -% (6/6),632bytes |0BYTES/S, Done. Total6(Delta2), reused0(Delta0) to https://Github.com/wangmingshun/studygit.git36cc09a.8737740Dev-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;

    • Establish the Association of local and remote branches, using thegit branch --set-upstream-to=origin/branch-name

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

GIT Branch Management: Multi-person collaboration

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.