Git & GitHub
Git & GitHub
Configuring git ~
git config --global core.autocrlf true
Chain of command:
mkdir Moon
cd Moon
git init
ls -> Printing file and folder
ls -a -> Print Hidden file or folder
rm -rf .git in a Unix-like system (e.g., Linux, macOS) deletes the
.git directory and all its contents recursively and forcefully.
git status
git add . -> add all files
git add *.txt -> add all files matching .txt
git commit -m “texting”
git commit -a -m “skipping the staging area”
rm file2.txt -> removing file
git ls-files in Git lists the files currently tracked by the Git
repository in the index (staging area).
mv file1.txt main.js in a Unix-like system (e.g., Linux, macOS)
renames or moves the file file1.txt to main.js.
gimv file1.txt main.js
git status -s -> short status
git diff --staged
git log
git log --oneline
git show commit_id
git show HEAD
git show HEAD~1 ( 1 step behind )
git show HEAD: file_name
git restore --staged file1.js || .
git restore –-source=HEAD~1 file1.js -> after commit
git bisect start
git switch
.gitignore ->
git branch <branch-name>
git switch -c <branch-name>
git checkout -b <branch-name>
git branch <branch-name>
git merge --abort
git restore --source=feature/send-email --toc.txt
git remote (-v)
git branch
git fetch -> origin -> branch_name
git branch -vv ( to find out divergence )
git switch
git branch -d feature/change-password
git branch -r
git reset or git reset <file-name> ( stage [add kora ] gula ke unstage
niwe ase )
git reset --hard dhile code base (like vs code ) theke o sob changes
gula chole jabee
The command echo hello > file1.txt in a Unix-like system (e.g., Linux,
macOS) writes the word "hello" to a file named file1.txt.
echo world >> file1.txt will append text into the file
🔹 Stash Changes:
git stash
🔹 List Stashes:
git stash list
💡 Use Case:
● Temporarily save changes without committing.
● Switch branches without losing work.
● Avoid merge conflicts before pulling updates.
This command resets your branch to the previous commit and deletes all
changes in your working directory permanently
These Git techniques help you manage commits efficiently when dealing
with merges, history cleanups, and selective changes.
1️⃣ Undoing a Faulty Merge
Sometimes, after merging branches, you might realize the merge was a
mistake. Here’s how to undo it:
🔹 This removes the merge commit completely and resets the branch to
the previous commit.
⚠️ Warning: This will discard uncommitted changes.
If you’ve already pushed the faulty merge, a reset isn’t ideal because
it rewrites history. Instead, use:
1.
2.
3.
4.
Use Case:
If you have a feature branch that is behind main, you can rebase
instead of merging.
Steps to Rebase:
Checkout your feature branch:
git checkout feature-branch
🔹 This moves your commits on top of the latest main commits, making
history cleaner than a merge.
1.
2.
3.
4.
🔹 This applies only the selected commit without merging the entire
branch.
🚀 Summary Table
Action Command Use Case
—---------------------------------------------------------------------
Let’s dive into Git and GitHub! I’ll break this down into the sections
you’ve outlined, explaining each concept clearly and concisely. This
will be a practical guide to get you comfortable with version control
and collaboration. Ready? Let’s go!
---
### 01 Introduction
Git is a distributed version control system that tracks changes in
your code, allowing multiple people to work on a project
simultaneously without overwriting each other’s work. GitHub is a
platform built on top of Git that hosts repositories (repos) online,
making collaboration and sharing easy. Think of Git as the tool and
GitHub as the cloud hub where your work lives.
---
### 02 Workflows
A Git workflow defines how you and your team use Git to manage
changes. Common workflows include:
- **Centralized Workflow**: One main branch (e.g., `main`), everyone
works directly on it.
- **Feature Branch Workflow**: Each new feature gets its own branch,
merged into `main` when done.
- **Gitflow**: Uses multiple branches like `main`, `develop`, and
feature branches for structured development.
Pick a workflow based on your team size and project complexity. For
beginners, start with the Feature Branch Workflow.
---
---
---
### 06 Fetching
Fetching retrieves updates from the remote repo without merging them
into your local work:
```bash
git fetch origin
```
Use this to see what’s new on the remote (e.g., `origin/main`) without
affecting your local branches.
---
### 07 Pulling
Pulling fetches updates *and* merges them into your current branch:
```bash
git pull origin main
```
Run this when you want to sync your local branch with the remote
(e.g., `main`).
---
### 08 Pushing
Pushing sends your local commits to the remote repo:
1. Stage changes: `git add .`
2. Commit: `git commit -m "Your message"`
3. Push:
```bash
git push origin <branch-name>
```
Replace `<branch-name>` with your current branch (e.g., `main` or
`feature-x`).
---
---
---
### 11 Releases
Releases are GitHub’s way to package tagged versions:
1. Go to your repo > “Releases” tab.
2. Click “Draft a new release.”
3. Enter the tag (e.g., `v1.0`), title, and description.
4. Optionally attach files (e.g., binaries).
5. Publish! Users can now download it.
---
---
---
---
### 16 Issues
Issues track tasks or bugs:
1. Go to your repo > “Issues” tab.
2. Click “New issue,” add a title and description.
3. Assign it to someone or add labels/milestones.
4. Close it when resolved.
---
### 17 Labels
Labels categorize issues/PRs (e.g., “bug,” “enhancement”):
1. Go to “Issues” > “Labels.”
2. Create or edit labels with names, colors, and descriptions.
3. Apply them to issues/PRs for organization.
---
### 18 Milestones
Milestones group issues into goals (e.g., “v1.0 Release”):
1. Go to “Issues” > “Milestones.”
2. Create a milestone with a title, due date, and description.
3. Link issues/PRs to it.
---
---
---
---
### 22 Collaboration Using GitKraken
GitKraken is a visual Git client:
1. Open your repo in GitKraken.
2. Use the GUI to branch, commit, push, and pull.
3. Manage PRs and collaborate with its GitHub integration.
---
### Exercises
1. Create a GitHub repo, clone it, and push a “Hello World” file.
2. Add a collaborator and have them push a change.
3. Create a branch, make a PR, and merge it.
4. Fork a public repo, update it, and submit a PR.
---
### Summary
Git and GitHub are powerful tools for version control and
collaboration. Git handles the local tracking and branching, while
GitHub adds remote storage, PRs, and team features. Start small—clone,
commit, push—then explore branches, PRs, and open-source
contributions. Practice makes perfect!
Let me know if you want to dive deeper into any section or try a
hands-on example!
Collaboration
Cloning a repository
-> git clone url
Syncing with remotes
-> git fetch origin master
-> git fetch origin
-> git fetch
-> git pull
-> git push origin master
-> git push
Sharing tags
-> git push origin v1.0
-> git push origin —delete v1.0
Sharing branches
-> git branch -r
-> git branch -vv
-> git push -u origin bugfix
-> git push -d origin bugfix
Managing remotes
-> git remote
-> git remote add upstream url
-> git remote rm upstream
—---------------------------------------------------------------------
Let's break down Revert, Reset, and Squash Commits in a simple way!
1. Revert Changes
● What is it?
Reverting is like undoing a commit but keeping the history
intact.
It creates a new commit that reverses the changes of an old
commit.
● When to Use:
Use git revert when you want to undo a change without losing
history. It's helpful for fixing mistakes in previous commits.
●
● Key Point:
Reverting doesn’t delete the original commit, it just creates a
new one that undoes the changes.
2. Reset
● What is it?
git reset is like rewinding your history to a previous commit.
It can affect your working directory and commit history.
● Types of Reset:
○ After --soft reset, all the changes from B and C are staged
as if they were just made.
● When to Use:
3. Squash Commits
● What is it?
Squashing commits means combining multiple commits into one.
This helps keep your commit history clean and readable,
especially when you've made small, incremental changes.
● When to Use:
Change it to:
pick A
squash B
squash C
squash D
After saving and exiting the editor, Git will combine B, C, and D into
a single commit. The history will look like:
A --- BCD (a new squashed commit)
●
● Key Point:
Summary
What is Cherry-Picking?
Example Scenario
● You realize commit E contains a useful bug fix, and you only want
that commit in main (without bringing F).
● Instead of merging feature into main, you cherry-pick commit E:
How to Cherry-Pick?
1.Find the commit hash (from feature branch): git log --oneline
feature
Example output:
-> f5d3c21 (HEAD -> feature) Add feature F
Final Result
✅ When you only need one or a few commits from another branch
✅ When merging the entire branch is not needed or possible
✅ When applying hotfixes or urgent bug fixes from one branch to
another
Key Takeaways