← Back to Git & GitHub

03. Git Commands

Remote & Local

#1Remote & Local Repositories

The GitHub repo we created is empty and sad right now, so let's work on adding some files. Think of something that you can share online, it could be some code (like a .html, .css, .js, .py, .cpp, .java file) from one of our courses, a personal project, or a homework assignment.

To get our code online, we have to work with two types of repos that connect with one another:

- ā˜ļø Remote repository: a GitHub repo stored somewhere on the internet (for the latest version) - šŸ” Local repository: a Git repo that lives on your own computer (for drafts)

We have the empty remote repo on GitHub, so now we need a local repo. Let's go over some basic Git terminal commands to create a local repo and connect it to the remote repo.

#2Git Init

The git init command initializes a new Git repo (local repo). It's the first command you run when you have a new project and want to start tracking changes. The history of your project starts here!

bash
git init

#4Understanding Git Init

When you run this command, Git will create a new directory named .git in your project folder. This directory contains all the information about your project's history and configuration.

The terminal will return something like: Initialized empty Git repository in /Users/username/Desktop/python/.git

Think of this like turning a normal computer folder into a Git-tracked project.

#5Git Remote

Local repo? Check. Remote repo? Check. Now, we can connect our local repo with our remote repo.

The git remote command manages connections to remote repos. We can add a connection by:

bash
git remote add origin <repository_url>

#7Understanding Git Remote

The command breakdown: - add: Add a new remote connection - origin: Give a nickname for the remote repo's URL (origin is a common one) - <repository_url>: Placeholder for the remote repo's URL

Example: git remote add origin https://github.com/codedex-io/first-repo.git

Here we're telling Git: "Yo, connect my local repo to the first-repo remote repo, and I'll call the URL origin."

Now, we have our local and remote repos connected!

#8Git Branch

Let's now rename the branch to main. This will be the branch we push code to, and will be default branch.

bash
git branch -M main

#10Understanding Git Branch

The uppercase -M flag means "move" (or "rename"). We'll discuss what branches are later in the chapter.

To verify everything is set up correctly, you can run: git branch

If you see main in the output, you're all set!