Git Commit: How to Commit Code Changes to GitHub?

Imagine your hours' worth of code disappearing in seconds. 😱 Stop this nightmare from becoming reality, commit your code to GitHub. Here's how you can do it.

How to Commit Code Changes to GitHub?

Every developer’s nightmare is most likely to have hours' worth of code disappear in seconds. And sometimes this nightmare does come true. Scary, isn’t it? This is where a frequent code commit can come to your rescue.

But before we jump to the ‘how to use it’ part, let’s have a look at how code commits work at a high-level.

Here’s what we’ll cover in this article:

  1. How Commit Command in GitHub Works
  2. How to Commit Code Changes to GitHub

How Commit Command in GitHub Works

GitHub (and Git) enables you to create branches replicating the original code base. And to these branches, changes are made and committed to be merged with the original code base.

If you’re not familiar with Branches in git, here’s an article that covers how to create a new branch.

Every time you enter the git commit command in the terminal, snapshots of the repository are captured at that particular point in time. But how will git know what changes to save?

For this purpose, the git add command followed by the names of the files you made changes to and wish to commit are entered in the git terminal.

git add file 1 file 2

That is, using this command includes the files whose changes need to be added to the next commit in the staging area.

Then, using the git commit command, git is instructed to capture snapshots of these explicitly mentioned files at those specific times.

But if you use the GitHub Desktop app, this whole process is simplified to a few clicks. I’ll show you how to do this shortly.

These snapshots or milestones are saved as ‘save points’ in the local repository, which can be later merged with the central repository.

Tip: Make regular commits to enable your teammates to view the history of the changes made to the repository and review them so that a stable version of the code gets deployed.

Now that you know how the commit command works in git, let’s get to the actual part - the implementation.

How to Commit Code Changes to GitHub

Before You Begin: Make sure you stage the files with changes you want to commit.

Also, ensure that you have the right branch selected to make the commits to. You don’t want to touch that Master branch and make commits to it until you’re a hundred percent sure, trust me.

You can commit to GitHub in two ways:

  1. Using the GitHub Desktop App
  2. Using the Terminal Code

Let’s see how you can do that using both, shall we? :)

Committing to GitHub Using GitHub Desktop

Committing to git is incredibly easy using the GitHub desktop app.

Once you’ve made the changes to your files in your text editor and saved them, open your GitHub desktop app. You will be able to view the changes made to the repository and the repository history.

Make sure you have selected the branch you wish to commit the changes to.

Now, enter a very short description of the commit so that you and your team members are on the same page without any confusion. Click on the Commit to <your branch name> button.

Commit Button in GitHub Desktop App
Enter a name for the commit and click the Commit button to commit to GitHub

Finally, click the Push Origin button to merge all your commits from the local repository to your central repository or code base and keep it up-to-date for the rest of your team to access. It really is that simple. :)

Push Origin Button in GitHub Desktop App
Click the Push Origin button to merge commits from your local to central repository 

Using Terminal to Commit to GitHub

You’ve got a few options to use this git commit command in the terminal code. Let’s have a look at them one by one.

$ git commit -a

This will initiate the commit process to commit all changes in your current directory. But you will need to enter the commit message (i.e. the short description about the commit) separately. Your default text editor opens automatically on executing this command where you can enter the commit message.

$ git commit -m “<commit message>”

If you wish to avoid an external prompt to enter the commit message while committing the desired changes to your local repository, use this command.

$ git commit -am “<commit message>”

Running this command will commit all changes in the repository you’re currently working in, skipping the staging process, all the while including the descriptive commit message.

$ git commit --amend “<commit message>”

With the help of this command, you can rewrite the last made commit with the latest staged changes (as a new commit).

Since these commits are made to the local repository, you must publish them to reflect them in the remote origin. This way all your teammates will be able to access the latest version of the code base. To do this, run the following command:

$ git push <remote> <branch-name>

Here’s an article you might be interested in: 13 basic git commands every developer must know.