Lesson 32 of 60 – Connecting Local Git to GitHub
53%

Connecting Local Git to GitHub

Connecting a local Git repository to GitHub allows you to synchronize your local project with a remote repository. After the connection is created, you can push your commits to GitHub and retrieve changes from the remote repository.

Note: The most common remote name is origin. You can connect a local repository to GitHub using the Git remote command.

1. What Does Connecting Git to GitHub Mean?

A local Git repository exists on your computer. A GitHub repository exists on a remote server. Connecting them creates a remote reference between the two repositories.

Local Git Repository
        ↓
      origin
        ↓
GitHub Repository

2. Why Connect Git to GitHub?

Connecting your local repository to GitHub allows you to work with a remote copy of your project.

  • Upload project changes
  • Download remote changes
  • Back up project history
  • Collaborate with developers
  • Use GitHub pull requests
  • Share project code

3. Requirements Before Connecting

Before connecting a local project to GitHub, you normally need:

  • Git installed on your computer
  • A GitHub account
  • A GitHub repository
  • A local project
  • A local Git repository

4. Check Git Installation

First, verify that Git is installed:

git --version

Example output:

git version 2.x.x

The exact version depends on the Git installation on your computer.

5. Open Your Project Folder

Open the terminal or Git Bash and move into your project directory.

cd my-project

You can verify the current location using:

pwd

6. Initialize Git

If the project is not already a Git repository, initialize it:

git init

This creates the .git directory and turns the project folder into a Git repository.

7. Check Git Status

After initializing Git, check the repository:

git status

This shows the current branch and the state of your project files.

8. Add a .gitignore File

Before committing project files, create a suitable .gitignore file when necessary.

For example:

.env
node_modules/
*.log

This helps prevent files that should not be tracked from being added to the repository.

9. Stage Project Files

Add the project files to the staging area:

git add .

You can check the staged files with:

git status

10. Create the First Commit

Create a commit containing the staged project files:

git commit -m "Initial project"

The commit creates a saved point in the local Git history.

11. Create a GitHub Repository

Create a repository on GitHub for the local project.

For an existing local project, it is often convenient to create the remote repository without adding files that would conflict with the existing local history.

12. Copy the GitHub Repository URL

After creating the GitHub repository, copy its repository URL.

HTTPS example:

https://github.com/username/my-project.git

SSH example:

git@github.com:username/my-project.git

13. Add the GitHub Remote

Use git remote add to connect the local repository:

git remote add origin https://github.com/username/my-project.git

Here:

  • origin is the remote name.
  • The URL identifies the GitHub repository.

14. Verify the Remote

Verify that the remote was added correctly:

git remote -v

Example:

origin  https://github.com/username/my-project.git (fetch)
origin  https://github.com/username/my-project.git (push)

15. Check the Current Branch

Check which branch you are currently using:

git branch

If your branch is named main, you can push it using:

git push -u origin main

16. Rename the Branch to main

If your local branch has another name and you want to use main, you can rename it:

git branch -M main

Then push it to GitHub:

git push -u origin main

17. First Push to GitHub

The following command pushes the local main branch to GitHub:

git push -u origin main

The -u option sets the upstream relationship between the local branch and the remote branch.

18. Future Pushes

After the upstream relationship has been configured, future changes can commonly be pushed using:

git push

A typical workflow is:

git add .
git commit -m "Update project"
git push

19. Pulling Changes from GitHub

If the remote repository has changes that you need locally, you can retrieve and integrate them with:

git pull origin main

If the upstream branch is configured, you can often use:

git pull

20. Fetching Changes

You can download information from GitHub without directly integrating the changes into your current branch:

git fetch origin

After fetching, you can inspect the remote-tracking branches before deciding how to integrate the changes.

21. HTTPS Remote

An HTTPS remote commonly looks like:

https://github.com/username/project.git

HTTPS is one way to configure Git operations with a GitHub repository. The authentication process depends on GitHub's current authentication requirements and your local credential configuration.

22. SSH Remote

An SSH remote commonly looks like:

git@github.com:username/project.git

SSH can be used for Git authentication after an SSH key has been properly configured with GitHub.

23. Changing an Existing Remote URL

If the remote URL is incorrect, you can change it:

git remote set-url origin https://github.com/username/new-project.git

Then verify the new URL:

git remote -v

24. Multiple Remotes

A local repository can have more than one remote.

git remote add origin https://github.com/user/my-project.git
git remote add upstream https://github.com/company/my-project.git

This can be useful when working with a fork and an original project repository.

25. Authentication When Pushing

When pushing to GitHub, GitHub must be able to authenticate the Git operation.

Depending on your setup, this may involve credential management, authentication through HTTPS, or SSH keys.

Important: Never place passwords, personal access tokens, or private SSH keys inside your source code or commit them to a repository.

26. Common Connection Errors

Common problems when connecting a local repository to GitHub include:

  • Incorrect repository URL
  • Remote name already exists
  • Authentication failure
  • Wrong branch name
  • Repository access permissions
  • Remote repository already containing unrelated commits

Use commands such as git remote -v, git status, and git branch to inspect the local configuration.

27. Complete Local-to-GitHub Workflow

cd my-project

git init

git add .

git commit -m "Initial project"

git branch -M main

git remote add origin https://github.com/username/my-project.git

git remote -v

git push -u origin main

This is a common sequence for publishing an existing local project to a newly created GitHub repository.

28. Daily GitHub Workflow

git pull

# Make changes

git status

git add .

git commit -m "Update project"

git push

This workflow can be used to retrieve remote changes, make local changes, commit them, and push the new commit to GitHub.

29. Connection Checklist

  • Install Git.
  • Create or open your local project.
  • Initialize the repository with git init if necessary.
  • Check the repository using git status.
  • Commit the project files.
  • Create the GitHub repository.
  • Copy the correct GitHub repository URL.
  • Add the remote using git remote add origin URL.
  • Verify the remote using git remote -v.
  • Push the correct branch.

30. Summary of Connecting Git to GitHub

Connecting a local Git repository to GitHub creates a remote relationship that allows your project to communicate with the GitHub repository.

Local Project
     ↓
git init
     ↓
git commit
     ↓
git remote add origin URL
     ↓
git push
     ↓
GitHub

The most important command for creating the connection is:

git remote add origin https://github.com/username/project.git

📌 Key Points

  • A local Git repository can be connected to a GitHub repository using a remote.
  • git init initializes a local Git repository.
  • git add . stages project files.
  • git commit records changes in local Git history.
  • git remote add origin URL adds the GitHub remote.
  • git remote -v verifies the configured remote URL.
  • git branch -M main can rename the current branch to main.
  • git push -u origin main pushes the first main branch and sets upstream tracking.
  • git push can be used for later pushes after upstream tracking is configured.
  • git pull retrieves and integrates remote changes.
  • git fetch retrieves remote information without directly integrating it.
  • HTTPS and SSH are common remote URL methods.
  • Never commit passwords, tokens, or private keys.

🧠 Quick Quiz

Question: Which command connects a local Git repository to a GitHub repository using the remote name origin?