Learn Git and GitHub the Easy Way: A Beginner's Guide
Learn Git and GitHub the Easy Way: A Beginner's Guide
If you're new to coding or just starting to work on real projects, Git can seem confusing. But once you understand the basics, it becomes one of the most powerful tools you'll use every day. This guide will walk you through Git step by step, using simple examples and clear explanations.
๐ What Is Git?
Git is a free tool that helps you:
- Save versions of your code and keep track of every change.
- Go back in time if something breaks or you make a mistake.
- Work with others on the same project without overwriting each other's changes.
Think of Git as a smart "undo" and collaboration system for your code.
๐ ๏ธ How to Set Up Git (First-Time Only)
Before you can start using Git, you need to tell Git who you are. This info gets saved with every change you make, so it's important to set it up properly.
Step 1: Open the Terminal
You need to use the terminal to enter Git commands.
If you're using VSCode:
- Go to the top menu and click:
View > Terminal
Or use the keyboard shortcut:
- Ctrl + ` (on Windows or Linux)
- Cmd + ` (on Mac)
This opens the terminal panel at the bottom of the editor.
If you're using macOS and not VSCode:
- Open the built-in Terminal app.
- Press Cmd + Space, type Terminal, and hit Enter.
The terminal is where you'll type and run Git commands.
Step 2: Set Your Name and Email
Once the terminal is open, run the following commands:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Replace "Your Name"
with your actual name and use the email address you plan to use on GitHub.
What These Commands Do
user.name
anduser.email
tell Git who is making each change.- This information shows up next to each commit.
- The
--global
flag means these settings apply to all Git projects on your computer (you only need to do this once).
Check Your Git Settings
To make sure your info was saved correctly, run:
git config --global --list
This will show your name and email.
To see all Git settings and where they were set from:
git config --list --show-origin
Now you're ready to start using Git in any project!
๐งพ Most Used Git Commands (And What They Do)
Start a New Git Project
git init
This command tells Git to start tracking your project. It creates a hidden .git
folder inside your project. You won't usually touch this folder, but it's where Git saves all the history and settings.
Check What's Going On
git status
This shows the current state of your files โ which ones are new, which ones have been changed, and which ones are ready to be saved (committed). It's a command you'll use a lot.
Add Files to Be Saved
Before you save your changes permanently, you need to stage them using:
git add file.txt
This stages the file file.txt
.
To stage everything in the current folder:
git add .
To stage everything in the entire project (no matter where you are):
git add -A
Save the Changes (Make a Commit)
git commit -m "your message here"
This saves all the staged changes and adds a message so you know what the change was.
Good messages are short and clear, like:
git commit -m "add login form"
git commit -m "fix footer layout"
Avoid unclear messages like:
git commit -m "stuff"
git commit -m "things"
Commit messages help you remember what you did and help others understand your work too.
See Your Project History
git log
This shows all the past commits โ including who made them, the message, and when.
If you want a shorter version:
git log --oneline
This shows one line per commit, so it's easier to scan quickly.
๐ How Git Tracks Files (File States Explained)
Each file in Git can be in one of several states:
- Untracked โ Git doesn't know about the file yet.
- Staged โ You've told Git to include the file in the next save (
git add
). - Committed โ The file has been saved in the project history.
- Modified โ You've changed a file that was already saved.
Understanding these states is helpful when you're figuring out what will be saved and what hasn't yet.
๐งช Try It Yourself: Your First Git Project
Let's go step by step and practice everything.
- Create a folder called
git-tutorial
and open it in VSCode. - Open the terminal (Ctrl + `).
- Run this to start Git in your project:
git init
- Create a file:
echo "Hello Git" > main.txt
- Add the file to the staging area:
git add main.txt
- Save it with a message:
git commit -m "add main.txt"
- Add more files:
echo "New file" > second.txt
git add .
git commit -m "add second.txt"
- See your history:
git log --oneline
Now you've made multiple commits and can review your project's history.
โ๏ธ Making Changes and Adding New Files
Let's try editing and adding at the same time:
- Edit
main.txt
and change the text. - Create a new folder called
notes
. - Inside that folder, create a file:
todo.txt
. - Add and commit both changes:
git add .
git commit -m "update main.txt and add todo.txt"
This shows how Git handles both modified and new files at the same time.
๐งฉ Using Git Inside VSCode
Instead of using the terminal, you can use the Git GUI built into VSCode.
- Click the Source Control icon in the sidebar (it looks like a branch).
- You'll see file changes, and you can:
- Stage files by clicking the "+" button
- Write commit messages in the input box
- Click the checkmark to commit
This is helpful if you're more comfortable with a visual interface instead of using the terminal.
๐งญ Going Back in Time with Git
Every time you commit, Git saves a unique ID (called a hash). You can use it to go back to that version.
- See your history:
git log --oneline
- Copy the hash from a commit.
- Move to that version:
git checkout <commit_hash>
While in this "detached" state, don't make changes. When you're ready to go back to the main version:
git checkout main
โ ๏ธ Important Git Tips
git add .
vs git add -A
git add .
adds changes from the current folder only.git add -A
adds changes from the entire project, even from other folders.
To make sure you don't miss anything, use git add -A
.
Never Touch the .git
Folder
This folder stores Git's internal data. Do not:
- Create folders inside
.git
- Run
git init
inside it - Edit or delete files in it
Doing this can break your project and delete your commit history.
๐ฏ Practice Challenge
- Create a new folder:
my-git-project
. - Open it in VSCode and open the terminal.
- Start Git:
git init
- Create a file and write something inside.
- Add and commit it:
git add .
git commit -m "initial commit"
- Edit the file, create another one, and repeat the process.
- Use
git log --oneline
to see what you've done.
Repeat the process to practice. The more you use Git, the more natural it becomes.
โ What's Next?
Once you're comfortable with these basics, the next step is to learn:
- Branches โ Work on features without touching the main code.
- Merging โ Combine different branches together.
- Rebasing โ Clean up your history.
- Fixing conflicts โ Handle situations when changes overlap.
That's all coming in the next guide.
You're doing great โ Git is your friend now! ๐
Git Branches, Merging, and Rebasing โ A Simple Guide
Now that you know the basics of Git, it's time to learn how to work on different tasks or features without breaking your main project. That's where branches come in. You'll also learn how to merge and rebase changes, which are two ways of bringing branches back together.
๐ฑ What Are Branches?
A Git branch is like a separate copy of your project where you can work without affecting the original. This is helpful when you're adding new features, fixing bugs, or testing ideas.
Common Branch Commands
git branch # List all branches
git branch new-feature # Create a new branch
git checkout main # Switch to another branch
git switch main # Same as checkout (Git 2.23+)
git checkout -b new # Create and switch in one step
git switch -c new # Same as above (Git 2.23+)
๐งช Example: Creating and Switching Branches
Let's walk through it.
-
Open VSCode and create a folder called
git-branches
. -
Open the terminal:
- VSCode: Click
View > Terminal
or useCtrl + `` (Windows/Linux) or
Cmd + `` (Mac). - macOS Terminal: Press
Cmd + Space
, type Terminal, and hit Enter.
- VSCode: Click
-
Start Git:
git init
- Create a file and commit:
echo "main file" > main-01.txt
git add .
git commit -m "add main-01.txt"
- Do the same for another file:
echo "another main file" > main-02.txt
git add .
git commit -m "add main-02.txt"
- Create and switch to a new branch:
git checkout -b feature
- Check current branch:
git branch
- Add a new file in this branch:
mkdir features
echo "feature work" > features/feature-01.txt
git add .
git commit -m "add feature-01.txt"
- Switch back to
main
:
git checkout main
Notice that feature-01.txt
is gone โ that's because each branch has its own files and changes.
โฉ Quick Practice: Create a Bugfix Branch
- Run this to create and switch to a bugfix branch:
git checkout -b bugfix
- Add and commit a new file:
mkdir bugfix
echo "bugfix work" > bugfix/file-01.txt
git add .
git commit -m "add bugfix file"
- Switch back to main:
git checkout main
Again, the new file won't appear here. Each branch is its own world.
๐ Merging Branches
Merging brings changes from one branch into another.
1. Fast-Forward Merge
Happens when the target branch (like main
) has no changes of its own. Git just moves its pointer forward.
Example:
- You are on the
feature
branch and want to mergemain
:
git merge main
If main
hasn't changed, this merge is fast and clean.
2. Three-Way Merge
Used when both branches have made changes. Git creates a new merge commit to combine both histories.
Steps:
- Switch to the branch you want to merge into:
git checkout main
- Make sure you're on the right branch:
git branch
- Merge the feature branch into it:
git merge feature
- See the result:
git log --oneline
Look for something like:
(HEAD -> main, feature)
That means both branches point to the same commit.
๐งช Practice: Merge main
into bugfix
- Switch to
bugfix
:
git checkout bugfix
- Merge
main
(this will create a merge commit):
git merge main
Git may open a text editor (like Vim or Nano) to confirm the commit message.
In VSCode GUI:
- You'll see the message box in the Source Control panel. Just click Commit.
In Vim:
- Press
i
to edit. - Press
Esc
, type:wq
, and hit Enter to save and quit.
In Nano:
- Type your message.
- Press
Ctrl+O
thenEnter
to save. - Press
Ctrl+X
to exit.
- Now go back to
main
and mergebugfix
:
git checkout main
git merge bugfix
- Check history:
git log --oneline
- Check current status:
git status
๐งน Deleting Branches
Once a branch is merged, you can safely delete it.
git branch -d branch-name
If it's not merged yet, Git will stop you from deleting it by accident.
To force-delete:
git branch -D branch-name
Example:
git checkout main
git branch -d feature
git branch -d bugfix
Create a test branch:
git checkout -b test
echo "test" > test.txt
git add .
git commit -m "add test file"
Now delete it from main
:
git checkout main
git branch -d test # Git warns it's not merged
git branch -D test # Force delete
๐ Why Merge main
into Feature?
Sometimes your teammates update main
while you're working on a feature. Merging main
into your branch helps you:
- Get the latest updates
- Solve conflicts early
- Avoid surprises later when merging back
๐ What Is Rebase?
Rebase is another way to bring changes from one branch into another. But instead of merging, it moves your commits and creates a clean, straight timeline.
When to Use It?
- To make history cleaner.
- Avoid on shared/public branches like
main
โ it changes history and can confuse others.
Rebase Example (Instead of Merging)
- Create a new project:
mkdir git-rebase
cd git-rebase
git init
-
Repeat the same steps as before โ make a
main
,feature
, andbugfix
branch. -
In
feature
orbugfix
, instead of merging:
git rebase main
Now your branch will be updated with main
's changes, and your commits will be placed on top.
Compare this to the git-branches
project. The commit history is cleaner, but the result is the same.
Some teams prefer rebase, others prefer merge. At work, you usually follow team rules.
๐งโ๐ป Using VSCode for Branching & Merging
You can do all of this in VSCode without typing commands.
- Use the Source Control panel.
- Click the "..." menu to create branches, switch, and merge.
- When merging, VSCode will often auto-commit for you.
- It's quicker and easier, especially for beginners.
๐ง Git Challenge: Branches & Merging
Try this on your own:
- Create a folder:
my-branch-project
- Open it in VSCode and start Git:
git init
- Add and commit a couple of files in
main
. - Create
feature
andbugfix
branches:
git checkout -b feature
git checkout -b bugfix
- Add different files in each branch and commit them.
- Merge
main
intofeature
, then mergefeature
intomain
. - Do the same with
bugfix
, but try rebase instead of merge. - Try deleting the branches.
- Create a
test
branch, commit something, and practice forced deletion.
โ Summary
- Use branches to safely work on features.
- Use merge to combine branches.
- Use rebase to clean up history (only when it's safe).
- Use VSCode to manage Git without the terminal if you prefer visuals.
With practice, all of this becomes second nature!
Keep practicing โ you're one branch away from mastering Git! ๐ฟ
Git Deep Dive: Config, Ignore, Stash, Reset, and Recovery
Now that you're comfortable with the basics of Git and branches, let's explore some important Git tools and tricks that will make your work easier and safer. In this guide, you'll learn how to:
- Use global and local Git settings
- Ignore files with
.gitignore
- Stash temporary changes
- Understand HEAD and detached HEAD
- Use reflog to recover lost commits
- Undo and reset changes safely
๐ง Git Config (Global vs Local)
When using Git, it's important to tell Git who you are. You can do this globally (for all projects) or locally (just for one project).
Global Git Config
To set your name and email globally, open the terminal and run:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
This will apply to all Git projects on your computer.
Local Git Config
If you want to use a different name or email in just one project, you can set it locally:
git config --local user.name "Another Name"
git config --local user.email "another@email.com"
These settings are stored inside the .git/config
file in your project folder.
You can view local settings with:
git config --local --list
Or open the file .git/config
in any text editor.
Extra: Customize Default Branch Name
By default, Git uses main
or master
as the first branch name. But you can set your own:
git config --global init.defaultBranch shakeAndBake
Now all new Git projects will start with a branch named shakeAndBake
. It's great for teams who follow specific naming rules.
๐ซ Ignoring Files with .gitignore
Some files don't belong in your Git history โ like passwords, log files, or generated files.
Create a .gitignore
file in your project and add filenames or patterns you want Git to ignore.
Example:
.env
node_modules/
*.log
Git will not track these files, and they won't show up in git status
.
Practice: Try It Yourself
- Create a project and run:
git init
- Create a file and commit it:
echo "hello" > main-01.txt
git add .
git commit -m "initial commit"
- Create a
.gitignore
file and add:
.env
- Create a
.env
file with:
SECRET_KEY=supersecret
- Run:
git status
You'll see .env
is ignored โ it won't be staged or committed.
๐ Git Stash: Save Work for Later
Sometimes you need to switch tasks but don't want to commit unfinished work. git stash
helps with that.
- Save changes:
git stash
- Save with a message:
git stash push -m "WIP: fixing button style"
- See stash list:
git stash list
- Reapply the last stash and remove it:
git stash pop
- Reapply without removing:
git stash apply
- Apply a specific stash:
git stash pop stash@{0}
Stash only works if your project has at least one commit. Also, it only saves tracked files.
๐ Understanding HEAD and Detached HEAD
HEAD
HEAD is a pointer that shows where you are in your Git history. Usually, it points to the latest commit on your current branch.
When you make a new commit, HEAD moves forward.
Detached HEAD
If you checkout a specific commit (instead of a branch), HEAD gets "detached."
Example:
git checkout abc123
Now you're not on any branch. Any commits made now will not belong to a branch โ they're "orphaned."
To save your work:
git checkout -b new-branch
That creates a new branch and attaches HEAD to it again.
๐ก Use git log
or git show <hash>
to view a commit instead of checking it out, so you avoid detached HEAD mode.
๐งญ Git Reflog: Recover Lost Commits
Made a mistake? Deleted a branch? Don't panic โ git reflog
can help.
git reflog
This shows everything Git has done โ every branch switch, commit, reset, etc. You can use it to recover lost commits:
- Copy the commit hash from
git reflog
. - Checkout that commit or branch:
git checkout <hash>
git checkout -b recovered-branch
Think of git log
as your official history, and git reflog
as your full activity log.
โป๏ธ Git Reset: Undoing Commits
Git reset helps you undo commits or unstage files.
Undo the Last Commit (keep changes staged):
git reset --soft HEAD~1
Undo and discard changes completely:
git reset --hard HEAD~1
Reset to a specific commit:
git reset --hard <commit-hash>
Unstage a file:
git reset filename.txt
Unstage all files:
git reset
๐ก HEAD1 means "one commit before the current one". HEAD2 would mean "two commits back".
๐ Git Restore and Checkout
To undo changes you haven't staged yet, use:
git restore filename.txt
To discard all unstaged changes:
git restore .
Or use the older version:
git checkout -- filename.txt
git checkout .
Both do the same, but git restore
is newer and easier to read.
๐งน Cleaning Untracked Files
Untracked files are files Git doesn't know about (you haven't added them).
Preview what will be deleted:
git clean -dn
Actually delete untracked files:
git clean -df
Be careful โ this cannot be undone.
๐ฅ Deleting Unmerged Branches
If you try to delete a branch that hasn't been merged:
git branch -d branch-name
Git will stop you.
To force delete:
git branch -D branch-name
The commits are still saved (for a while) and can be recovered using git reflog
.
Tips:
- Merge important changes before deleting
- Create a backup branch if unsure
- Only use
-D
when you're 100% ready
๐ง Git Challenge: Practice Config, Ignore, Stash & Recovery
Try these steps to put everything together:
- Create a folder
my-git-tools
- Open in VSCode, open terminal
- Run:
git init
- Set local name and email:
git config user.name "Your Local Name"
git config user.email "your@local.email"
- Add and commit 3 files (
main-01.txt
,main-02.txt
,main-03.txt
) - Create
.gitignore
with:
temp\*.md
- Create
.temp-1.md
and.temp-99.md
- Run:
git status
- Modify
main-01.txt
but don't commit - Stash your changes:
git stash push -m "work in progress"
- Check stashes:
git stash list
- Apply the stash:
git stash pop
- View commit log:
git log --oneline
- Copy a hash and check it out:
git checkout <hash>
- Make a change and commit
- Realize you're in detached HEAD, save it:
git checkout -b recovery-branch
-
Switch back to
main
-
View reflog:
git reflog
-
Make 2 commits
-
Run:
git reset --soft HEAD~1
-
Edit and re-commit
-
Run:
git reset --hard HEAD~1
-
Confirm changes are gone
-
Stage a change, then unstage it:
git restore --staged filename.txt
- Make more changes, discard them:
git restore .
- Create
temp.txt
and preview clean:
git clean -dn
- Delete it:
git clean -df
โ Summary
- Use local config when working in different teams/projects
- Use .gitignore to keep sensitive and messy files out of Git
- Use stash to save work temporarily without committing
- Understand HEAD and reflog to recover lost work
- Use reset, restore, and clean to undo, fix, or clean things up
These are the everyday tools real developers use to avoid mistakes and recover fast when something goes wrong.
You're now a Git troubleshooting ninja โ stash it, reset it, or reflog your way to safety! ๐ง
Git Merge Conflicts, Rebase, Squash, and Cherry-Pick: Explained Simply
As your Git projects grow, you'll start working with multiple branches โ and sooner or later, you'll run into merge conflicts. Don't panic. Conflicts are normal and fixable. In this guide, we'll explain merge conflicts, how to fix them, and how to work with powerful tools like rebase, squash, and cherry-pick.
๐ฅ What Is a Merge Conflict?
A merge conflict happens when Git can't figure out how to combine changes from two branches. This usually occurs when:
- Two branches changed the same line in a file
- One branch deleted a file while the other edited it
- A file is renamed differently in two branches
- One branch creates a file, the other creates a folder with the same name
- Binary files (like images) are edited differently in each branch
When this happens, Git pauses the merge and marks the file as conflicted โ waiting for you to fix it.
๐ Rebase vs Merge: What's the Difference?
Both git merge
and git rebase
are ways to combine branches, but they behave differently.
๐ Merge (git merge main
from feature
branch)
- Keeps the full branch history
- Creates a new merge commit
- Keeps the context of both branches
How it works:
- Finds the common base commit
- Combines the new changes from both branches
- Creates a merge commit
๐ Rebase (git rebase main
from feature
branch)
- Rewrites history
- No merge commit โ creates a clean, linear history
- Moves your changes on top of the other branch
How it works:
- Temporarily removes your changes
- Updates your branch to match
main
- Re-applies your changes one by one
๐ก Use rebase when you want a cleaner history. Use merge when you want to preserve the full story.
๐งจ Merge Conflicts During Rebase
Rebase also runs into conflicts โ especially if the changes in the base branch touch the same lines you've changed.
What happens:
- Git stops and tells you which files are in conflict
- You must fix the file(s)
- Then run:
git add filename.txt
git rebase --continue
You can also cancel a rebase:
git rebase --abort
๐งบ Squashing Commits
Sometimes you make lots of small commits while working on a feature. Instead of merging all of them into main
, you can squash them into one commit.
1. Squash with merge:
git merge --squash feature
git commit -m "add full feature in one commit"
2. Squash with interactive rebase:
git rebase -i HEAD~n
This opens a list of your last n
commits in a text editor. Change pick
to squash
or s
for the commits you want to combine.
Follow the prompts to write the new, single commit message.
๐ Cherry-Pick
Cherry-pick means copying a single commit from one branch and adding it to another.
This is useful when:
- You want one small change from another branch
- You don't want to merge the whole branch
Steps:
- Find the commit hash:
git log --oneline
- Switch to the target branch:
git checkout cherry-target
- Apply the commit:
git cherry-pick <commit-hash>
- If there are conflicts, resolve them, then run:
git add .
git cherry-pick --continue
You can cherry-pick multiple commits too:
git cherry-pick a1b2c3..f6g7h8
๐ง Git Challenge: Practice Merge Conflicts, Rebase & Cherry-Pick
Try this exercise to put everything together:
๐ง Setup
- Create a new folder:
git-merge-rebase-practice
- Open it in VSCode
- Open the terminal:
View > Terminal
or Ctrl + ` (Windows/Linux)
Cmd + ` (Mac)
- Initialize Git:
git init
- Create and commit a file:
echo "Hello from main" > main.txt
git add .
git commit -m "main.txt initial commit"
๐ Create Conflict
- Create a branch and switch to it:
git checkout -b feature
- Modify
main.txt
(same line), commit:
echo "Feature change" > main.txt
git add .
git commit -m "update main.txt in feature"
- Create another file:
echo "New feature file" > feature-1.txt
git add .
git commit -m "add feature-1.txt"
- Switch back to
main
:
git checkout main
- Modify the same line in
main.txt
differently, commit:
echo "Main branch change" > main.txt
git add .
git commit -m "conflicting change in main"
โ๏ธ Trigger a Merge Conflict
- Merge
main
intofeature
:
git checkout feature
git merge main
Git will stop and show a conflict in main.txt
.
- Open
main.txt
, you'll see:
<<<<<<< HEAD
Feature change
=======
Main branch change
>>>>>>> main
-
Pick the correct version or combine both. Then save and exit.
-
Stage the file:
git add main.txt
- Finish the merge:
git commit
๐งผ Squash the Commits
- Switch to
main
:
git checkout main
- Squash merge
feature
:
git merge --squash feature
git commit -m "squashed feature changes"
๐ Practice Rebase
- Reset to the commit before merge:
git reset --hard HEAD~1
- Switch to
feature
, then rebase:
git checkout feature
git rebase main
Resolve the conflict as before. Then run:
git add main.txt
git rebase --continue
โ๏ธ Interactive Rebase
Make a few small commits:
echo "line1" >> notes.txt
git add . && git commit -m "add line1"
echo "line2" >> notes.txt
git add . && git commit -m "add line2"
Now run:
git rebase -i HEAD~2
Change pick
to squash
for the second commit. Save and confirm the new message.
๐ Cherry-Pick a Commit
- Switch to
feature
and make a new change:
echo "unique update" > cherry.txt
git add .
git commit -m "special change to cherry-pick"
- Copy the commit hash:
git log --oneline
- Switch to
cherry-target
:
git checkout -b cherry-target
- Cherry-pick the commit:
git cherry-pick <hash>
โ Summary
- Conflicts happen when two branches change the same thing.
- Rebase creates a cleaner history, but merge preserves the original timeline.
- Squash helps you combine messy commits into one.
- Cherry-pick lets you copy just one change without merging everything.
With these tools, you're ready to work like a real Git pro โ even when things get messy.
Next time you see conflict markers, don't panic โ you're ready to fix them like a boss. ๐จโ๐ป๐ฉโ๐ป
GitHub for Beginners: Remotes, SSH, Pull Requests & Collaboration
You've learned how to use Git locally. Now let's take things to the next level โ working with GitHub! GitHub is a platform that helps you host your code online, collaborate with others, and track changes as a team.
๐ What is GitHub?
GitHub is the world's largest development platform where:
- You can host your Git repositories
- Collaborate with teammates or open-source communities
- Share code, track issues, and submit pull requests (PRs)
๐ Working with Remotes
When you connect your local Git project to GitHub, you set up a remote. GitHub becomes the online version of your project.
Here are the essential Git remote commands:
1. Add a Remote
git remote add origin https://github.com/username/repo.git
origin
is a nickname for the remote GitHub repo.- The URL is the repo's SSH or HTTPS link.
2. Push Your Changes
git push -u origin main
- Sends your local commits to GitHub.
- Use
-u
to set tracking so future pushes don't need branch names.
For feature branches:
git push -u origin feature-name
3. Fetch Updates (No Merge)
git fetch
- Downloads new data from GitHub but does not change your files.
- Use when you want to review changes before applying them.
4. Pull Changes (Fetch + Merge)
git pull
- Updates your branch with the latest changes from GitHub.
- Combines
fetch
+merge
.
5. Clone a Repo
git clone https://github.com/username/repo.git
- Makes a local copy of a GitHub repo.
- Sets up remotes automatically.
๐ Setting Up SSH with GitHub
Using SSH is a secure and convenient way to connect to GitHub without typing your password every time.
Steps:
- Check for existing SSH keys:
ls -al ~/.ssh
- Generate a new SSH key:
ssh-keygen -t ed25519 -C "you@example.com"
- Copy the public key:
pbcopy < ~/.ssh/id_ed25519.pub
-
Go to GitHub:
- Settings > SSH and GPG keys > New SSH key
- Paste and save
-
Test connection:
ssh -T git@github.com
If you get a success message, you're all set!
Optional: SSH Config File
If needed, add a config file:
touch ~/.ssh/config
Example config:
Host github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
Then run:
ssh-add ~/.ssh/id_ed25519
๐ First GitHub Project
- Create a new folder locally and initialize Git:
git init
- Add a file and commit:
echo "console.log('hello')" > app.js
git add .
git commit -m "initial commit"
-
On GitHub, create a new repository.
-
Connect and push:
git remote add origin git@github.com:username/repo.git
git branch -M main
git push -u origin main
- Make more changes locally and push again:
git add .
git commit -m "second commit"
git push
๐ง Pull Requests (PRs)
Pull Requests let you share changes with others and ask for review.
- Create a new branch:
git checkout -b feature-xyz
-
Make changes and commit.
-
Push the branch to GitHub:
git push -u origin feature-xyz
-
Go to GitHub, click "Compare & pull request".
-
Add a description and create the PR.
Now collaborators can review, suggest changes, or approve it for merging.
๐งช Practice PR with TICKET-22 Branch
- Create a new branch:
git checkout -b TICKET-22
- Edit files:
echo "hello people" >> main-01.txt
echo "hello from ticket-22" > hello.js
echo "app is running..." > app.js
- Make 2 commits:
git add .
git commit -m "TICKET-22 make changes"
git commit -am "TICKET-22 update app.js"
- Merge
main
intoTICKET-22
:
git merge main
- Push to GitHub:
git push -u origin TICKET-22
- Open a pull request.
๐ฌ What If You See "Already up to date"?
That means the branches don't have new changes to merge. Just continue working or open a PR if your branch is ready to merge.
๐ Understanding Remotes and Remote-Tracking Branches
What is a Remote?
A remote is a version of your project stored online (like GitHub).
What is a Remote-Tracking Branch?
When you fetch
, Git updates your local copy of the remote branch (like origin/main
). This lets you see what changed without modifying your current branch.
Check All Branches (Local + Remote)
git branch -a
You'll see something like:
main
remotes/origin/main
remotes/origin/TICKET-22
โก Common Remote Actions
Fetch:
git fetch origin
- Downloads updates but doesn't merge.
Pull:
git pull origin main
- Downloads and merges changes into your current branch.
Push:
git push origin feature-name
- Sends your local commits to GitHub.
โ๏ธ Interactive Rebase and Force Push
Let's say you want to squash commits in your branch:
git rebase -i HEAD~3
Mark some commits as squash
, save, and follow the prompts.
Then push:
git push -f
โ ๏ธ Use force push carefully โ it rewrites history and can overwrite others' work if you're sharing the branch.
๐ Cherry-Picking Commits from Another Branch
Cherry-pick lets you copy a single commit from one branch to another.
Steps:
- Copy commit hash from
git log
- Switch to your target branch:
git checkout checkout
- Cherry-pick the commit:
git cherry-pick <commit-hash>
If Git can't find the commit:
- Run:
git fetch
- Try again
Cherry-pick is useful when you want one feature or fix from another branch without merging the whole thing.
๐ง GitHub Collaboration Challenge
- Create a GitHub repo and clone it in 3 folders:
dev1
,dev2
,dev3
In dev1
:
- Create branch
TICKET-11
- Change
app.js
, commit - Merge
main
into it - Push to GitHub
- Create PR โ Merge using merge
In dev2
:
- Create branch
TICKET-22
- Make 2 commits to
app.js
- Push to GitHub
- Create PR โ Merge using merge
In dev3
:
- Create branch
TICKET-33
- Make 3 commits
- Use interactive rebase to squash
- Rebase onto
main
- Force push to GitHub
- Create PR โ Merge using rebase
๐ Advanced Cherry-Pick Practice
- Clone the repo into
dev4
anddev5
In dev4
:
- Create branch
feature
- Add
feature.txt
- Commit and push
In dev5
:
- Create branch
checkout
- Add
checkout.txt
- Commit
Try to cherry-pick feature.txt
commit โ you'll get an error
Fix it with:
git fetch
Then:
git cherry-pick <commit-hash>
Now the commit can be applied.
โ Summary
- Use
git remote
andpush/pull/fetch/clone
to connect local and remote repos - Use SSH for secure communication with GitHub
- Use branches + PRs for team collaboration
- Use
rebase
to clean up commit history - Use
cherry-pick
to bring over specific changes - Use force-push only when you know what you're doing
You're now ready to work with teams, contribute to open source, and push your projects to the world with GitHub! ๐