Introduction to GitHub: Part 2

The previous GitHub tutorial covered what a repository is, how to create one, and how to commit your first changes. You also got hands-on with GitHub Desktop and cloned your repository to your local machine.

Let’s start with Part 2, where we’ll focus on new topics like committing changes locally and pushing the changes to GitHub.com.

Commit Locally and Push to Remote

Step 1: Start by adding a file to the local repository. For example, add firstfile.html to your local repository.

Step 2: Open GitHub Desktop. You should see the new file shown as waiting to be committed.

Enter a comment describing the commit, such as Commit 1st File.

Step 3: Next, click Commit to main.

Step 4: After committing the changes, click Push origin on GitHub Desktop to upload your changes to the remote repository.

Step 5: Click View on GitHub on GitHub Desktop. This will open the remote repository on GitHub.com, where you can check if firstfile.html is listed under the repository files.

Revert a Commit

Reverting a commit means reversing the changes introduced by the commit. Here’s how to do it with GitHub Desktop:

Step 1: Click History to see the repository’s commit history.

Step 2: Right-click a commit and select Revert changes in commit. In this case, we’ll revert the commit of firstfile.html.

When you revert a commit, it creates a new commit. Desktop indicates that the revert hasn’t been pushed to the remote repository on GitHub.com.

Step 3: Click the Push option under Repository to push the revert commit to the remote repository on GitHub.com.

As show in the screenshot:

  • firstfile.html now doesn’t exist on GitHub.com.

  • The latest commit message Revert “Commit 1st file” indicates that the revert was carried out.

In addition, firstfile.html isn’t there in the local repository.

Amend a Commit

You can amend a commit that exists in your local repository and hasn’t been pushed to a remote repository.

As a scenario, we added secondfile.html to the local repository. GitHub Desktop shows that the file is ready to be committed.

Click Commit to main.

Here’s how to amend this commit:

Step 1: Click History.

Step 2: Right-click the commit and select Amend commit.

Step 3: In this case, we’ll change the commit message from Create secondfile.html to Create a 2nd HTML file.

Step 4: Click Amend last commit.

Pull Changes from Remote Repository

When we do a pull, GitHub Desktop pulls any changes in the remote repository to the local repository.

To illustrate this, add thirdfile.html to the remote repository on GitHub.com.

To do a pull:

Step 1: Open GitHub Desktop. It indicates there has been a commit to the remote repository that doesn’t exist in the local repository.

Step 2: Click Pull origin.

thirdfile.html now exists in the local repository.