How to Commit to GitHub?
When working with repositories, it is important to know the basic command lines for manipulating repositories. The following command lines displayed below are only a few of the basic command lines used. Therefore, in this web tutorial, you will learn what it is to push and commit your saved files to GitHub.
Commit vs. Push command
Basically the key difference between these two commands are where they’re saved. When you use
git commit, you only save the changes of the source code/s to your local repository. However, when
git push is used, you save all the changes to the remote repository (online repository).
How to commit to GitHub?
First, open your terminal/command prompt and change the directory to where you have linked your remote repository to your IDE workspace.
[OPTIONAL] You can check the files that you have changed by entering into the command line:
This command will automatically list down the files that you have changed over your progress.
You will see that the codes you’ve changed are in red. This is because you have not committed or pushed any of the files.
Then enter into the command line:
git add .
This command will tell Git that you want to add all of the changed files to the index. The index is a temporary storage. However, when you add the files to the index you are not actually doing anything to the repository itself.
The dot represents all the files that have changed. Therefore,
git add . means that you’re adding all the changed files to the index. You can also choose selected files to be added to index.
Upon entering the command, check for the status of the files added to the index to double check if the files have been successfully added to the index via
As you can see the color of the added files to index have turned to green. This indicates that the transfer of the files from your IDE workspace to the index is successful and are now ready to be saved into your local repository.
Next, enter into the command line:
git commit -m "Insert message here"
This is when you commit your changed files to your local repository. The -m indicated next to the word “commit” stands for “message”. Declaring a message whenever you commit your works to your repository becomes very useful and helpful in the long-run of your progress. Make sure to make the message short but meaningful so you will know what that commit is for. Later on in this tutorial, you will see the message appear in GitHub.
[OPTIONAL] You can also check the history of commits using
As you can see, the newly committed file is now attached to the Head. However, this is still not noted by GitHub in its remote repository. To do this, enter into the command line the final command line.
How to push to GitHub?
git push origin master
This code will then push all of the files located in the local repository to the remote repository in GitHub.
[OPTIONAL] Check if the files have succeeded in pushing to your remote repository by entering
git status or
NOTE: Each time you push progressed files to GitHub, you are creating a so-called “snapshot” of your code. Snapshots are basically save points to your codes.
For a final inspection, you may see your pushed files in GitHub.
These are only some of the basic commands. Also, these commands work best if the master branch is not tampered with by other contributors. Otherwise, you would have to use a force command.
git push origin master -f
NOTE: Only use this command when an error occurs and you are absolutely sure that you want to push the changed files to your remote repository. An error would occur while pushing the changed files if someone has tampered with the master branch. In the case of an error, another alternative is to fetch & rebase the latest commit from master to your local master branch.