Git is a distributed version control system. Instead of relying on a single central server, each developer clones a full copy of the repository (including history) to their local machine. The original repository can live on a self-hosted server or a hosting service like GitHub.
Sign up at: https://github.com/
macOS / Linux / Windows (Git Bash / PowerShell):
git --version
Every commit records your name and email.
git config --global user.name "Your_UserName"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global --list # verify settings
Follow GitHub’s guide: https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository
Tip: Create it empty (no README/license) to avoid merge conflicts on your first push.
Navigate to the folder you want under Git, then initialize:
cd ~/Desktop/your_directory
git init
touch README.md
echo "My first repo" >> README.md
Stage and commit:
git add .
git commit -m "First commit"
⚠️ Important: GitHub no longer supports password authentication for Git operations. You must use either a Personal Access Token (PAT) or SSH key.
-
Generate a Personal Access Token (PAT):
- Go to: https://github.com/settings/tokens
- Choose Fine-grained token (recommended) or Classic token
- Scope: allow repo (read and write)
-
Set the remote:
git remote add origin https://github.com/<your-username>/<repo-name>.git
-
Push your local commits:
git branch -M main git push -u origin main
-
When prompted:
- Username: your GitHub username (e.g.,
sudipdhakal1
) - Password: your PAT (paste the token here; you won’t see it on screen)
- Username: your GitHub username (e.g.,
-
(Optional) To store your token securely on macOS:
git config --global credential.helper osxkeychain
🔒 Never paste your token directly into commands or share it. If you ever expose your token, revoke it immediately in GitHub settings and generate a new one.
Generate an SSH key and add it to GitHub:
ssh-keygen -t ed25519 -C "you@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Copy the public key to GitHub:
pbcopy < ~/.ssh/id_ed25519.pub # macOS
# or clip < ~/.ssh/id_ed25519.pub (Windows)
Then set your remote and push:
git remote add origin git@github.com:<your-username>/<repo-name>.git
git branch -M main
git push -u origin main
Test connection:
ssh -T git@github.com
If successful, you’ll see:
Hi <username>! You've successfully authenticated.
git remote -v # verify current
git remote set-url origin <new-url>
Examples:
# switch to HTTPS
git remote set-url origin https://github.com/<your-username>/<repo-name>.git
# switch to SSH
git remote set-url origin git@github.com:<your-username>/<repo-name>.git
On GitHub, click Code → copy the HTTPS or SSH URL, then:
cd /path/to/your/folder
git clone https://github.com/<username>/<repository-name>.git
cd <repository-name>
git status
git add -A
git commit -m "Describe what changed"
git push
Invite a collaborator:
- Go to your repo → Settings → Collaborators → Add people
Work on a branch:
git checkout -b feature/update-readme
git add .
git commit -m "Updated README with secure token instructions"
git push -u origin feature/update-readme
Then open a Pull Request on GitHub to merge changes into main
.
If your local and remote branches differ (e.g., GitHub created a README online):
git pull --rebase origin main
git push
Handle line endings:
# Windows
git config --global core.autocrlf true
# macOS/Linux
git config --global core.autocrlf input
If you see conflict markers like:
<<<<<<< HEAD
...
=======
...
>>>>>>> some-commit-hash
Edit to keep the correct content, then:
git add <file>
git commit -m "Resolve merge conflict"