Cloning and Customizing a Repository
This page explains how to create your own repository based on a template or an existing project. By understanding the relationship between origin (your repository) and upstream (the original repository), you can safely customize the project and bring in updates later.
Prerequisites
Section titled “Prerequisites”- Git is installed
- You have created a GitHub account
- You have access to a terminal in an environment such as VS Code
Create a Private Repository
Section titled “Create a Private Repository”First, create a new repository for yourself on GitHub.
- Sign in to GitHub
- Click + in the upper-right corner, then click New repository
- Enter a name in Repository name (example:
your-repo-name) - Choose Private (or Public if you want it visible)
- Click Create repository
The resulting URL looks like this:
https://github.com/you/new_repo.gitClone the Template and Disconnect the Original Remote
Section titled “Clone the Template and Disconnect the Original Remote”Clone the template repository and disconnect the original origin.
# Clone the template repository
git clone https://github.com/someone/original_repo.git
cd original_repo
# Check current remotes
git remote -v
# Remove the original origin connection
git remote remove origin
# Confirm nothing is listed
git remote -v💡
git remote remove originonly removes the remote connection. It does not delete your local files.
Connect Your Own Repository and Make the First Commit
Section titled “Connect Your Own Repository and Make the First Commit”After disconnecting the original remote, register your own repository as the new origin and push the first commit.
# Stage and make the initial commit
git add .
git commit -m "Initial commit"
# Add your own repository as origin
git remote add origin https://github.com/you/new_repo.git
# Push main and set tracking
git push -u origin mainNow your local repository is connected to your own remote. From now on, git push alone is enough.
⚠️ If
git push -u origin mainsays that themainbranch does not exist, the template may still be usingmaster. Rungit branch -M mainfirst, then push again.
Set Up Upstream to Pull in Updates
Section titled “Set Up Upstream to Pull in Updates”If the original template changes, set up upstream so you can pull those changes in later.
# Add upstream
git remote add upstream https://github.com/someone/original_repo.git
git remote -v
# Fetch the latest information and merge it
git fetch upstream
git checkout main
git merge upstream/main⚠️ If a conflict occurs during the merge, fix the file manually. After that, run
git add <file>andgit committo finish.
After a successful merge, push the changes to your own remote:
git push origin mainCheck the Remote Setup
Section titled “Check the Remote Setup”Your final remote configuration should look like this:
origin https://github.com/you/new_repo.git (fetch)
origin https://github.com/you/new_repo.git (push)
upstream https://github.com/someone/original_repo.git (fetch)
upstream https://github.com/someone/original_repo.git (push)| Remote name | Repository | Purpose |
|---|---|---|
origin | Your repository | Daily push / pull |
upstream | The original template | Pulling in updates |
Troubleshooting
Section titled “Troubleshooting”You added the wrong URL to a remote
git remote set-url origin https://github.com/you/correct_repo.gitBranch name mismatch (main vs master)
# Standardize the branch name to main
git branch -M main
git push -u origin mainPush was rejected because the remote is newer
git pull --rebase upstream mainAuthentication errors appear
Switching from HTTPS to SSH may solve the problem.
git remote set-url origin git@github.com:you/new_repo.git💡 Create a new branch before you start working so you have a safety net if something goes wrong. Get in the habit of creating a backup branch with
git checkout -b feature/backup.
Summary
Section titled “Summary”| Step | Command |
|---|---|
| Clone the template | git clone <URL> |
| Remove the original remote | git remote remove origin |
| Connect your own repository | git remote add origin <URL> |
| Make the initial commit and push | git push -u origin main |
| Register upstream | git remote add upstream <URL> |
| Pull in updates | git fetch upstream -> git merge upstream/main |
Q. Do I need git init?
No. A cloned repository already has a .git folder, so git init is not needed. You only need git init when starting a completely new project from scratch.
Q. Do I have to set up upstream?
Not if you do not plan to pull in template updates. However, it is convenient to set it up from the beginning, so I recommend registering it during setup.
Q. What does -u in git push -u origin main mean?
-u is short for --set-upstream, which sets the tracking relationship between the local branch and the remote branch. Once it is set, you can use git push alone next time.