Master Github from Scratch
Master Github from Scratch

Master Git Like a Pro: How to Set Up, Commit, and Push a GitHub Repo from Scratch!

How to Start a GitHub Repository Locally and Push to Main or Feature Branch

GitHub provides an efficient way to manage your code. If you’re starting fresh, you can initialize a local repository and push it to GitHub.

1. Prerequisites

Before you begin, ensure you have:

  • Git Installed: Download and install Git from git-scm.com.
  • GitHub Account: Sign up at GitHub.
  • GitHub Repository: Create one or use Git commands to create it directly.

Method 1: Starting a Repository Locally and Pushing to GitHub

Step 1: Open Terminal or Command Prompt

  • Windows: Use Git Bash or Command Prompt
  • Mac/Linux: Use Terminal

Step 2: Navigate to Your Project Folder

If you already have a folder, navigate to it; otherwise, create a new one.

mkdir my-project && cd my-project

Step 3: Initialize Git in the Project

git init

This sets up a local repository.

Step 4: Add a Remote GitHub Repository

First, create a repository on GitHub:

  • Go to GitHub
  • Click New Repository
  • Copy the remote repository URL

Now, link your local repo to GitHub:

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

Step 5: Add Files to the Repository

To track all files in the project directory:

git add .

Step 6: Commit the Changes

git commit -m "Initial commit"

Step 7: Push the Code to GitHub

To push to the main branch:

git branch -M main
git push -u origin main

To push to a feature branch:

git checkout -b feature-branch
git push -u origin feature-branch

Method 2: Cloning an Existing GitHub Repository and Pushing Changes

If you already have a repository on GitHub and want to work locally:

Step 1: Clone the Repository

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

Step 2: Navigate into the Repository

cd repository-name

Step 3: Create a New Feature Branch

git checkout -b feature-branch

Step 4: Make Changes and Commit

After modifying files, stage and commit them:

git add .
git commit -m "Added new features"

Step 5: Push to GitHub

git push origin feature-branch

Best Practices for GitHub Repositories

  • Use Meaningful Commit Messages: Always describe what changes were made.
  • Create Feature Branches: Never work directly on main, use feature branches instead.
  • Pull Before Pushing: Run git pull origin main to sync changes before pushing.

Final Thoughts

By following these steps, you can successfully initialize, commit, and push a repository to GitHub. Whether you are working on a personal project or collaborating with a team, these Git commands will help streamline your workflow.


External References:

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *