Clone Repository
git clone https://github.com/user/repo.gitConnect Local Code to GitHub
git remote add origin URL
git branch -M main
git push -u origin mainSet Git User Identity
git config --global user.name "Name"
git config --global user.email "email@x.com"Check Connected URL
git remote -vChange Remote URL
git remote set-url origin NEW_URLSafe Rule for Beginners
git statusCheck Git Status
git statusQuick Daily Workflow
git status
git add .
git commit -m "message"
git push origin mainAdd Files to Git
git add .git add src/pages/Home.jsxCommit Code
git commit -m "updated homepage"Push Code to GitHub
git push origin maingit pushPull Latest Code
git pull origin mainForce Update Local Code
git fetch origin
git reset --hard origin/mainResolve Pull Conflict
# 1. Find <<<<<<< markers in file
# 2. Keep correct code, delete markers
# 3. Save & run:
git add .
git commit -m "resolved conflict"
git push origin mainCheck Current Branch
git branchSwitch Branch
git switch branch-name
# or older command:
git checkout branch-nameCreate New Branch
git switch -c new-branch-nameDiscard Local Changes
git reset --hard
git clean -fdUndo Last Commit (Keep Code)
git reset --soft HEAD~1Undo Last Commit (Delete Code)
git reset --hard HEAD~1Stash Local Changes
git stash
git pull origin main
git stash popFix LF/CRLF Warning
git config --global core.autocrlf trueMost Common Git Commands
| Action | Command |
|---|---|
| Check changes | git status |
| Add all changes | git add . |
| Commit changes | git commit -m "message" |
| Push to GitHub | git push origin main |
| Pull from GitHub | git pull origin main |
| Check GitHub URL | git remote -v |
| Change GitHub URL | git remote set-url origin URL |
| Check branch | git branch |
| Switch branch | git switch branch-name |
| Clone repo | git clone URL |
| Force match GitHub | git fetch origin + git reset --hard origin/main |
Frequently Asked Questions
Git & GitHub Basics
What is the difference between Git and GitHub?
Git is the underlying tool installed on your computer that tracks changes to your files. GitHub is an online service where you host those tracked files so you can share them with others and collaborate.
Do I need to install Git separately?
Yes. Git is a piece of software that must be installed on your computer. GitHub is just a website. You can download Git from git-scm.com.
How do I check if Git is installed?
Open your terminal and run git --version. If it returns a version number (like
git version 2.39.0), it is installed successfully.
What is a repository (repo)?
A repository is simply a folder that Git is actively tracking. It contains all your project files
and a hidden .git folder that stores the history of every change ever made.
GitHub Account & Repository Setup
Should I create the repository first on GitHub or locally?
For beginners, it's easier to create the repository on GitHub first (so it creates the initial
files), and then use git clone to download it to your computer. If you already have
code on your computer, use git init then git remote add origin to
connect it.
Should my repository be Public or Private?
Public: Anyone on the internet can see your code. Great for portfolios and open
source.
Private: Only you and people you explicitly invite can see the code.
Best for client work and proprietary projects.
What should I name my repository?
Use simple, descriptive names in lowercase with hyphens for spaces. For example:
my-portfolio-website or crm-dashboard.
What is README.md?
It is a Markdown text file that serves as the "homepage" of your repository. When someone visits your GitHub repo, this is the first thing they read to understand what the project does.
Uploading Code to GitHub
How do I connect git to my code and upload it?
If you have an existing local folder, follow these steps to connect and upload:
1. git init (Initialize git)
2. git add . (Stage files)
3. git commit -m "first commit" (Save locally)
4. git remote add origin https://github.com/... (Link to GitHub)
5. git push -u origin main (Upload)
What does `git init` do?
It creates a hidden .git folder in your current directory. This turns a normal
folder into a Git repository, allowing Git to start tracking your files.
What does `git add .` do?
It "stages" all modified and new files. This tells Git: "Get these files ready, I want to include them in my next save."
What is a commit?
A commit is a permanent snapshot of your code at a specific point in time. It acts like a save point in a video game that you can always return to.
Why are commit messages important?
They act as a log of what was changed and why. Instead of vague messages like "updated stuff", use clear messages like "fixed login button bug" so your team understands the history.
Authentication & Access
Why is GitHub asking for a token instead of a password?
GitHub removed password authentication for security reasons. You must generate a Personal Access Token (PAT) in your GitHub Settings (Developer Settings) and use that token as your password in the terminal.
What is SSH vs HTTPS?
Both are methods to securely connect to GitHub.
HTTPS: Easier to set up, but
requires entering your token periodically.
SSH: Requires generating a
cryptographic key on your computer once, but you will never have to type a password/token again.
How do I connect VS Code with GitHub?
Open the "Source Control" tab on the left sidebar (it looks like a branching node). Click "Publish to GitHub" or "Initialize Repository". VS Code will prompt you to log into GitHub through your browser automatically.
Common Errors Beginners Face
Why is `git push` failing?
This usually happens when someone else uploaded changes to GitHub that you don't have on your
computer yet. Run git pull origin main first to download their changes, fix any
conflicts, and then try git push again.
What does “remote origin already exists” mean?
You tried to run git remote add origin, but your computer is already connected to a
GitHub URL. If you want to change the URL, use git remote set-url origin NEW_URL
instead.
Why are my files not appearing on GitHub?
Saving files on your computer does not automatically send them to GitHub. You must complete the
full cycle: git add . → git commit -m "..." →
git push origin main.
Why is `.gitignore` important?
It's a text file where you list files or folders that Git should completely ignore (like
node_modules, passwords, or massive databases). This prevents you from accidentally
uploading junk or sensitive data to the internet.