Branch
Preface
Branches are like avatars in fantasy novels, let one avatar learn JavaScript
, and the other avatar to learn Git
, and then merge the two avatars. , only at a certain time, the two avatars merged, and you learned Git
and JavaScript
!
Well, without further ado, what is the use of branches in practice? Suppose you are going to develop a new feature, but it takes two weeks to complete. You wrote 50% of the code in the first week. If you submit it immediately, the incomplete code base will cause others to be unable to work because the code has not been written yet. If you wait for the code to be fully written and then submit it again, there is a huge risk of losing your daily progress.
Now that you have branches, you don't have to be afraid. You created a branch that belongs to you, others can't see it, and you continue to work normally on the original branch, while you work on your own branch, submit it if you want to, until the development is completed, and then merge it into the original branch, in this way, it is both safe and does not affect the work of others.
Then some people may ask, will it be very slow to create and switch branches, if there are a lot of files.
Don't worry about this, Git's branches are different, no matter creating, switching and deleting branches, Git can complete it in 1 second! Whether your repository is 1 file or 10,000 files.
Recommend
Submit more, push less. When multiple people collaborate, push will frequently bring about merge conflicts and affect efficiency. Therefore, use the commit command as much as possible and reduce the use of merges, which will save a lot of time.
Use Git flow, which is a multi-branch structure.
Use branches to keep the master branch clean. This is a point I highly recommend, commit in the branch, then switch to the master branch to update
git pull --rebase
, then merge the branch and push. Such a process will avoid the situation of cross-merging (there will be no situation where there are multiple common ancestor nodes). In fact, the reason many people feel overwhelmed by the git merge operation is the cross-merge problem caused by various reasons, which causes some code to be lost in the process of merging. Keeping the master branch clean can avoid cross merges.Disable
fast-forward
mode. Use therebase
parameter when pulling the code (provided that the main branch is kept clean), and use the--no-ff
parameter to disable thefast-forward
mode when merging, which can ensure the clarity of the nodes. It also avoids the occurrence of cross-merging.