So, I'm fairly new to git and I've after a bit of reading around over the last couple of weeks I've read a few people saying that the master branch shouldn't be changed but rather branched from and then merged to.
I'm happy enough to work with branches but was wondering for the reasons behind not working on the master branch?
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.
Work should never be done in this branch. Instead of working in master, changes should be merged in via Pull Requests from other feature branches or from a fork. To this end, you should protect the master branch to encourage code reviews before changes are merged and deployed.
In order to switch to the master branch, on this specific commit, we are going to execute the “git checkout” command and specify the “master” branch as well as the commit SHA. In order to check that you are correctly on a specific commit, you can use the “git log” command again.
Other people have made very good cases for not making changes directly in master
, and I agree with them. However, always advocating workflows like this sometimes leads people new to git to believe it is needlessly complex, so I wanted to provide a counterpoint.
If you have a one-person or very small team and your development is highly linear, i.e. you rarely work on more than one thing at a time and each feature is usually completed before starting the next, there really is little to no benefit to not work directly out of master
. It is very easy to go back and add a branch at any point should the need arise. I highly recommend being aware of the feature branch workflow, but if you feel in your case it's just adding extra steps without buying you anything, I promise I won't tell the git police.
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