Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

branch management

Branch commits in Git

Dale Sande

When I am working on a new feature branch, I commit often. This allows me to move forward without fear that if something goes wrong, or I have to back out for some reason, I don’t lose too much work. Think of committing as that save button habit you have so well programmed into you.

Each commit also tells a little bit about what was just worked on; this is important when other devs on the team are reviewing my code. It’s better to have many commits with messages that explain the step than it is to have one large commit that glosses over important details.

Commit your code

All the changes I am creating in my project are unseated updates. With each commit, there will most likely be additions and deletions from time to time. To get a bearing of the updates I have made, let’s get the status:

$ git status

This command will give you a list of all the updated, added, and deleted files.

I can add files individually or I can add them all at once. From the root of the project I can use:

$ git add .

In order to remove deleted files from the version control, I can, again, either remove them individually or remove them all from the root address like so:

$ git add -u

I’m lazy, I don’t want to think, so I make heavy use of the following command to address all additions and deletions.

$ git add --all

All the preceding commands will stage the updates for commitment. If I run a git status at this point, I will see my updates presented differently (typically under the heading of Changes to be committed:). At this point, the changes are only staged and not yet committed to the branch. The next step is to commit with a message. Here is where I lean on the Angular style commit messages I linked to above. To commit, do the following:

$ git commit -m "<type>(<scope>): <subject>"

It is considered best to illustrate your comment in the tense that this will do something to the code. It didn’t do something in the past and it won’t do something in the future – the commit is doing something now.

A bad subject example would be:

$ git commit -m "fixed bug with login feature"

A good subject example would be:

$ git commit -m "update app config to address login bug" 

Comments are cheap. For more on how to write expressive commit messages, read 5 Useful Tips For A Better Commit Message.


branch management

View all Courses

Keep Exploring