Table of Contents
Github
GitHub is a cloud-based platform where you can store, share, and work together with others to write code.
Storing your code in a “repository” on GitHub allows you to:
- Showcase or share your work.
- Track and manage changes to your code over time.
- Let others review your code, and make suggestions to improve it.
- Collaborate on a shared project, without worrying that your changes will impact the work of your collaborators before you’re ready to integrate them.
Collaborative working, one of GitHub’s fundamental features, is made possible by the open-source software, Git, upon which GitHub is built.
understandable comparison example: Microsoft’s Word’s.
About Git
Git is a version control system that intelligently tracks changes in files. Git is particularly useful when you and a group of people are all making changes to the same files at the same time.
Typically, to do this in a Git-based workflow, you would:
- Create a branch off from the main copy of files that you (and your collaborators) are working on.
- Make edits to the files independently and safely on your own personal branch.
- Let Git intelligently merge your specific changes back into the main copy of files, so that your changes don’t impact other people’s updates.
- Let Git keep track of your and other people’s changes, so you all stay working on the most up-to-date version of the project.
understandable comparison example: Google Doc’s.
How does Git and GitHub Work Together?
When you upload files to GitHub, you’ll store them in a “Git repository.” This means that when you make changes (or “commits”) to your files in GitHub, Git will automatically start to track and manage your changes.
There are plenty of Git-related actions that you can complete on GitHub directly in your browser, such as creating a Git repository, creating branches, and uploading and editing files.
However, most people work on their files locally (on their own computer), then continually sync these local changes—and all the related Git data—with the central “remote” repository on GitHub. There are plenty of tools that you can use to do this, such as GitHub Desktop.
Once you start to collaborate with others and all need to work on the same repository at the same time, you’ll continually:
- Pull all the latest changes made by your collaborators from the remote repository on GitHub.
- Push back your own changes to the same remote repository on GitHub.
Git figures out how to intelligently merge this flow of changes, and GitHub helps you manage the flow through features such as “pull requests.
Git Concepts
What is Git? Git is a distributed version control system that tracks changes in files and coordinates work among multiple people. It maintains a complete history of your project and allows you to revert to previous versions.
Core Concepts:
- Repository (Repo): A directory containing your project files and Git’s tracking information
- Working Directory: The current state of files you’re editing
- Staging Area: A temporary area where changes are prepared before committing
- Commit: A snapshot of your project at a specific point in time
- Branch: An independent line of development
- HEAD: A pointer to the current branch/commit you’re working on
Essential Commands:
- `git init`: Initialize a new Git repository- `git add <file>`: Add files to staging area- `git commit -m "message"`: Save changes with a descriptive message- `git status`: Check current state of files- `git log`: View commit history- `git diff`: Show changes between versionsGit Config
This is important for version control systems, as each Git commit uses this information:
User Name:
Your name will be attached to your commits. Set it with:
git config --global user.name "Your Name"Email Address:
Your email is also attached to your commits. Set it with:
git config --global user.email "you@example.com"Configuration Levels:
There are three levels of configuration:
-
System (all users): git config —system
-
Global (current user): git config —global
-
Local (current repo): git config —local
Viewing Your Configuration:
You can see all your Git settings with:
git config --listNote:
Use —global to set the value for every repository on your computer.
Use —local (the default) to set it only for the current repository.
Q. Why Configure Git?
Git uses your name and email to label your commits.
If you do not set these, Git will prompt you the first time you try to commit.
Now you have added the minimum of configuration needed to start using Git.
So feel free to continue with the next chapter.
Getting Started with Git
Now that we are in the correct folder, we can initialize Git on that folder:
git initQ. What Happens When You Run git init?
Git creates a hidden folder called .git inside your project.
This is where Git stores all the information it needs to track your files and history.
Now that Git is installed, and it knows who you are, you can start using Git.
Lets create our first repository
Key Steps to Get Started:
-
Create a project folder
-
Navigate to the folder
-
Initialize a Git repository
Git Staging Environment(Staging Area)
The staging environment (or staging area) is like a waiting room for your changes.
You use it to tell Git exactly which files you want to include in your next commit.
This gives you control over what goes into your project history.
Here are some key commands for staging:
- git add
- Stage a file
To add a file to the staging area, use git add
git add index.html- git add —all or git add -A - Stage all changes
You can stage all changes (new, modified, and deleted files) at once:
git add --all- git status - See what is staged
See which files are staged and ready to commit:
git status- git restore —staged
- Unstage a file
If you staged a file by mistake, you can remove it from the staging area (unstage it) with:
git restore --staged index.htmlGit Commit
A commit is like a save point in your project.
It records a snapshot of your files at a certain time, with a message describing what changed.
You can always go back to a previous commit if you need to.
Here are some key commands for commits:
- git commit -m “message” - Commit staged changes with a message
To save your staged changes, use git commit -m “your message”:
git commit -m "First release of Hello World!"- git commit -a -m “message” - Commit all tracked changes (skip staging)
you can skip the staging step for already tracked files with git commit -a -m “message”.
git commit -a -m "Quick update to README"Other Useful Commit Options
Create an empty commit:git commit --allow-empty -m "Start project"Use previous commit message (no editor):git commit --no-editQuickly add staged changes to last commit, keep message: git commit --amend --no-edit- git log - See commit history
To view the history of commits for a repository, you can use the git log command:
git logFor a shorter view, use
git log --onelineGit Tags
A tag in Git is like a label or bookmark for a specific commit.
Tags are most often used to mark important points in your project history, like releases (v1.0 or v2.0).
Tags are a simple and reliable way to keep track of versions and share them with your team or users.
- git tag
- Create a lightweight tag
git tag v1.0- git tag -a
-m “message” - Create an annotated tag
An annotated tag stores your name, the date, and a message.
git tag -a v1.0 -m "Version 1.0 release"- git tag
- Tag a specific commit
You can tag an older commit by specifying its hash:
git tag v1.1 1a2b3c4d- git tag - List tags
git tag- git show
- Show tag details
See details about a tag and the commit it points to:
git show v1.0Git Stash
Sometimes you need to quickly switch tasks or fix a bug, but you’re not ready to commit your work.
git stash lets you save your uncommitted changes and return to a clean working directory.
You can come back and restore your changes later.
Here are some common use cases:
-
Switch branches safely: Save your work before changing branches.
-
Handle emergencies: Stash your work to fix something urgent, then restore it.
-
Keep your work-in-progress safe: Avoid messy commits or losing changes.
Key Commands for Stashing
- git stash
Save your current changes (both staged and unstaged tracked files) with:
git stashThis command saves your changes and cleans your working directory so you can safely switch tasks or branches.
Your changes are now saved in a stack.
note:
What is a stash stack?
Each time you run git stash, your changes are saved on top of a “stack”.
The most recent stash is on top, and you can apply or drop stashes from the top down, or pick a specific one from the list.
- git stash push -m “message”
Add a message to remember what you stashed:
git stash push -m "WIP: homepage redesign"- git stash list
See all your saved stashes:
git stash list- git stash show
See what was changed in the latest stash:
git stash show- git stash apply
Restore your most recent stashed changes (keeps the stash in the stack):
git stash apply- git stash apply stash@{n}
This command lets you restore a specific stash from your list, not just the most recent one.
git stash apply stash@{1}- git stash pop
Apply the latest stash and remove it from the stack:
git stash pop- git stash drop
Delete a specific stash when you no longer need it:
git stash drop stash@{0}- git stash clear
Delete all your stashes at once:
git stash clear- git stash branch
Create a new branch and apply a stash to it.
Useful if your stashed work should become its own feature branch:
git stash branch new-feature stash@{0}Git Branch
In Git, a branch is like a separate workspace where you can make changes and try new ideas without affecting the main project.
Think of it as a “parallel universe” for your code.
Q. Why Use Branches?
Branches let you work on different parts of a project, like new features or bug fixes, without interfering with the main branch.
Common Reasons to Create a Branch
-
Developing a new feature
-
Fixing a bug
-
Experimenting with ideas
Key Commands
- Creating a Branch
Let’s say you want to add a new feature. You can create a new branch for it.
Let add some new features to our index.html page.
We are working in our local repository, and we do not want to disturb or possibly wreck the main project.
So we create a new branch:
git branch hello-world-images- Listing All Branches
Let’s confirm that we have created a new branch.
To see all branches in your repository, use:
git branch- Switching Between Branches
checkout is the command used to check out a branch.
Moving us from the current branch, to the one specified at the end of the command:
git checkout hello-world-images
or
git switch hello-world-images- Deleting a Branch
When you’re done with a branch, you can delete it:
git branch -d hello-world-imagesGit Undo
Git Revert
Q. What Does Git Revert Do?
The git revert command undoes a previous commit by creating a new commit that reverses the changes. Git Revert Always creates new commits.
This keeps your commit history intact and is the safest way to undo changes in a shared repository.
- git revert HEAD - Revert the latest commit
Reverts the most recent commit (the one currently pointed to by HEAD).
- Creates a new commit that undoes all changes introduced by the latest commit
- Safe to use on shared/public branches (doesn’t rewrite history)
- Keeps the original commit in history (just adds an opposite change)
git revert HEAD- git revert
- Revert a specific commit
Reverts any specific commit — not just the latest one.
# Revert the commit with hash abc1234git revert abc1234You are Assigned To visitor Kiara Hinton
# Revert an older commit (you can find it with git log)git revert 9f2a1d5- git revert HEAD~2 - Revert a commit further back in history
Reverts the commit that is two steps before the current HEAD.
git revert HEAD~2 # same as: git revert HEAD^^git revert HEAD~5 # reverts commit from 5 steps ago- git revert —no-edit - Skip commit message editor
Skips opening the commit message editor and uses the default generated message.
git revert HEAD --no-edit
git revert abc1234 --no-edit- git log —oneline - Show commit history
Shows a compact, one-line-per-commit view of the commit history — very popular.
git log --oneline
# Typical output:a1b2c3d Fix login buge4f5g6h Add user profile page9h8i7j6 Merge branch 'feature/auth'...Git Reset
git reset is a powerful command that moves HEAD (and usually the current branch) to a different commit.
What happens to your changes in the staging area and working directory depends on the mode you choose.
Git divides your changes into three areas:
-
Working directory → actual files you edit
-
Staging area (index) → changes prepared for commit (
git add) -
Commit history → committed snapshots
Q. What Does Git Reset Do?
The git reset command moves your current branch (HEAD) to a different commit.
- git reset —soft
- Move HEAD to commit, keep changes staged
It moves HEAD to the specified commit, but keeps all your changes staged (in the index).
This is useful if you want to combine several commits into one, or just want to rewrite history but keep your work ready to commit.
git reset --soft 9a9add8All changes after 9a9add8 are now staged, ready for a new commit.
- git reset —mixed
- Move HEAD to commit, unstage changes (default)
git reset —mixed
This is the default option and is useful if you want to “undo” a commit but keep your changes for editing or recommitting.
git reset --mixed 9a9add8- git reset —hard
- Move HEAD to commit, discard all changes
Moves HEAD and discards everything after that commit — the most dangerous variant.
git reset --hard HEAD~1 # Throw away last commit + its changesgit reset --hard origin/main # Reset everything to match remote branch- git reset
- Unstage a file
Unstages a specific file (removes it from the index), but does not touch the working directory.
git reset HEAD docs/README.mdgit reset main.py # modern equivalent: git restore --staged main.pyGit Amend
Q. What is Git Amend?
Git Amend is a command that allows you to modify the most recent commit.
You can use it to fix typos, add or remove files, or change the commit message.
Q. When to Use Git Amend?
Use Git Amend when you need to make small changes to your last commit.
It’s perfect for fixing mistakes, adding forgotten files, or updating the commit message.
- Fix Last Commit Message
git commit --amend -m "Corrected commit message"- Add Files to Last Commit
git add forgotten.txt
git commit --amend- Remove Files from Last Commit
git reset HEAD^ -- unwanted.txt
git commit --amendGit Rebase
Q. What is Git Rebase?
Rebasing moves or combines a sequence of commits to a new base commit.
It is often used to keep a clean, linear project history.
Rebasing can make your commit history easier to read by avoiding unnecessary merge commits.
Q. When to Use Git Rebase
Use Git Rebase to:
a. Keep a clean, linear project history b. Avoid unnecessary merge commits c. Combine multiple commits into one d. Edit or reorder commits
- Basic Rebase
To move your current branch on top of another branch (e.g., update your feature branch with latest main):
git checkout feature-branch
git rebase mainThis reapplies your feature branch changes on top of the latest main branch.
- Interactive Rebase
git rebase -i
This is useful for cleaning up your commit history before sharing it with others.
git rebase -i HEAD~3This opens an editor where you can:
a. pick: keep the commit as is
b. squash: combine commits together
c. edit: pause to change a commit
d. reword: change just the commit message
Follow these steps:
-
Edit the commit message or choose an action (pick, squash, edit, reword)
-
Save and close the editor
-
Git will apply the changes and let you review the results
-
Continue
If you hit a conflict or need to finish editing a commit, use git rebase —continue after resolving the issue.
This tells Git to keep going with the rebase process.
git add fixed_file.txt
git rebase --continue- Abort
If something goes wrong or you want to stop the rebase, use git rebase —abort.
This will put your branch back to how it was before you started rebasing.
git rebase --abortGit Reflog
Q. What is Git Reflog?
git reflog records updates to the tip of branches and HEAD.
It lets you see where your branch and HEAD have been, even changes you made by mistake.
This is useful for recovering lost commits or undoing a reset.
Q. When to Use Git Reflog?
Use git reflog when you need to:
a. Recover lost commits or changes
b. Undo a reset or a merge
c. See the history of your branch and HEAD
- Show the Reflog
To see the history of where HEAD and branches have pointed, use:
git reflog- Find and Recover Lost Commits
If you accidentally reset or deleted commits, you can use the reflog to find the commit and restore it.
Each entry in the reflog has a reference like HEAD@{2}.
git refloge56ba1f (HEAD -> master) HEAD@{0}: commit: Revert "Just a regular update, definitely no accidents here..."52418f7 HEAD@{1}: commit: Just a regular update, definitely no accidents here...9a9add8 (origin/master) HEAD@{2}: commit: Added .gitignore81912ba HEAD@{3}: commit: Corrected spelling error...git reset --hard HEAD@{2}HEAD is now at 9a9add8 Added .gitignore- Clean Up the Reflog
The reflog is automatically cleaned by Git, but you can manually expire old entries if needed:
git reflog expire --expire=30.days refs/heads/maingit gc --prune=nowCounting objects: 15, done.Compressing objects: 100% (10/10), done.Pruning objectsGit Recovery
Q.What is Git Recovery?
Git recovery means getting back lost commits, branches, or files.
Git keeps a record of recent changes so you can undo mistakes—even after a reset or delete.
Use Git recovery when you:
a. Accidentally delete a branch or file
b. Reset your branch to a previous commit and lose changes
c. Need to recover lost commits or changes
- Restore a Deleted Branch
if you deleted a branch but the commits are still in reflog, you can recreate it:
git checkout -b branch-name <commit-hash>
Switched to a new branch 'branch-name'This brings back the branch at the commit you specify.
- Recover a Deleted or Changed File
If you deleted or changed a file and want to get it back, use git restore:
git restore filename.txt- Recover from a Hard Reset
If you used git reset —hard and lost commits, you can use the reflog to find and restore them:
git refloge56ba1f (HEAD -> master) HEAD@{0}: commit: Revert "Just a regular update, definitely no accidents here..."52418f7 HEAD@{1}: commit: Just a regular update, definitely no accidents here...9a9add8 (origin/master) HEAD@{2}: commit: Added .gitignore81912ba HEAD@{3}: commit: Corrected spelling error3fdaa5b HEAD@{4}: merge: Merge pull request #1 from w3schools-test/update-readme836e5bf HEAD@{5}: commit: Updated readme for GitHub Branches...git reset --hard HEAD@{2}HEAD is now at 9a9add8 Added .gitignoreGit Advanced
Git .gitignore
The .gitignore file tells Git which files and folders to ignore (not track).
- Ignoring Folders
To ignore a folder and everything inside it, use a trailing slash:
temp/- Negation (!)
Use ! to not ignore something that would otherwise be ignored. This is called an exception:
*.log!important.logThis ignores all .log files except important.log.
- Comments and Blank Lines
Lines starting with # are comments and are ignored by Git. Blank lines are also ignored. Use comments to explain your rules:
# Ignore log files*.log
# Ignore temp folderstemp/- How to Stop Tracking a File
if you add a file to .gitignore but Git is still tracking it, you need to tell Git to stop:
git rm --cached filename.txtGit LFS
Git LFS (Large File Storage) is an extension for Git that helps you manage large files (like videos, images, or datasets) efficiently.
Instead of storing big files directly in your repository, LFS stores a small pointer file in your repo and keeps the real content on a separate LFS server.
This keeps your repository fast and small, even if you work with huge files.
Everyone who clones the repo gets the pointer, and Git LFS fetches the real file content as needed.
git lfs installGit Cherry-pick & Patch
Q. What is Cherry-pick?
Cherry-pick lets you copy a single commit from one branch to another. It’s useful when you want just one (or a few) changes, not everything from another branch.
Q. What is a Patch?
A patch is a file with changes from one or more commits. You can share a patch or apply it to another repository, even if it’s unrelated to your own.
- How to Cherry-pick a Commit
Copy a specific commit from another branch to your current branch:
git cherry-pick abc1234- Edit the Commit Message
Use —edit to change the commit message while cherry-picking:
git cherry-pick abc1234 --edit- Apply Without Committing
Use —no-commit (or -n) to apply the changes, but not create a commit yet. This lets you make more changes before committing:
git cherry-pick abc1234 --no-commit- How to Create a Patch
Make a patch file from a commit:
git format-patch -1 abc1234- How to Apply a Patch
Apply a patch file to your current branch:
git apply 0001-some-change.patchGit CI/CD
Q. What is CI/CD?
CI/CD stands for Continuous Integration and Continuous Deployment/Delivery.
It means your code is automatically tested and deployed every time you push.
This helps you catch bugs early and deliver features faster, with less manual work.
Q. Why Use CI/CD?
CI/CD automates the process of testing and deploying your code. This means:
a. Find bugs before they reach users
b. Deploy changes faster and more safely
c. Reduce manual steps and mistakes
d. Get quick feedback on every push
Popular CI/CD Services
GitHub Actions: Built into GitHub, uses YAML files in .github/workflows/
GitLab CI/CD: Built into GitLab, uses .gitlab-ci.yml
CircleCI: Works with GitHub/GitLab, easy setup for many languages
Travis CI: Popular for open-source, uses .travis.yml
Azure Pipelines: Works with Azure DevOps and GitHub, supports many platforms
Git Hooks
Q. What are Git Hooks?
Git hooks are scripts that run automatically when certain Git events happen, like making a commit or pushing code.
Q. Why Use Hooks?
Hooks help you automate repetitive tasks, enforce coding standards, and catch problems early.
For example, you can:
.Run tests before every commit or push
.Check code style automatically
.Block bad commit messages
.Enforce rules for everyone on your team
Q. Where Do Hooks Live?
Hooks are stored in .git/hooks inside your repository.
Git Advanced Remote
Q. What Are Git Remotes?
Remotes are references to remote repositories.
They let you collaborate, fetch, and push code to shared projects on services like GitHub, GitLab, or Bitbucket.
Q. Why Use Multiple Remotes?
You can add more than one remote to your project. This is useful for:
a. Collaborating with different teams (e.g., your fork and the main project)
b. Mirroring repositories
c. Maintaining backups
- How to Add a Remote
git remote add upstream https://github.com/other/repo.git- How to Remove a Remote
git remote remove upstream- How to Rename a Remote
To change the name of an existing remote (for example, renaming origin to main-origin):
git remote rename origin main-origin- How to List All Remotes
git remote -v- How to Show Remote Details
git remote show upstream- How to Fetch from a Remote
git fetch upstream- How to Push to a Remote
git push upstream main