Local Git is your version control alone.
By connecting a remote repository such as GitHub, you can safely back up your code.
You can download it anywhere and work with your team.
This section clearly distinguishes the differences between clone, push, pull, and fetch.
Set up PAT (Personal Access Token) and SSH key authentication.
Learning Objectives
- You can clone remote storage with
git clone. - The difference in operation between
git push·git pull·git fetchcan be explained. - You can manage remote connections with
git remote add/rename/remove. - Authentication can be set up with GitHub PAT or SSH key.
- Understand the relationship between the local branch and the remote branch (
origin/main).
Key concepts
1) git clone
git clone https://github.com/user/repo.git # HTTPS
git clone [email protected]:user/repo.git # SSH
git clone https://github.com/user/repo.git mydir #by another name
git clone --depth=1 https://github.com/user/repo.git #shallow replication
2) push / pull / fetch
local remote (origin)
main ──push──▶ origin/main
main ◀──pull── origin/main (fetch + merge)
──fetch──▶ origin/main (no effect on local branches)
git push origin main #Local main → remote main
git push -u origin main #Push after configuring upstream (afterwards, only git push is possible)
git pull origin main # fetch + merge
git fetch origin #Update remote information only, no merge
git fetch origin main #Only certain branches
3) remote management
git remote -v #Remote list + URL verification
git remote add origin URL #add remote
git remote rename origin upstream #change name
git remote remove origin #remote wipe
git remote set-url origin NEW_URL #Change URL
4) Remote branch tracking
git branch -vv #Check local and remote tracked branches
git switch -c feature origin/feature #Check out remote branch locally
git push origin --delete feature #Delete remote branch
5) Credential settings
PAT (Personal Access Token) — HTTPS method
# GitHub → Settings → Developer settings → Personal access tokens → Generate
#When cloning/pushing, enter token instead of password.
git config --global credential.helper store #Save (be careful of plain text)
SSH key method
ssh-keygen -t ed25519 -C "[email protected]" #key generation
cat ~/.ssh/id_ed25519.pub #Copy public key
# GitHub → Settings → SSH and GPG keys → New SSH key
ssh -T [email protected] #Connection test
See as example
Example 1 — ex01_clone.sh: Clone a local bare repository to simulate two repositories
#!/usr/bin/env bash
#The GitHub lab requires a network, so we simulate it with a local bare repository.
WORKDIR=$(mktemp -d)
cd "$WORKDIR"
#Create a bare repository for the “remote” role
git init --bare remote.git
echo "Create a bare repository: $WORKDIR/remote.git"
#Create a local work repository and push
git init local_a
cd local_a
git config user.name "Worker A" && git config user.email "[email protected]"
echo "Hello" > README.md
git add . && git commit -q -m "init"
git remote add origin "$WORKDIR/remote.git"
git push -u origin main
echo ""
echo "=== clone ==="
cd "$WORKDIR"
git clone remote.git local_b
cd local_b
git log --oneline
rm -rf "$WORKDIR"
Execution result
Create bare repository: /tmp/tmp.XXX/remote.git
=== clone ===
Cloning into 'local_b'...
a1b2c3d init
Key point: git clone copies the entire history of the remote repository and automatically configures the origin remote.
Example 2 — ex02_remote.sh: Add/Confirm/Change remote
#!/usr/bin/env bash
REPO=$(mktemp -d)
cd "$REPO"
git init -q
git config user.name "For practice" && git config user.email "[email protected]"
echo "=== Add remote ==="
git remote add origin https://github.com/example/repo.git
git remote add upstream https://github.com/original/repo.git
echo ""
echo "=== remote list ==="
git remote -v
echo ""
echo "=== Change origin URL ==="
git remote set-url origin https://github.com/example/new-repo.git
git remote -v
echo ""
echo "=== remove upstream ==="
git remote remove upstream
git remote -v
rm -rf "$REPO"
Execution result
=== Add remote ===
=== remote list ===
origin https://github.com/example/repo.git (fetch)
origin https://github.com/example/repo.git (push)
upstream https://github.com/original/repo.git (fetch)
...
=== Change origin URL ===
origin https://github.com/example/new-repo.git (fetch)
...
Key point: You can check all remote repositories and URLs connected to git remote -v.
Example 3 — ex03_push_pull.sh: Simulating push/pull with local bare repository
#!/usr/bin/env bash
WORKDIR=$(mktemp -d)
git init --bare "$WORKDIR/remote.git" -q
#Storage A
git clone "$WORKDIR/remote.git" "$WORKDIR/repo_a" -q
cd "$WORKDIR/repo_a"
git config user.name "Worker A" && git config user.email "[email protected]"
echo "A's file" > a.txt
git add . && git commit -q -m "feat: A adds file"
git push origin main -q
#Repository B
git clone "$WORKDIR/remote.git" "$WORKDIR/repo_b" -q
cd "$WORKDIR/repo_b"
git config user.name "Worker B" && git config user.email "[email protected]"
echo "=== Log before pull from B ==="
git log --oneline
git pull origin main -q
echo ""
echo "=== Log after pull from B ==="
git log --oneline
rm -rf "$WORKDIR"
Execution result
=== Log before pull from B ===
(none)
=== Log after pull from B ===
a1b2c3d feat: A adds file
Key point: git pull performs fetch + merge at once.
Example 4 — ex04_credentials.sh: SSH key generation and instructions
#!/usr/bin/env bash
set -euo pipefail
echo "=== Instructions on how to create an SSH key ==="
echo ""
echo "1. Generate key:"
echo " ssh-keygen -t ed25519 -C \"[email protected]\""
echo ""
echo "2. Verify public key:"
echo " cat ~/.ssh/id_ed25519.pub"
echo ""
echo "3. GitHub registration:"
echo " GitHub → Settings → SSH and GPG keys → New SSH key"
echo "Paste the public key copied above"
echo ""
echo "4. Test connection:"
echo " ssh -T [email protected]"
echo " # Hi username! You've successfully authenticated..."
echo ""
if [ -f "$HOME/.ssh/id_ed25519.pub" ]; then
echo "=== Current public key ==="
cat "$HOME/.ssh/id_ed25519.pub"
else
echo "(The ~/.ssh/id_ed25519.pub file does not exist. Create it using command 1 above)"
fi
Execution result
=== Instructions on how to create an SSH key ===
1. Generate key: ssh-keygen -t ed25519 ...
...
(The ~/.ssh/id_ed25519.pub file does not exist. Create it using command 1 above)
Bottom line: The SSH key method allows secure authentication without having to enter PAT every time.
Seeing from a different perspective
| Git commands | Metaphor |
|---|---|
git clone |
Copy original document to USB |
git push |
Upload my edits to the server |
git fetch |
Preview server change history |
git pull |
Receive server change details and apply them immediately |
origin |
Server alias associated with my repository |
Common Mistakes
--forceabuse aftergit pushfailure — Team member's commits may disappear; Use--force-with-lease.- Forget
git fetchinstead ofgit pull— Checking the diff after fetch can reduce unexpected conflicts. - Use passwords for HTTPS URLs — As of August 2021, GitHub does not support password authentication; Use PAT or SSH.
- Tracking branches remaining locally after deleting remote branches — Organize with
git fetch --prune. - First push without
-u—git pushdoes not work afterward because upstream is not set.
Cleanup
- Clone the remote repository to
git clone URLandoriginis automatically set. - If you set the upstream to
git push -u origin <branch>, you only need to entergit pushafterwards. git fetchdoes not change the local branch, andgit pullperforms fetch + merge at once.- The SSH key method is more convenient and safer than HTTPS PAT.
Try it yourself
cd 04_remote_storage/src
chmod +x ex01_clone.sh ex02_remote.sh ex03_push_pull.sh ex04_credentials.sh
./ex01_clone.sh
./ex02_remote.sh
./ex03_push_pull.sh
./ex04_credentials.sh
Application:
- Check the difference with the remote using
git diff HEAD origin/mainaftergit fetch origin. - Try deleting the remote branch with
git push origin --delete <branch name>.