In each manual and documentation about Git you can see the single advise - "Do not commit to master". So, if you need to add some changes to master, you need to create a new branch and merge it. Therefore, I interesting to know why? Which advantages it this behavior? For example, you have no need in a separate branch if you wanna to revert a change - you can do it using a commit hash.
Here I found a one reason - if you have many commits, it will be easyer to merge branch with master then push commits one after another http://waterstreetgm.org/git-why-you-should-never-commit-directly-to-master/
But what if your workflow is divided into many small tasks and each of this tasks fit to the one commit. So, each branch contains one commit. What are the reasons not to commit to master in this case?
You don't even need that first commit to master . Remember that master isn't anything special to git, it doesn't need to be there. You can just have a development branch until you want to make a release.
To find it go to Settings > Branches > Branch Protection Rules and click 'Add Rule'. Then, enter the name of the branch you want to protect and click the checkbox to require pull request reviews before merging.
Working directly in master means that if you create bugs you have no other option for "going back" than to reverse/delete/reset commits, which is not a clean way of working and can cause you to lose the parts of the new code that were OK.
Those who said 'never' or 'always' (like in the accepted answer!?!) are often (always!?! ๐ ) wrong...
โ Like you will discover once you understand git, there are often multiple ways to do the same things and your choices will mostly be team choices.
There is absolutely no rule that prevents to commit on master
. There are advantages and disadvantages to the 2 ways of doing.
For example, the post you linked is more a problem that the writer didn't master git enough than something else...
In less than 5 minutes, he could have created its new branch and reset its local master
to the remote ref and pushed one commit on master
branch.
Here is a list that is not exhaustive...
Using feature branches (also known as GitHub flow
) :
๐ Pros:
๐ Cons:
rebase
just before the merge
could make the history easier to read)Using "trunk Based development" (and feature toggles
. Real good development practice that you should have a look) :
๐ Pros:
๐ Cons:
Choose the one that suits your team and stick to it most of the time and apply the other when you think it makes more sense and solve a new problem you encountered...
A very good, long and complete article on the subject by Martin Fowler: https://martinfowler.com/articles/branching-patterns.html (ยง Comparing Feature Branching and Continuous Integration
. But you will have to read nearly all to get it well...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With