GIT : Branch and Merge

GIT : Branch and Merge

Best practice in GIT is to create a "feature branch" for each new feature, bug fix, or enhancement we have to code.

Each branch should contain only the commits related to its particular feature. This guarantees smaller and more focused commits.

To create a new branch, we can use the command

git branch newbranch

Same command without arguments lists instead all existing branches. An asterisk marks the active branch.

git branch
* master
newbranch

Asterisk says we are still located on master. To start working on the new branch we need to run

git checkout newbranch

Thus "newbranch" becomes the active branch.

...........

Now we can start developing our feature/bugfix, committing to "newbranch"

...........

When "newbranch" is ready to be merged into master we run

 git checkout master

to change the active branch back to master.

THIS IS VERY IMPORTANT : we need to position ourselves on the branch that we want to merge into.

Finally we run

 git merge newbranch

to merge "newbranch" into the master branch.

Finished: new feature commits are now part of the master branch ( if no merge conflict is found , but this is another thrilling story)