Home » Git » How to commit and push in git

How to commit and push in git

By Emily

Let’s face it we all find it hard to commit sometimes. But that would be a different post entirely 😉 For now, I’ll write about how to commit and push in git. If you’re not a software developer and you’ve somehow stumbled on this post, then you’re probably wondering what I’ve been taking! However if you know the terms git commit and git push, but you’re not sure what they mean exactly, then this post is for you. I’ll outline the process, and what each stage does. I’ll also provide some git commit message examples.

A day in the life of a coder

In the normal course of a day in the life of a coder you’ll have pulled down the most up to date version of the code for the app that you and your team are working on. Then you will create a feature branch, switch to that git branch, and start writing your code.

Once you’ve finished writing the code for this shiny new feature, and you’re ready to submit your code back to the central repository you’ll need to follow this process.

Step by step – the process

  1. First you need to add all of the changed files to the staging area – this is called staging your changes. Think of this as packing a box with all the items for an order.
  2. Next you need to commit the staged files. Think of this as wrapping the box and taping it shut so it’s ready to post.
  3. And now you will push the files up to the central repository. Think of this as posting the box to the recipient.
  4. Finally you need to raise a pull request. Think of this final stage of the box being delivered to the recipient.

Now that I’ve explained the stages involved, how do you actually do this using git commands?

How to git commit and push – commands

Step 1 – stage all the files

git add .

Step 2 – commit the changes

git commit -m "Your commit message goes here" 

Step 3 – push the files up to the central repository

git push

More about the git commit message

When you commit code, you write a commit message which describes the purpose of the code you’re committing.

If you work for a company as part of a larger development team then your commit messages will probably need to follow some kind of convention. Perhaps they need to include the bug fix JIRA ticket reference -> git commit -m "Fixed JIRA-456".

If you’re an indie hacker then you’re commit messages will probably say something like git commit -m "Fixed it", or git commit -m "No more bugs".

Commit messages will help the person reviewing your PR understand what your intention was. You can tell them if you fixed a bug, added a new feature, or improved the comments.

So, what’s next

Now you’re familiar with the git commit and push process, you’ll need to know how to view unpushed commits, and how to view your last commit. If thing’s have gone really badly then you’ll probably also want to read about how to undo your last commit 🙂