Skip to content

Instantly share code, notes, and snippets.

@sumitsahoo
Created September 4, 2025 07:33
Show Gist options
  • Save sumitsahoo/a32e2bd46fb3e30302c2dc9c81d441df to your computer and use it in GitHub Desktop.
Save sumitsahoo/a32e2bd46fb3e30302c2dc9c81d441df to your computer and use it in GitHub Desktop.
Add existing repository to GitHub
#!/bin/bash
# ==============================================================================
# A simple script to link a local Git repository to a new remote on GitHub
# and push the initial 'main' branch.
#
# INSTRUCTIONS:
# 1. Make sure you have initialized a Git repository (`git init`).
# 2. Add and commit your files (`git add .` and `git commit -m "Initial commit"`).
# 3. Replace the URL with your own repository's URL.
# 4. Make the script executable: `chmod +x push_to_github.sh`
# 5. Run the script: `./push_to_github.sh`
# ==============================================================================
# --- STEP 1: Add the Remote Repository ---
# This command connects your local repository to a remote one on GitHub.
# 'origin' is the standard nickname given to the remote server.
# The URL points to your new, empty repository on GitHub.
git remote add origin https://github.com/sumitsahoo/abc-xyz.git
# --- STEP 2: Rename the Default Branch to 'main' ---
# This command renames your current local branch to 'main'.
# The '-M' flag will create the branch if it doesn't exist or forcefully
# rename it if it does. This is a common practice to move away from 'master'.
git branch -M main
# --- STEP 3: Push the 'main' Branch to the Remote ---
# This command uploads your local 'main' branch to the 'origin' remote.
# The '-u' flag (short for --set-upstream) creates a tracking link between
# your local 'main' branch and the remote 'main' branch. After this first push,
# you can simply use 'git push' to push future changes from this branch.
git push -u origin main
echo "✅ Successfully pushed the 'main' branch to the remote 'origin'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment