Skip to content

Latest commit

 

History

History
160 lines (107 loc) · 4.31 KB

File metadata and controls

160 lines (107 loc) · 4.31 KB

How to Connect Local Git to GitHub Repository

Step-by-Step Guide

  1. Create a Repository on GitHub

    • Go to GitHub.
    • Click on the "+" icon in the top-right corner and select "New repository".
    • Enter a repository name, description (optional), and choose visibility (public or private).
    • Click on "Create repository".
  2. Initialize Local Repository

    If you haven't already initialized a local repository, do so with the following commands:

    git init my_project
    cd my_project
  3. Add a Remote Repository

    After creating the repository on GitHub, you will get a URL (e.g., https://github.com/username/repository.git or git@github.com:username/repository.git). Add this as a remote to your local repository:

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

    Verify the remote:

    git remote -v

What is the Use of a Central Repository?

A central repository serves as a shared hub where multiple collaborators can push their changes and pull updates from. It enables collaborative development by providing a common place to synchronize work, facilitating version control, backup, and collaboration.

How to Run git push Command

Step-by-Step Example

  1. Initialize Local Repository and Make Initial Commit

    git init my_project
    cd my_project
    echo "Initial content" > file1.txt
    git add file1.txt
    git commit -m "Initial commit"
  2. Add Remote Repository

    git remote add origin https://github.com/username/repository.git
  3. Push to Remote Repository

    git push -u origin main

    This pushes your local main branch to the origin remote. The -u flag sets the upstream tracking for the main branch.

How to Run git pull, git clone, git push

Step-by-Step Examples

  1. git clone

    Cloning a repository means creating a local copy of a remote repository.

    git clone https://github.com/username/repository.git

    This command creates a directory named repository and initializes it with the contents of the remote repository.

  2. git pull

    Pulling changes from a remote repository updates your local repository with changes from the remote. It fetches the changes and merges them into your current branch.

    git pull origin main

    This command fetches and merges changes from the main branch of the origin remote repository into your current branch.

  3. git push

    Pushing changes uploads your local commits to a remote repository.

    git push origin main

    This command pushes the main branch to the origin remote repository.

Real-World Example: Full Workflow

  1. Create a GitHub Repository

    Follow the steps to create a new repository on GitHub.

  2. Clone the Repository Locally

    git clone https://github.com/username/repository.git
    cd repository
  3. Make Changes and Commit

    echo "New content" > file2.txt
    git add file2.txt
    git commit -m "Add file2.txt with new content"
  4. Push Changes to GitHub

    git push origin main
  5. Pull Changes from GitHub

    Make some changes directly in the GitHub repository (e.g., edit a file via the GitHub interface) and then pull those changes:

    git pull origin main

Summary

  • Connecting Local Git to GitHub:

    1. Create a repository on GitHub.
    2. Initialize a local repository.
    3. Add the GitHub repository as a remote (git remote add origin <URL>).
  • Use of Central Repository:

    • Facilitates collaborative development by providing a shared hub for synchronization, version control, and backup.
  • Running Git Commands:

    • git push: Push local changes to a remote repository.
      git push origin main
    • git pull: Fetch and merge changes from a remote repository.
      git pull origin main
    • git clone: Create a local copy of a remote repository.
      git clone https://github.com/username/repository.git

These steps cover the basics of connecting to a remote repository, cloning, pulling, and pushing changes, allowing for effective collaboration and version control in software development.