Connecting Git and GitHub

Connecting Git and GitHub

Learn how to connect Git and GitHub.

ยท

6 min read

Introduction

In the previous article, you learned how to set up Git and GitHub.

Now that you've logged in and set up Git and GitHub, you can start connecting them.

In this article, I'll teach you how to connect Git and GitHub. You'll use Bakery for this, so please cd into that directory now.

NOTE: You might want to be at a computer or laptop (to get the full experience).

git init

Let's start with git init. Use this command to initialize (or create) a new Git repository.

You, the baker, want to save your changes remotely - just in case something happens to your local machine. After careful thought, you decide to use GitHub to save your changes remotely.

First, verify that you're in the directory that you'd like to start tracking in Git. In this case, use pwd to ensure that you are in Bakery.

Then initialize a new Git repository by simply typing git init into the terminal.

git add

Now that you've created your Git repository, you can start tracking your saved changes using the two-stage commit process.

The two-stage commit process is named so because there are two stages. The first stage involves staging (or readying) your files for a commit. The second stage involves actually committing (or saving) them.

You would use git add to ready your files, and git commit to commit them (as I'll discuss soon).

Use git add like so:

git add <filename>

That would be the simplest use case, but there are many more:

  • to add multiple on the same path, separate each file with a space (e.g. git add file1.txt file2.txt)

  • to stage multiple on different paths, separate each file with a space and call their exact paths

  • to stage all files in a directory, simply use a period in place of <filename>

There's also the option to unstage your changes using git reset, as I'll explain in a future article.

git commit

As I stated earlier, the second stage of the two-stage commit process is to actually commit the files.

You can commit files using git commit, like so:

git commit -m "commit message here"

This commits all staged files and logs your commit (as I'll discuss in the future). Another facet that I'll discuss later on is the possibility of amending your commits, or changing the message you wrote for a previous commit.

Mini-Test: Use what you've just learned to stage all files in Bakery for a commit, then commit them.

Connecting Git and GitHub

Next, let's start saving the Bakery in a remote repository. As I stated in the previous article, saving changes in a remote repository allows for outside interaction. It also gives you a backup repository, in case something happens to your local machine.

NOTE: This section will not cover connecting Git and GitHub in a team. For that, check out this article by @Catalinpit.

First, verify that you're in the directory that you'd like to push to GitHub. In this case, use pwd to ensure that you are in Bakery.

You've already:

  • initialized a new Git repository

  • created files and directories

  • employed the two-stage commit process

Since that's all completed (good on you!), you can move on to the next part - pushing Bakery to GitHub for everyone to see!

As good practice, you should always check the status of the repository before pushing. Typing git status will do that for you. It should output something like this:

nothing to commit, working tree clean

That's a great sign! It means you're ready to push.

If it doesn't, you may not have staged or committed any recent changes to the Bakery repository. You'd want to do this now, before moving on.

Once that's cleared up, navigate to GitHub. You can create a new repository in two ways:

  • clicking the New button on the left sidebar

OR

  • clicking the + on the top-right, then clicking New repository in the dropdown

For now, only name and create the repository - don't worry about anything else. Preferably name it something descriptive, like bakery-project or my-git-bakery.

After you click Create repository, you'll be directed to another page. At this point, make sure that HTTPS is selected (not SSH).

Copy the commands from "...or push an existing repository from the command line" into your terminal, then press the enter key.

Wait for everything to load and update, then refresh the page - your pushed changes should have been updated remotely! ๐ŸŽ‰

Updating Remote Repositories

Let's say that you've made some changes to Bakery locally, and you want to update it remotely.

If you want, you can actually make changes to Bakery now. ๐Ÿ˜‚

To start pushing these changes, first make sure you're in the right directory. Navigate there if you aren't.

Now, complete the two-stage commit process outlined earlier.

Once that's done, simply push them using git push -u origin main, which I'll break down in a future article.

NOTE: It's good practice to push changes when you make them. If you don't want to constantly push changes to your remote (but you still want to save and log them), omit the git push step. After that, you can push whenever you'd like - you've got the power! ๐Ÿ˜

Git vs Bash

The last bit of information to note is that Git is different from Bash.

Although the app is called "Git Bash", there's still a big difference: whereas Bash can be used without initializing a new project, Git can only be used in a new project.

This can be helpful for looking at your past commands or commits.

To view a previous command in Bash, type history anywhere in the terminal - even in a Git project!

To find a previous commit in Git, you have to be in a Git-tracked repository first. Once there, type git log to see your previous commits.

Conclusion

In this article, you learned some commands to help you connect Git and GitHub, like:

- git init (used to initialize a new Git-tracked repository)
- git add (used to stage files for a commit)
- git commit (used to commit files)

You also pushed your local repository Bakery to your remote repository on GitHub! ๐ŸŽ‰

Great job on completing the fifth part of the series!

This is the 5th part of a 10 part series to teach Windows users about Git Bash. This series includes:

  • how to install Git Bash

  • how to navigate your file system

  • how to change your file system

  • how to set up Git and GitHub

  • how to keep up good practices in Git

  • how to backtrack in Git

  • how to branch in Git

  • ...and more!!

Stay tuned for more - a new part will come out every Tuesday! ๐Ÿ˜Š

ย