git branch application and multi person collaboration

git branch application and multi person collaboration

1. Encyclopedia of knowledge

    Git (pronunciation / g ɪ t /) is an open-source distributed version control system, which can effectively and rapidly handle project version management from very small to very large. It is also an open source version control software developed by Linus Torvalds to help manage Linux kernel development.
    git includes a complete client and server, which upload and download files through some secure communication protocol (HTTPS or SSH).
    git tool installation and basic use reference: git configuration and basic use under ubuntu

2.git branch

This is the flat universe of science fiction movies. When you are working hard to learn Git in front of a computer, another person is working hard to learn SVN in another flat universe. Both can be executed simultaneously.
What's the use of this separation in practice? Suppose you are ready to develop a new function, but it takes two weeks to complete it. In the first week, you wrote 50% of the code. If you submit it at the moment, because the code has not been written yet, if you submit it directly, the code will be incomplete and affect the work of others. However, if all the codes are written and submitted at one time, there is a great risk of losing the progress of each day. With the generation of branches, you can create a branch of your own, and only you can see the branch content. If you can't see it, you can continue to work normally on the original branch. You can work on your own branch, submit it if you want, and then merge it into the original branch again after the development is completed. In this way, it is safe, and can be developed by multiple people without affecting each other.
Before there is no branch, the master branch is a line. Git Master points to the latest submission, and HEAD points to the master. The current branch and the submission point of the current branch can be determined:

   every time you submit, the master score will move forward by one step. In this way, as you continue to submit, the master score will also increase
Come to Vietnam.
  when we create a new partition, such as dev, git creates a new pointer called dev to point to the same submission as the master, and then points the HEAD to dev. the current partition of the table is on dev:

    when there is a dev branch, we can operate different branches by changing the HEAD direction. When the HEAD points to the dev branch, we can only operate dev. After our work is completed, we can merge the master and dev branches, that is, directly let the master point to the dev submit branch. You can even delete the dev branch directly.

3. Create a branch

$ git branch dev //Create branch
$ git checkout dev //Switch branch
$ git branch  //View branches
* dev  
  master
$ git branch -d dev //Delete branch
$ git checkout -b dev //Create a branch dev and switch to the current branch

    * indicates the current branch position.

3.1 example of creating a branch

$ touch 1.txt //create a new file
$ git checkout -b dev  //Create a dev branch and switch to the current branch
$ git add 1.txt //Add 1.txt
$ git commit -m "Branch application instance"  //Preparation of submission instructions
[dev 8873ebd] Branch application instance
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
$ git push origin dev  //Commit to dev branch


3.2 merge branches and submit

$ git checkout master //Switch to main branch
$ git merge dev  -m "Branch merge" //Merge branch, merge dev branch to main branch
 Update 38 d25e9..8873ebd
Fast-forward
 1.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

$ git add -A
$ git push origin master //Submit the merged content to the main branch

    after merging and submitting to the main branch, the dev branch can be deleted

$ git branch -d dev 
Branch deleted dev(Once 8873 ebd). 

4. Branch conflict

4.1 create dev branch and submit

$ git checkout -b dev  //Create a branch and switch to the dev branch
$ vim 1.txt  //Open 1.txt and add content
git Branch test
git Branch conflict test
$ git add 1.txt  //Add 1.txt
$ git commit -m "Branch conflict test"  
$ git push origin dev  //Commit to dev branch

4.2 modify the main branch and submit

$ git checkout master //Switch to main branch
$ vim 1.txt //Modify the 1.txt file
git Branch test
 Main branch test
$ git add 1.txt 
$ git commit -m "Main branch commit test"
$ git push origin master  //Submit the modified content to the main branch

4.3 consolidated branches

$ git merge dev -m "Branch merge" //Merge branches
 Auto merge 1.txt
 Conflict (content): merge conflict on 1.txt
 Auto merge failed. Fix the conflict and submit the corrected result.
$ git status  //View status
 In branch master
 Your branch and upstream branch 'origin/master' agreement.

You have paths that have not been merged.
  (Resolve conflicts and run "git commit")
  (use "git merge --abort" Termination of merger)

Unmerged paths:
  (use "git add <file>..." Tag solution)

	Modification by both parties: 1.txt

Modification has not been added to the submission (use "git add" and/or "git commit -a")

    open the 1.txt file to view the modifications of the main branch and dev branch.

    because both the main branch and the dev branch have modified the 1.txt text, the automatic merge of branches fails. At this time, it is necessary to manually solve the branch conflict problem. We can see the modification status of the main branch and dev branch by opening the 1.txt file. We need to merge manually.

$ vim 1.txt 
 git Branch test
 Main branch test
 git Branch conflict test
$ git add 1.txt 
$ git commit -m "Resolve merge conflicts"
$ git push origin master //Resubmit


    when we encounter automatic merge failure when merging branches, we can view the status information through git status, view the problem files that have failed to merge, then merge branches, add submission files again, and write submission instructions to resubmit.
In actual development, we should follow the following three basic principles to manage the distribution: first, the master distribution should be always stable, that is, we only release new versions, and we can't work on it at ordinary times; Where do you work? All work is done on the dev branch, that is to say, the dev branch is unstable. At a certain time, for example, when the 1.0 version is released, the dev branch is merged into the master, and the 1.0 version is released on the master; You and your partners work on the dev branch every day, and each branch has its own branch. You can merge with the dev branch from time to time.
  therefore, the division of teamwork looks like this:

5. Multi person collaboration

    Git is an open source distributed version control system, which can effectively complete version management and multi person collaborative development. To realize multi person collaboration, you need to add collaborators on the server side.
    in warehouse management, you can add developers, set code reviewers and branch merge reviewers.

Tags: git gitee

Posted by bloo on Fri, 02 Sep 2022 22:25:44 +0530