Books/Git Essentials/Working with Remote Repositories

    Working with Remote Repositories

    Working with Remote Repositories

    So far, everything has been on your local computer. Now it's time to learn how to push your code to the cloud (GitHub) and pull changes from it.

    What is a Remote?

    A remote is a copy of your repository hosted on a server like GitHub. Think of it as a cloud backup that you and others can access.

    Your Computer (local)  ←→  GitHub (remote)
         git push  →              stores your code
         git pull  ←              sends updates to you
    

    Setting Up a Remote

    Option 1: You Cloned the Repo

    If you used git clone, the remote is already set up. Check it:

    git remote -v
    origin  https://github.com/username/repo-name.git (fetch)
    origin  https://github.com/username/repo-name.git (push)
    

    The name origin is the default name for your remote. Think of it as a nickname for the GitHub URL.

    Option 2: You Started Locally with git init

    If you created a local repo with git init, you need to connect it to GitHub:

    1. Create a new repository on GitHub (don't initialize with README)
    2. Add the remote:
    git remote add origin https://github.com/username/repo-name.git
    1. Push your code:
    git push -u origin main

    The -u flag links your local main branch to the remote main branch. You only need it the first time.

    What to ask your AI: "I have a local Git project and I just created an empty GitHub repo. Can you help me push my code to GitHub?"

    Pushing Code to GitHub

    After making commits locally, send them to GitHub:

    git push

    That's it! Your commits are now on GitHub.

    If it's a new branch that doesn't exist on GitHub yet:

    git push -u origin branch-name

    What Can Go Wrong?

    If someone else pushed changes that you don't have locally, Git will reject your push:

    ! [rejected] main -> main (fetch first)
    

    Solution: pull their changes first, then push.

    Pulling Changes from GitHub

    Get the latest changes from GitHub:

    git pull

    This downloads any new commits from the remote and merges them into your local branch.

    A Typical Daily Workflow

    # 1. Start your day — get the latest code
    git pull
    
    # 2. Create a branch for your work
    git checkout -b feature/my-task
    
    # 3. Work, commit, repeat
    git add .
    git commit -m "Add new feature"
    
    # 4. Push your branch to GitHub
    git push -u origin feature/my-task
    
    # 5. When done, merge to main (often done via a Pull Request on GitHub)

    GitHub Basics

    Creating a Repository on GitHub

    1. Go to github.com and sign in
    2. Click the + button → New repository
    3. Give it a name and description
    4. Choose Public or Private
    5. Click Create repository

    GitHub will show you instructions for connecting it to your local project.

    Pull Requests (PRs)

    A Pull Request is GitHub's way of saying "I'd like to merge this branch into main." Instead of merging directly, you create a PR so others can review your code first.

    The basic flow:

    1. Create a branch and push it to GitHub
    2. On GitHub, create a Pull Request
    3. Review the changes (or have someone else review)
    4. Merge the PR

    What to ask your AI: "I pushed my branch to GitHub. Can you help me create a pull request?"

    README Files

    The README.md file is the "front page" of your GitHub repository. It's shown automatically when someone visits your repo. Good READMEs include:

    • What the project does
    • How to install and run it
    • How to contribute

    What to ask your AI: "Can you help me write a README for my project? It's a [description of your project]."

    Authentication: HTTPS vs SSH

    When connecting to GitHub, you'll need to authenticate. There are two common methods:

    HTTPS (Easier for Beginners)

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

    You'll be asked for your credentials. GitHub recommends using a Personal Access Token instead of your password.

    SSH (More Convenient Long-Term)

    git clone git@github.com:username/repo.git

    Requires setting up SSH keys, but then you never have to enter credentials again.

    What to ask your AI: "Can you help me set up SSH keys for GitHub?"

    Essential Remote Commands

    CommandWhat It Does
    git remote -vShow connected remotes
    git pushUpload commits to remote
    git pullDownload and merge remote changes
    git push -u origin branchPush a new branch and track it
    git remote add origin URLConnect to a remote repository
    git fetchDownload remote changes without merging

    What's Next?

    You now know the full Git basics — from creating repos to pushing code to GitHub. The final tutorial is a cheat sheet with all the commands you've learned, plus example prompts for working with AI tools.


    🌐 www.genai-mentor.ai