Lesson 3-3

Git Commit & Git Push - What is it/How to do it

git commit slingshot

Have you ever told someone that you will do something, and actually followed through with it? Maybe like saying you'll hang out soon and then the next weekend you go to the movies? Those are examples of commitments, and with your repository you will do the same.

Git Commit is a command used to stage changes to be pushed to the GitHub Cloud. When you commit something, you are getting it ready to be put up on GitHub and update all other locations with your new changes.

Commits are very critical to the communication and stages of a project. Here's how:

Commit Messages

Commit messages are text-based messages that you write outlining the changes that you made in a project at a certain commit. These messages are read by not only partners on a project but also senior management and/or professors. It is also important to make good commit messages because someone coming from an outsider's perspective will use your commit messages to get an idea of how you solved a problem, or did something.A general rule of thumb for commit messages is as follows:

  1. The message should be at least 20 characters in length, ideally more. This would equate to a few words. DO NOT MAKE EMPTY COMMITS!
  2. The message should describe, in detail, what you did. For example, if you made a function that added two numbers that wasn't there before, you could say "created a function that adds 2 inputted numbers"
  3. Use proper grammar and spelling.
  4. Does NOT include code snippets in the message itself. This is bad practice as no one will understand what the code is supposed to do.
  5. Overall readable by any person at any time. Does not require context.

Now there are some times when you are doing one line changes and then committing them. For example you switched a + for a - or you added css styling to one part of a webpage. Depending on your organization, you can do simple 1 word commits, but I strongly recommend that you still 'commit' (sorry for the cheesy joke there) to writing messages based on the aforementioned rules.

Since this site is on a open-source repository, I can show you my commit history for this site. You can view the commit history here. git commit slingshot

The final step to upload files to the GitHub Cloud is git push. This command will send the updated version of files and folders to the GitHub Cloud from your local machine or server, depending on where you initiate it. It will also make note of the commit message that you thoughtfully wrote and display it on GitHub.com in each iteration of a file. Git Push is like you send the rock from the slingshot towards a target (hopefully not another living thing).

Pushes are the reason that pulls are necessary. When someone does a push, a pull is needed to maintain consistency in the repository's files.

Now then, let's see how we can commit and push files.

GitHub Desktop

  1. Open GitHub Desktop and select your current repository.
  2. If you have changes to commit, you should see something like Figure 3F. If not, wait until you make changes, and then check back.
  3. commit window before typing message Figure 3F: The pane to the right shows the additions you made in green. It shows deletions in red.
  4. To Commit, type in a brief message in the top box, and a longer description in the bottom box. Click the "Commit to main" button at the bottom. (see Figure 3G)
  5. commit window after typing message Figure 3G: Writing commit title and message.
  6. Finally, to push, just click the "push origin" button at either location. (see Figure 3H)
  7. commit window before typing message Figure 3H: About to push the changes to the GitHub Cloud.
  8. One done, it should go back to normal, like in figure 3A from the previous page.

Terminal

  1. Open terminal and navigate to your repository via the cd command.
  2. Use git status to see if there are changes to commit. If there are, it will look like figure 3I.
  3. git add in terminal Figure 3I: This is the message that should pop up if there are changes to be made. It will look different depending on what changes you made.
  4. To make a new commit, type in git add --all which will add all files to the commit stage. Hit enter when done. Note: it won't say anything back once you do this, and that is normal. (see Figure 3J)
  5. git commit in terminal Figure 3J: After doing git add --all you should see nothing happen, just a new line for a new command to be entered.
  6. Now, to add a message to our commit, type in git commit -m "yourMessageHere" and click enter. (See Figure 3K).
  7. git commit with message in terminal Figure 3K: Git will output some jibber jabber about the changes you made, similar to a pull.
  8. Finally, to push, type in git push and click enter. (see Figure 3L)
  9. git add in terminal Figure 3L: After pushing, more jibber jabber comes out but the deed is done if no errors are thrown.

I know that was a lot of information for lesson. On the bright side, that is all there is to pulling, committing and pushing. Let's take a breather and come back and review later.