Ready to connect your local Git work to the world? After mastering Git installation and local repositories, it's time to learn how to create and connect to remote repositories on GitHub. This tutorial follows a real terminal session, explaining every command step-by-step.
🎯 What You'll Learn: In this comprehensive guide, you'll discover:
- How to create a new local Git repository from scratch
- Adding and committing files with detailed explanations
- Understanding what remote repositories are and why they matter
- Setting up a new repository on GitHub correctly
- Connecting your local repository to GitHub using git remote
- Understanding the difference between 'origin' and custom remote names
- Checking remote connections with git remote -v
- Best practices for remote repository management
Prerequisites: Basic Git knowledge and Git installation
🎯 Starting Point: Creating a Local Repository
Setting Up Your Project Directory
Let's start by creating a new local repository. This follows the exact terminal session that demonstrates a complete workflow:
[centos9@vbox Repo 23:26:25]$ mkdir seconddemo
[centos9@vbox Repo 23:26:47]$ cd seconddemo/
Command Breakdown:
mkdir seconddemo
- Creates a new directory named "seconddemo"cd seconddemo/
- Changes into the newly created directory- Purpose: Establishes a clean workspace for our new project
Initializing the Git Repository
Now let's transform this regular directory into a Git repository:
[centos9@vbox seconddemo 23:27:00]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/centos9/Razzaq-Labs-II/random/Repo/seconddemo/.git/
Command Explanation:
git init
- Initializes a new Git repository in the current directory- Creates: Hidden
.git
folder containing all Git infrastructure - Result: Transforms a regular folder into a Git-tracked repository
Understanding Git's Output:
- Branch Name Hint: Git is using 'master' as the default branch name
- Configuration Tip: Shows how to change default branch name to 'main' (modern standard)
- Success Message: Confirms the repository was created successfully
- Location: Shows exact path where the
.git
folder was created
Checking Repository Status
Let's verify our new repository:
[centos9@vbox seconddemo 23:27:08]$ ls -al
total 0
drwxr-xr-x. 3 centos9 centos9 18 Sep 30 23:27 .
drwxr-xr-x. 5 centos9 centos9 76 Sep 30 23:26 ..
drwxr-xr-x. 7 centos9 centos9 119 Sep 30 23:27 .git
[centos9@vbox seconddemo 23:27:11]$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Output Analysis:
- Directory Listing: Shows the hidden
.git
folder (119 bytes of Git infrastructure) - Git Status Information:
On branch master
- Currently on the master branchNo commits yet
- This is a brand new repository with no historynothing to commit
- No files to track yet
📄 Adding Content to Your Repository
Creating Your First File
Let's add some content to track:
[centos9@vbox seconddemo 23:27:23]$ touch first.txt
[centos9@vbox seconddemo 23:27:46]$ cat <<EOF > first.txt
> hello world
> EOF
[centos9@vbox seconddemo 23:27:49]$ cat first.txt
hello world
Command Breakdown:
touch first.txt
- Creates an empty file named "first.txt"cat <<EOF > first.txt
- Uses a "here document" to write content to the file> hello world
- The content being written> EOF
- Marks the end of the inputcat first.txt
- Displays the file contents to verify
What is a "Here Document"?
The <<EOF
syntax creates a here document, allowing you to input multiple lines of text until you type EOF
. This is a common way to write content to files from the command line.
Checking File Status
Now let's see how Git responds to our new file:
[centos9@vbox seconddemo 23:27:57]$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
first.txt
nothing added to commit but untracked files present (use "git add" to track)
Status Breakdown:
- Untracked files: Git sees
first.txt
but isn't tracking changes to it yet - Helpful hint: Git suggests using
git add <file>...
to start tracking - Important concept: New files are "untracked" until you explicitly tell Git to track them
Staging Your First File
Let's tell Git to start tracking our file:
[centos9@vbox seconddemo 23:28:00]$ git add .
[centos9@vbox seconddemo 23:28:02]$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: first.txt
Command Explanation:
git add .
- Adds all files in the current directory to the staging area- Alternative: Could use
git add first.txt
to add just that specific file - Result: File moves from "untracked" to "staged" (ready to commit)
Status Changes:
- Before: "Untracked files" section
- After: "Changes to be committed" section
- New option: Git shows how to unstage if needed (
git rm --cached
)
💾 Making Your First Commit
Creating the Initial Commit
Let's save our first snapshot:
[centos9@vbox seconddemo 23:28:12]$ git commit -m "added first.txt"
[master (root-commit) 303fea0] added first.txt
1 file changed, 1 insertion(+)
create mode 100644 first.txt
[centos9@vbox seconddemo 23:28:17]$ git status
On branch master
nothing to commit, working tree clean
Command Breakdown:
git commit -m "added first.txt"
- Creates a commit with the specified message-m
flag allows you to provide the commit message inline- Commit message: Brief description of what this commit does
Commit Output Analysis:
[master (root-commit) 303fea0]
- Branch, commit type, and commit hash1 file changed, 1 insertion(+)
- Summary of changescreate mode 100644 first.txt
- File creation with permissions
After Commit:
nothing to commit, working tree clean
- Everything is saved and synchronized
Adding More Content
Let's add another file and modify the existing one:
[centos9@vbox seconddemo 23:29:20]$ touch second.txt
[centos9@vbox seconddemo 23:29:32]$ cat <<EOF > second.txt
> working
> EOF
[centos9@vbox seconddemo 23:30:03]$ cat <<EOF >> first.txt
>
> new changes
> EOF
New Commands Introduced:
cat <<EOF >> first.txt
- Uses>>
to append to existing file (vs>
which overwrites)- Result: Adds content to the end of
first.txt
without losing existing content
Let's check what our files look like now:
[centos9@vbox seconddemo 23:29:48]$ cat first.txt
hello world
new changes
[centos9@vbox seconddemo 23:29:46]$ cat second.txt
working
Checking Status with Multiple Changes
[centos9@vbox seconddemo 23:30:14]$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: first.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
second.txt
no changes added to commit (use "git add" and/or "git commit -a")
Status Analysis:
- Modified files:
first.txt
shows as modified (was tracked, now changed) - Untracked files:
second.txt
is new and untracked - Two different states: Git distinguishes between modified tracked files and new untracked files
Staging and Committing Multiple Changes
[centos9@vbox seconddemo 23:30:18]$ git add .
[centos9@vbox seconddemo 23:30:38]$ git commit -m "updated first.txt and added second.txt"
[master a38542e] updated first.txt and added second.txt
2 files changed, 3 insertions(+)
create mode 100644 second.txt
Commit Summary:
2 files changed
- Both first.txt (modified) and second.txt (created) were included3 insertions(+)
- Total lines added across both files- Shows both modification and creation in one commit
📚 Understanding Git Log
Viewing Repository History
Let's look at our commit history:
[centos9@vbox seconddemo 23:30:43]$ git logs
git: 'logs' is not a git command. See 'git --help'.
The most similar command is
log
[centos9@vbox seconddemo 23:30:46]$ git log
commit a38542e836ebbc1e75179c3b6b6c064efb72b8cd (HEAD -> master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Tue Sep 30 23:30:38 2025 +0500
updated first.txt and added second.txt
commit 303fea0985b17714f6f3ed934684a653e11c6578
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Tue Sep 30 23:28:12 2025 +0500
added first.txt
Learning from the Error:
git logs
(with 's') is not valid - demonstrates common typo- Git helpfully suggests the correct command:
git log
Log Output Explanation:
- Commit Hash: Unique identifier for each commit (a38542e... and 303fea0...)
- HEAD -> master: Shows current position in the repository
- Author: From your git config user.name and user.email
- Date: When the commit was made
- Commit Message: The description provided with -m flag
- Order: Most recent commits first
🌐 Introduction to Remote Repositories
What are Remote Repositories?
Remote Repository: A version of your project hosted on a server (like GitHub, GitLab, or Bitbucket) that multiple people can access and contribute to.
Key Benefits:
- ✅ Backup: Your code is safely stored in the cloud
- ✅ Collaboration: Multiple developers can work on the same project
- ✅ Sharing: Others can see and use your code
- ✅ Synchronization: Keep multiple machines in sync with the same codebase
Understanding Remote Connections
Before we create a GitHub repository, let's check our current remote status:
[centos9@vbox seconddemo 23:36:05]$ git remote -v
Command Explanation:
git remote -v
- Lists all remote repositories connected to your local repo-v
flag stands for "verbose" - shows both fetch and push URLs- Current output: Nothing! This repository has no remote connections yet
🏗️ Setting Up GitHub Repository
Creating a New GitHub Repository
Now we need to create a repository on GitHub. Here's the step-by-step process referenced in our session:
Steps to Create GitHub Repository:
- Go to GitHub.com and sign in to your account
- Click "New repository" or the "+" icon in the top right
- Give it a name (e.g., "secondproject" as in our example)
- Add a description (optional but recommended)
- Set visibility to Public or Private
- IMPORTANT: Do NOT add a README file for existing local repositories
Why avoid README for existing repos? If you add a README, LICENSE, or .gitignore file, GitHub creates an initial commit. This creates a conflict when you try to push your existing local commits, requiring a merge that complicates the process for beginners.
The Safe Repository Creation Method
Scenario | GitHub Setup | Why This Way |
---|---|---|
Existing Local Repository | Create empty repository (no README) | Avoids merge conflicts when pushing |
Starting Fresh | Add README, clone locally | Clean start with GitHub structure |
🔗 Connecting Local Repository to GitHub
Adding the Remote Connection
After creating your empty GitHub repository, you'll get a URL like https://github.com/username/repository-name.git
. Let's connect it:
[centos9@vbox seconddemo 23:36:25]$ git remote add hello https://github.com/owais-io/secondproject.git
[centos9@vbox seconddemo 23:37:19]$ git remote -v
hello https://github.com/owais-io/secondproject.git (fetch)
hello https://github.com/owais-io/secondproject.git (push)
Command Breakdown:
git remote add hello [URL]
- Adds a new remote repository connectionhello
- Custom name for this remote (usually people use "origin")[URL]
- The HTTPS URL from your GitHub repository- Result: Creates a connection between local and remote repository
Remote -v Output Explanation:
- hello (fetch): URL used when downloading changes from GitHub
- hello (push): URL used when uploading changes to GitHub
- Both same: For HTTPS connections, fetch and push URLs are typically identical
Why "hello" Instead of "origin"?
Remote Naming Conventions:
"origin" - The standard default name when you clone a repository "hello" - Custom name used in this example to demonstrate that the name is flexible
When to use custom names:
- Multiple remotes: When you have forks, upstream repositories, or multiple destinations
- Learning purposes: To understand that "origin" is just a convention, not magic
- Team workflows: Some teams use descriptive names like "upstream", "fork", "production"
Checking Remote Repository Details
Let's get more information about our remote connection:
[centos9@vbox seconddemo 23:38:12]$ git remote show origin
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[centos9@vbox seconddemo 23:38:27]$ git remote show hello
* remote hello
Fetch URL: https://github.com/owais-io/secondproject.git
Push URL: https://github.com/owais-io/secondproject.git
HEAD branch: (unknown)
Understanding the Commands:
git remote show origin
- Failed because we named our remote "hello", not "origin"git remote show hello
- Succeeded and shows detailed information- HEAD branch: (unknown) - GitHub repository is empty, so no default branch is set yet
🎯 Remote Repository Best Practices
Conventional Remote Names
Remote Name | Purpose | When to Use |
---|---|---|
origin | Your main repository (usually your GitHub repo) | Most common, default for cloned repos |
upstream | Original repository you forked from | Contributing to open source projects |
fork | Your personal fork of a project | When working with multiple forks |
Custom names | Descriptive names for specific purposes | Multiple deployment targets, team workflows |
Common Remote Commands Reference
Command | Purpose | Example |
---|---|---|
git remote -v | List all remotes with URLs | Check what remotes are configured |
git remote add <name> <url> | Add a new remote connection | Connect to GitHub repository |
git remote show <name> | Show detailed remote information | Check remote status and branches |
git remote rename <old> <new> | Rename a remote | Change "hello" to "origin" |
git remote remove <name> | Remove a remote connection | Clean up unused remotes |
🔧 Troubleshooting Remote Connections
Common Issues and Solutions
Issue 1: Wrong Remote Name
git remote show origin
# Error: 'origin' does not appear to be a git repository
Solution: Check your remote names with git remote -v
and use the correct name.
Issue 2: Invalid URL
git remote add origin https://github.com/wrong-url.git
# Later: Could not read from remote repository
Solution: Verify the URL is correct on GitHub and update with git remote set-url
.
Issue 3: Authentication Problems
# Error: Permission denied or authentication failed
Solution: Check GitHub username/password, or set up SSH keys for easier authentication.
🎯 Key Takeaways
✅ Concepts Mastered
-
Local Repository Creation: Used
git init
to create a repository from scratch -
File Lifecycle: Understood untracked → staged → committed progression
-
Multiple Commits: Created meaningful commit history with descriptive messages
-
Remote Concepts: Learned what remote repositories are and why they're important
-
GitHub Setup: Created an empty repository to avoid merge conflicts
-
Remote Connections: Used
git remote add
to link local and remote repositories -
Remote Management: Explored commands to view and manage remote connections
-
Naming Conventions: Understood the difference between "origin" and custom names
🚀 Best Practices Summary
Repository Setup Best Practices
Practice | Why Important | How To |
---|---|---|
Meaningful Commit Messages | Help team understand changes | Use present tense, be specific |
Empty GitHub Repository | Avoid merge conflicts | Don't add README for existing repos |
Use Standard Remote Names | Team consistency and clarity | Use "origin" unless you have a specific reason |
Regular Status Checks | Stay aware of repository state | Run git status frequently |
📋 Git Remote Commands Cheat Sheet
Essential Remote Commands
# View all remote connections
git remote -v
# Add a new remote repository
git remote add origin https://github.com/username/repo.git
# Show detailed information about a remote
git remote show origin
# Rename a remote
git remote rename old-name new-name
# Change the URL of an existing remote
git remote set-url origin https://github.com/username/new-repo.git
# Remove a remote connection
git remote remove origin
# List just the remote names
git remote
File Operations Commands
# Initialize a new Git repository
git init
# Check repository status
git status
# Add files to staging area
git add . # Add all files
git add filename.txt # Add specific file
# Create a commit
git commit -m "commit message"
# View commit history
git log
git log --oneline # Condensed view
🎉 Congratulations! You've successfully:
- ✅ Created a local Git repository from scratch
- ✅ Made multiple commits with meaningful messages
- ✅ Set up a GitHub repository correctly
- ✅ Connected your local repository to GitHub using remotes
- ✅ Understood the relationship between local and remote repositories
Next Step: Learn how to push your changes to GitHub and pull updates to keep your repositories synchronized!
💬 Discussion
How did your first remote repository setup go?
- Did you encounter any issues connecting your local repository to GitHub?
- Which part of the remote concept was most challenging to understand?
- Have you experimented with different remote names besides "origin"?
- What questions do you have about managing multiple remote repositories?
Share your experience with setting up your first GitHub connection in the comments below!