Skip to main content

Command Palette

Search for a command to run...

Git Methods: Getting & Creating Repos

Published
4 min read
Git Methods: Getting & Creating Repos
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

Topics covered: git init (create a new repository) and git clone (copy a remote repository)

0) Prerequisites

  • Git installed: git --version

  • A terminal (PowerShell, CMD, Git Bash, macOS Terminal, or Linux shell)

  • Optional: A Git hosting account (e.g., GitHub/GitLab/Bitbucket) if you plan to push to a remote

1) Create a brand-new repository with git init

1.1 Create a project folder

mkdir hello-git && cd hello-git

1.2 Initialize the repository

git init
# If your Git is old and still defaults to 'master':
# git init --initial-branch=main

What happened:

  • Git created a hidden .git/ directory.

  • Your current folder is now a Git repository.

Verify:

ls -la            # you should see a .git directory
git status        # "No commits yet"

1.3 Add your first file

echo "# Hello Git" > README.md
git add README.md
git commit -m "Initial commit: add README"

1.4 (Optional) Add a .gitignore

printf "node_modules/\n.DS_Store\n.env\n" > .gitignore
git add .gitignore
git commit -m "Add .gitignore"

1.5 Connect to a remote and push

Create an empty repo on your hosting service, then link it locally:

HTTPS example:

git remote add origin https://github.com/<your-user>/hello-git.git
git branch -M main             # ensure your default branch is 'main'
git push -u origin main

SSH example (requires SSH keys set up on the host):

git remote add origin git@github.com:<your-user>/hello-git.git
git branch -M main
git push -u origin main

Verify:

git remote -v      # shows 'origin' fetch/push URLs
git log --oneline  # shows your commit(s)

You’ve created a repo from scratch and published it.

2) Initialize Git in an existing project (common case)

If you already have files:

cd path/to/existing-project
git init
git add .
git commit -m "Initial commit for existing project"
# then add a remote and push as in 1.5

Tip: add a tailored .gitignore before git add . to avoid committing build artifacts or secrets.

3) Copy an existing remote repository with git clone

3.1 Clone via HTTPS (simple)

cd ~/code             # go to the folder where you keep projects
git clone https://github.com/<org-or-user>/<repo>.git
cd <repo>

3.2 Clone via SSH (passwordless after key setup)

git clone git@github.com:<org-or-user>/<repo>.git
cd <repo>

What happened:

  • Git created a new folder named <repo>.

  • It downloaded all tracked files and the Git history.

  • It set origin to the remote URL and checked out the default branch.

Verify:

git remote -v
git branch --show-current
git log --oneline --decorate --graph -n 5

3.3 Optional clone flags you’ll actually use

  • Shallow clone (faster, less history):

      git clone --depth 1 https://github.com/<org>/<repo>.git
    
  • Specific branch only:

      git clone --branch develop --single-branch https://github.com/<org>/<repo>.git
    
  • Include submodules:

      git clone --recurse-submodules https://github.com/<org>/<repo>.git
    

3.4 Make a change and push (to verify access)

git checkout -b feature/readme-note
echo "Small note" >> README.md
git add README.md
git commit -m "Add small note to README"
git push -u origin feature/readme-note

Open a Pull Request on your host to merge the branch.

4) When to use init vs clone

  • Use git init when you’re starting from scratch or converting an existing local project into a Git repo.

  • Use git clone when a repo already exists remotely and you want a local working copy with history and a preconfigured origin.

5) Practice checklist

  1. Create a new repo with git init, add a README, commit, add a remote, and push.

  2. Initialize Git in an existing folder, add a .gitignore, make an initial commit, push.

  3. Clone a public repo via HTTPS, create a branch, commit a change, and push the branch.

  4. Repeat #3 using SSH.

  5. Try --depth 1 and --branch to understand shallow and single-branch clones.

6) Common problems and quick fixes

  • fatal: not a git repository
    You’re outside a repo. cd into the project folder (one containing .git/) or run git init.

  • Auth errors on git push with HTTPS
    Use a Personal Access Token instead of a password (GitHub). Set a credential helper:

      git config --global credential.helper manager-core   # Windows
      git config --global credential.helper osxkeychain    # macOS
    
  • Permission denied (publickey) on SSH
    Generate and add an SSH key:

      ssh-keygen -t ed25519 -C "you@example.com"
      eval "$(ssh-agent -s)"
      ssh-add ~/.ssh/id_ed25519
      # then add the public key (~/.ssh/id_ed25519.pub) to your Git host account
    
  • Pushed to the wrong branch or remote
    Check with git remote -v, git branch -av. Adjust:

      git branch -M main
      git remote set-url origin <correct-url>
    

7) Quick reference

# Create a new local repo
git init
git add .
git commit -m "Initial commit"

# Add a remote and push
git remote add origin https://github.com/<user>/<repo>.git
git branch -M main
git push -u origin main

# Clone an existing repo
git clone https://github.com/<org>/<repo>.git
# or
git clone git@github.com:<org>/<repo>.git

# Verify
git status
git remote -v
git log --oneline

.

1 views