Starting a Git Repository
This section explains two simple ways to start a Git repository: starting on GitHub (remote) or starting locally on your computer. Both methods lead to the same Git workflow, where you can track changes, collaborate, and manage your project. Let's break it down step by step.
Option 1: Start on GitHub (Remote)
This approach is great if you want to create the repository online first and then bring it to your computer.
Step 1: Create a Repository on GitHub
- Log in to GitHub: Go to github.com and sign in. If you don’t have an account, sign up for free.
- Create a New Repository:
- Click the + icon in the top-right corner and select New repository.
- Give your repository a name (e.g.,
my-project). - Choose whether it’s Public (anyone can see it) or Private (only you and collaborators can see it).
- Optionally, add a description, a README file, or a
.gitignorefile (to ignore certain files like logs or temporary files). - Click Create repository.
- Copy the Repository URL: After creating the repository, you’ll see a URL like
https://github.com/your-username/my-project.gitorgit@github.com:your-username/my-project.gitfor ssh. Copy it for the next step.
Step 2: Clone the Repository to Your Computer
- Open a Terminal or Command Prompt:
- On Windows, use Git Bash (install Git from git-scm.com if you haven’t).
- On macOS or Linux, use the built-in terminal.
-
Navigate to Your Desired Folder:
-
Use the
cdcommand to go to the folder where you want the repository. For example:cd ~/Desktop
-
-
Clone the Repository:
-
Run the following command, replacing
REPO_URLwith the URL you copied:git clone REPO_URLExample:
git clone git@github.com:your-username/my-project.git- This creates a folder namedmy-projecton your computer with the repository files.
-
-
Navigate to the Repository Folder:
-
Move into the cloned repository:
cd my-project
-
Step 3: Start Working
- Add files, edit them, and use Git commands like
git add,git commit, andgit pushto save changes and send them to GitHub. - Example workflow:
git add .
git commit -m "Initial commit"
git push origin main
Option 2: Start Locally
This approach is ideal if you already have a project on your computer and want to version-control it with Git and connect it to GitHub.
Step 1: Initialize a Git Repository on Your Computer
- Open a Terminal or Command Prompt:
- Make sure Git is installed (download from git-scm.com if needed).
-
Navigate to Your Project Folder:
-
Go to the folder containing your project files. For example:
cd ~/Desktop/my-project
-
-
Initialize the Git Repository:
-
Run the following command to turn the folder into a Git repository:
git init -
This creates a hidden
.gitfolder to track changes.
-
-
Add Your Files:
-
Stage all files in the folder:
git add . -
Commit the files to the repository:
git commit -m "Initial commit"
-
Step 2: Connect to GitHub
-
Create a Repository on GitHub:
- Go to github.com and create a new repository (as described in Option 1, Step 1).
- Important: Do not initialize the repository with a README,
.gitignore, or license, as your local repository already has files.
-
Link Your Local Repository to GitHub:
- Copy the repository URL (e.g.,
git@github.com:your-username/my-project.git). -
In your terminal, run:
git remote add origin REPO_URL
Example:
git remote add origin git@github.com:your-username/my-project.git - Copy the repository URL (e.g.,
-
Push Your Local Repository to GitHub:
-
Send your local commits to GitHub:
git push -u origin main -
The
-uflag sets the default branch (usuallymain) for future pushes.
-
Step 3: Start Working
- Your local repository is now connected to GitHub. Use commands like
git add,git commit, andgit pushto manage changes. - Example:
git add .
git commit -m "Added new feature"
git push origin main
Both Lead to the Same Workflow
Whether you start on GitHub or locally, you end up with:
- A local repository on your computer for working on files.
- A remote repository on GitHub for backup, collaboration, and sharing.
Basic Git Commands to Know
git add .: Stages all changed files for a commit.git commit -m "message": Saves changes with a descriptive message.git push origin main: Sends your commits to GitHub.git pull: Fetches and merges updates from GitHub to your local repository.git status: Shows the current state of your repository (e.g., changed files).
Basic workflow
Tips for Beginners
- Commit Often: Save your work frequently with meaningful commit messages.
- Check Git Status: Use
git statusto see what’s happening in your repository. - Backup on GitHub: Regularly push your changes to GitHub to avoid losing work.
- Learn More: Explore branching, merging, and collaboration as you get comfortable with Git.
Now you’re ready to start version-controlling your projects with Git and GitHub!