Using Git to implement a new feature/change without affecting the master branch

Using Git to implement a new feature/change without affecting the master branch

Congratulations, you've written your first project! Now what? It does almost everything you need it to do except one thing. That one thing requires some strategic code changes and if you break something you might not be able to fix it. The product has already shipped and the code needs to remain pristine but you've already promised to add that feature.

Let's assume that this project is being tracked by Git and the main branch is called master and won't be changed during your feature development.

The first thing to do would be to branch off of master and make a feature branch. This way if anything goes wrong, you can start over and try again.

The git plan

To create a new branch issue this in the git project root:

$ git checkout -b feature-branch

What this command does is it creates a new branch called 'feature-branch' and the '-b' argument automatically checks out your new branch.

This branch is a snapshot of your master at that current date and time. Any changes to master will not be reflected in this branch and any changes in your new branch will not be reflected in the master.

You can verify which branch you're on by using:

$ git branch

This will show an asterisk '*' beside the currently checked out branch. Something like this:

* feature-branch
master

To switch between the two, use:

$ git checkout <<branch>>

At this point you can make changes to your code using your favourite IDE. Perhaps you've done your work for the day and wish to save your progress, there's a couple steps that you can take at this point to commit your changes to the new branch.

To see the status of your files issue this command:

$ git status

This will show you created/changed/deleted files. Sometimes you don't want all files or wish to only add certain files to your commit.

To do so use:

$ git add <<path and name of file>>

If you want to add all created/changed/deleted files you can use:

$ git add .

Running `git status` again will show you a list of changes to be committed.

From here, you can commit your code changes using:

$ git commit -m "Feature: Added awesome feature"

The '-m' argument lets you specify the commit message on the command line otherwise you would be prompted in a text editor to enter the commit info.

After much hard work and long programming evenings, the feature is finally ready. The next steps would be to go back to the master branch and merge in your feature branch into it.

First you would checkout master again:

$ git checkout master

Then you would merge in your feature branch:

$ git merge feature-branch -m "Feature: Added cool feature"

At this point you've updated the your master branch with the latest feature and joy is in the world.

However, there can be other nuances in code development that might get missed.

If the master branch has changed in any way or has conflicting files you may need to rebase and resolve those issues before being able to add your features.

Perhaps all code needs to go through user peer review through pull requests and/or automated testing to ensure your new feature does not interfere with existing features, perhaps code styles or security checks need to be done - this is typically done in a CI/CD cycle; but those are blog topics for another day. ;)

To view or add a comment, sign in

More articles by Michel Noel

Explore content categories