Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the best moment to create a new git branch... before writing new code or after? [closed]

Tags:

git

Should I create a new branch when I am already in master and I've begun putting some new code in? Then do I create the new branch and commit on it?

Or do I create a fresh new branch from a clean master and start working on the new code?

I know this is probably simple... but I am only now getting the hang of git and I just want to iron out a few specifics in my workflow.

like image 208
default_noob_network Avatar asked Sep 23 '15 03:09

default_noob_network


People also ask

When should I create a new branch in git?

You should create a new branch when you're doing development work that is somewhat experimental in nature. So in your scenario definitely create a new branch and not a folder within master. If you created your sandbox work as a directory in the master, it's going to reside there until you remove it using git.

Should I create new branch every time?

It's a good practice to create a new branch for every new bit of work you start doing, even if it's a very small one. It's especially useful to create a new branch for every new feature you start working on. Branches are of course disposable, you can always remove them.

Where should I create a new branch in git?

The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Next, you just have to specify the name for the branch you want to create. To achieve that, you will run the “git checkout” command with the “-b” option and add “feature” as the branch name.

How do I create a new branch in git?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.


1 Answers

Should I create a new branch when I am already in master and I've begun putting some new code in? Then do I create the new branch and commit on it?

Technically, it doesn't matter when the branch creation and switch occurs - as long as it is before the commit1.

None of the working copy changes will be reverted/modified/deleted when creating a new branch off the current ('master') branch and switching to it by-and-by.

Or do I create a fresh new branch from a clean master and start working on the new code?

Regardless, I recommend creating the branch before. It gives clearer focus to the work being done and it becomes a consistent habit that makes it 'harder to forget' and have to tidy up afterwards.


1Even then it doesn't 'really' matter before the commit is shared as the commits can be tidied up and rebased etc. without affecting anyone else - but it's a bit more awkward in the workflow. I like to think of Git as a mutable-immutable design: commits that only you have (ie. have not pushed) are mutable; and every pushed and remotely merged commit should be left well-enough alone.

like image 88
user2864740 Avatar answered Sep 30 '22 20:09

user2864740