Do we really need to branch in Git?

微笑、不失礼 提交于 2020-06-22 22:56:27

问题


I'm in my first software engineering class. It's the first time any of us has worked in a team and used git and github. In class our teacher told us that you should usually branch off the master, after you finish your new feature, merge it back to the master. This is what I've been doing. However the other members of my group are not branching. They pull from the master on github to their local machine, make edits, finish their feature on their local master, then push to the master on github.

I'm trying to convince them to branch but now that I think about it, i find it more confusing. I've been told that the purpose of the branch is to make a copy of the code and not worry about ruining the master by accidentally putting unrunnable code.

But isn't their local master really just like a branch itself? As they are making edits, they aren't changing the master on github, so others are free to pull working code from github. Then they merge, similar to a branch.

I'm confused, why should we branch if what they are doing seems to be working?

Thanks!


回答1:


But isn't their local master really just like a branch itself?

Yes it is!

I'm confused, why should we branch if what they are doing seems to be working?

This depends on your workflow. What you seem to be describing is this workflow:

Alice makes a branch alice that tracks master and Bob makes a bob branch that also tracks master. Alice always commits to alice and Bob always commits to bob. Then when they are happy with their changes they merge into master.

Of course, there is hardly any difference from this and from Alice and Bob working on their local master branches!

The problem often comes in when the same person is working on multiple features at the same time. For example

Alice is working on Feature A and Bob is working on Feature B. Alice is halfway done with Feature A and has made a few commits in alice. However, Feature A is quite hard to implement so Alice decides that she should rather work on Feature C and makes a few commits onto alice. Bob finished Feature B and decides that he would like to tackle Feature A and so pulls alice into bob.

After Bob finishes Feature A he would like to merge bob into master. However, bob now contains Feature A, Feature B and parts of Feature C, but Feature C is not ready to be merged! It's easy to see that a workflow like this can lead to many confusing merge conflicts.

The trick is that instead of having personal branches one should have feature branches. Alice should have a branch for Feature A and Feature C and Bob should have a branch for Feature B and Feature A. That way they both can work on different features without tramping on each other's toes.




回答2:


The idea is that you have a master branch where the code is stable and merged.

Let's say that I have an application that scrapes a website.

I will create a new branch called develop/pulldata. In this branch, I will work on pulling the data. One of my team members could work on applying some logic on the data, so he can create his own branch develop/applylogic.

Once he has a few commits and his code is stable, he can open a pull request to merge his code to master, which you can view in the github UI, to see what code he has changed and decide whether or not you want to merge, or if any changes are required.

Now, after a while, my code is done, I open a pull request to master, so my colleagues can view my code and approve or suggest changes. It's easy to view and everybody in the team knows that master branch is stable. However, other branches do not have to be. They have to be when they are merged to master.




回答3:


Keep it shorter. the master what you have you in the machine is the local master. You push all the changes to origin/master server.

A primary reason why it's not encouraged to work on the master branch is several people work on the project and in which one branch needs to be the common base for all those who created the branch.


For example, you have the product you want to add Bluetooth module to it, then you create a branch add_blueetooth from master when you did then merge that to master. Another guy is working on wifi he can merge his branch into master. It's just the common CVS workflow, where creating branches leads to parallel work.


What really happens when you clone?

Actually, git clone fetches all remote branches it creates one local branch that is the master, for you. Just type this command git branch -a,

git branch -a
* master
  remotes/origin/HEAD

They are other commands where you can see the remote branches using git branch -r.

You can check the reference created by local branch by manoeuvring to this location. refs/remotes/origin



来源:https://stackoverflow.com/questions/44341008/do-we-really-need-to-branch-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!