Mastering Git & GitHub Essentials
Mastering Git & GitHub Essentials
To Contribute to Open Source: The world of open-source software runs on Git and GitHub. Learning
them allows you to contribute to projects you love, fix bugs, add features, and become part of a global
community.
To Effectively Manage Projects: Whether you're working alone or on a team, Git provides a powerful
way to track changes, experiment with new ideas in branches without risk, and easily revert to a
previous version if something goes wrong. It's like having a complete history of your project at your
fingertips.
To Open New Opportunities: A strong GitHub profile acts as a modern resume for developers. It
showcases your work, your coding style, and your collaboration skills to potential employers. Many
companies will look at your GitHub profile before they even look at your CV.
To Have a Key Skill That Makes You Versatile: Git and GitHub are industry standards. Knowing them is
a fundamental skill expected in almost every software development, DevOps, data science, and even
technical writing role. It makes you a more adaptable and valuable team member.
GitHub: The website where you store your projects online. It's a platform for collaboration and sharing
your Git repositories.
Repository (Repo): A folder for your project that contains all your files and the entire history of changes
( .git folder).
Commit: A "snapshot" or a save point of your project at a specific time. Each commit has a unique ID
and a message describing the changes.
Branch: An independent line of development. You create branches to work on new features or bugs
without disturbing the main, stable version of your code (often called the main branch).
Pull Request (PR): A request to merge your changes from one branch into another (usually your feature
branch into the main branch). It's the starting point for code review on GitHub.
Resources to Learn
Here are some of the best places to master Git and GitHub:
GitHub Skills: Interactive, bot-led courses directly within GitHub. It's a great hands-on way to learn.
The Pro Git Book: The official book, available for free online. It's comprehensive and covers everything
from beginner to advanced topics.
YouTube Channels: Channels like Fireship, The Net Ninja, and FreeCodeCamp have fantastic video
tutorials that walk you through the entire workflow.
Git & GitHub Tutorial | Visualized Git Course for Beginner & Professional Developers in 2024
In this visualized Git Tutorial, you'll learn everything from the basics of Git to advanced tips no one else
talks about. We’ll dive into real-world Git workflows, fixing production issues, and resolving merge
conflicts like a pro. By the end, you'll be your team's go-to Git expert, ready to handle any challenge!
[Link]
[Link]
[Link]
[Link]
[Link]
Git
The source of this book is hosted on GitHub.
Patches, suggestions and comments are welcome.
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Use .gitignore : Create a .gitignore file in your repository to tell Git which files or folders it should ignore
(like log files, dependency folders, or secret keys).
Branch Early, Branch Often: Don't be afraid to create a new branch for even the smallest change. It
keeps your main branch clean and makes your work easier to manage.
Pull Before You Push: Always run git pull to get the latest changes from your team before you git push
Use Aliases: Speed up your workflow by creating shortcuts for common commands in your Git
configuration. For example, git co for git checkout or git st for git status .
Open in CodeSandbox
How it Works: The is a shortcut in Git for the last branch you were on. For example, if you switch from
main to feature-branch , running git checkout - will take you right back to main .
Command Description
git init Initializes a new Git repository in your current folder.
git status Shows the status of your changes.
git add . Stages all files in the current directory for the next commit.
git commit -m "Message" Commits your staged changes with a descriptive message.
git log --oneline Shows a condensed history of your commits.
git switch -c <branch-name> Creates a new branch and switches to it.
git switch <branch-name> Switches to an existing branch.
git merge <branch-name> Merges the specified branch into your current branch.
git push origin <branch-name> Pushes your branch and its commits to the remote repo (GitHub).
git pull origin <branch-name> Pulls the latest changes from the remote repo to your local one.
🟢 Do’s
Commits & Branches
Commit Messages
Body: explain why the change is being made (if not obvious).
Good Practices
Learn Git from the command line first, then explore GUIs.
🔴 Don’ts
Don’t put unrelated changes in a single commit.
Don’t keep separate long-lived dev and main branches — it causes confusion.
Don’t use git add . or git commit -a (they add unintended files).
Instead:
tig
📝 Note: This document reflects personal experiences, insights from colleagues, friends, and various
online resources (Google, YouTube, Reddit, etc.), formatted with AI assistance for better readability. Hope
this helps you on your journey!
- Goutham Sankeerth
Git - Downloads
The entire Pro Git book written
by Scott Chacon and Ben Straub is available to read online for free.
Dead tree versions are available on [Link].
[Link]
1. Configure Git: After installation, open your terminal (or Git Bash on Windows) and introduce yourself to
Git. This information is attached to every commit you [Link]
1. Working Directory: The actual folder with your project files that you are currently editing.
2. Staging Area (Index): A "drafting" area where you prepare the specific changes you want to save in
your next "snapshot" (commit).
3. Repository ( .git directory): The database where Git permanently stores all the snapshots (commits) of
your project's history.
1. Initialize a Repository: Go into your project folder and run this command [Link]
git init
This creates the hidden .git directory, turning your project into a Git repository.
2. Check the Status: See what's happening—which files are new, modified, or [Link]
git status
3. Stage Your Changes: Add the files you want to include in your next [Link]
# Stage a specific file
git add [Link]
4. Commit Your Changes: Save your staged changes to the repository with a descriptive [Link]
git commit -m "Your descriptive message here"
Better Log: Use git log --oneline --graph --all for a clean, visual history of all branches.
Add & Commit: Combine staging and committing with git commit -am "Your message" . This only works for files
that are already tracked (not new files).
4. Merge Your Changes: Once your feature is complete, switch back to your main branch and merge the
feature branch into [Link]
# Go back to the main branch
git switch main
5. Delete the Branch (Optional): After merging, you can clean up by deleting the feature [Link]
git branch -d new-feature
Merge Conflicts: If you and another person edit the same line of the same file on different branches, Git
won't know which version to keep. This is a merge conflict. Git will mark the file, and you'll have to
manually edit it to resolve the conflict before you can complete the merge.
2. Connect Your Local Repo to GitHub: Copy the URL provided by GitHub and link your local repository to
the remote one on GitHub. We usually name the remote origin .Bash
git remote add origin <[Link]>
3. Push Your Code: Send your local commits (from your main branch) to the remote repository
( origin ).Bash
# The -u flag sets the upstream so next time you can just use 'git push'
git push -u origin main
This command is actually two commands in one: git fetch (downloads remote changes) + git merge
3. Push Your Branch: When you're ready to share your new feature branch, push it to the [Link]
git push origin new-feature
4. Create a Pull Request (PR): On GitHub, you'll see a prompt to create a Pull Request for your newly
pushed branch. A PR is a request to merge your changes into the main branch. This is where your team
can review your code, leave comments, and approve the merge.
Forking: If you want to contribute to a project you don't own, you fork it. This creates a personal copy
of the repository on your GitHub account. You then push changes to your fork and create a PR from
your fork to the original project.
Unstage a File: You used git add but want to undo [Link]
git restore --staged <file-name>
Undo Local Changes: Revert a file back to how it was at the last commit (discards your changes!).Bash
git restore <file-name>
Amend the Last Commit: Forgot to add a file or made a typo in the commit message?Bash
# Stage your forgotten file(s) first with git add
git commit --amend
This opens an editor to change the last commit message. Warning: Don't do this on commits you've
already pushed!
Revert a Commit: To safely undo a public commit, create a new commit that does the [Link]
git revert <commit-hash>
2. Branching: switch -c <branch> -> work -> switch main -> merge <branch> .
3. Remote Workflow: pull -> do your work -> push -> create a Pull Request on GitHub.
To keep learning:
Practice: Create your own projects and use Git for everything.
Contribute: Find a small open-source project on GitHub and try to fix a typo or a small bug. It's the best
way to learn the collaboration workflow.
Explore: Learn about git rebase , git cherry-pick , and git stash once you are comfortable with the basics.
[Link]
1. use insert key to enable the writing mode in any 3. Creating a new branch
file
git branch branch_name
2. then after finishing edits, press the left-right arrow 4. Then shift the head to the above branch using the
key to disable the writing mode and then write :x checkout command
to exit out
5. Then stage. Then commit.
git add file_name or git add . (to stage everything in 9. To make forked project even (updated) with the main
the current folder) project
6. Committing the files
11. Bringing back those changes or pop them from the Method 2
stash
1. To fetch all at once
git stash pop
git pull upstream main
12. To clear the changes or files in your stash
2. Then push to the origin url or your forked project
git stash clear
git push origin main
Method 3
How Git works
1. Update using the Fetch Upsteam button on forked
1. Connecting your Remote Repository to Local repo
Repository
10. Squashing all your multiple commits into one commit
git remote add origin insert_https_project_link_here
git rebase -i
2. Pushing local changes to remote repository insert_hash_code_of_commit_above_which_all_your_required_co
3. To view all your remote urls 11. Merge conflicts and how to resolve them
git remote -v 1. They happen when multiple users edit the same
4. Never commit on the main branch since it's the one code line and then push it. Git won't know which
used by the people, to prevent any mishaps one to merge and then there'd be a conflict
5. Shifting the head to a branch (head is the pointer 2. This has to be resolved manually by repo
which points to where all you do your changes) maintainer
git checkout branch_name
[Link]
-[Link]
Git is the free and open source distributed version control system that's responsible for everything GitHub
related that happens locally on your computer. This cheat sheet features the most important and commonly
used Git commands for easy reference.
Git for All Platforms diff of what is changed but not staged
[Link]
git diff --staged
set a name that is identifiable for credit when review version history
git config --global [Link] “[valid-email]”
BRANCH & MERGE
Isolating work in branches, changing context, and integrating changes
set an email address that will be associated with each history marker
git branch
git config --global [Link] auto
list your branches. a * will appear next to the currently active branch
set automatic command line coloring for Git for easy reviewing
git branch [branch-name]
initialize an existing directory as a Git repository merge the specified branch’s history into the current one
git clone [url] git log
retrieve an entire repository from a hosted location via URL show all commits in the current branch’s history
INSPECT & COMPARE SHARE & UPDATE
Examining logs, diffs and object information Retrieving updates from another repository and updating local repos
show the commit history for the currently active branch add a git URL as an alias
git log branchB..branchA git fetch [alias]
show the commits on branchA that are not on branchB fetch down all the branches from that Git remote
git log --follow [file] git merge [alias]/[branch]
show the commits that changed file, even across renames merge a remote branch into your current branch to bring it up to date
git diff branchB...branchA git push [alias] [branch]
show the diff of what is in branchA that is not in branchB Transmit local branch commits to the remote repository branch
git show [SHA] git pull
show any object in Git in human-readable format fetch and merge any commits from the tracking remote branch
delete the file from project and stage the removal for commit apply any commits of current branch ahead of specified one
change an existing file path and stage the move clear staging area, rewrite working tree from specified commit
show all commit logs with indication of any paths that moved TEMPORARY COMMITS
Temporarily store modified, tracked files in order to change branches
system wide ignore pattern for all local repositories discard the changes from top of stash stack
Education
Teach and learn better, together. GitHub is free for students and teach- education@[Link]
ers. Discounts available for other educational uses. [Link]
GIT CHEAT SHEET
Git is the free and open source distributed version control system that's responsible for everything GitHub
related that happens locally on your computer. This cheat sheet features the most important and commonly
used Git commands for easy reference.
Git for All Platforms diff of what is changed but not staged
[Link]
git diff --staged
set a name that is identifiable for credit when review version history
git config --global [Link] “[valid-email]”
BRANCH & MERGE
Isolating work in branches, changing context, and integrating changes
set an email address that will be associated with each history marker
git branch
git config --global [Link] auto
list your branches. a * will appear next to the currently active branch
set automatic command line coloring for Git for easy reviewing
git branch [branch-name]
initialize an existing directory as a Git repository merge the specified branch’s history into the current one
git clone [url] git log
retrieve an entire repository from a hosted location via URL show all commits in the current branch’s history
INSPECT & COMPARE SHARE & UPDATE
Examining logs, diffs and object information Retrieving updates from another repository and updating local repos
show the commit history for the currently active branch add a git URL as an alias
git log branchB..branchA git fetch [alias]
show the commits on branchA that are not on branchB fetch down all the branches from that Git remote
git log --follow [file] git merge [alias]/[branch]
show the commits that changed file, even across renames merge a remote branch into your current branch to bring it up to date
git diff branchB...branchA git push [alias] [branch]
show the diff of what is in branchA that is not in branchB Transmit local branch commits to the remote repository branch
git show [SHA] git pull
show any object in Git in human-readable format fetch and merge any commits from the tracking remote branch
delete the file from project and stage the removal for commit apply any commits of current branch ahead of specified one
change an existing file path and stage the move clear staging area, rewrite working tree from specified commit
show all commit logs with indication of any paths that moved TEMPORARY COMMITS
Temporarily store modified, tracked files in order to change branches
system wide ignore pattern for all local repositories discard the changes from top of stash stack
Education
Teach and learn better, together. GitHub is free for students and teach- education@[Link]
ers. Discounts available for other educational uses. [Link]
Git - 0 to Pro Reference
By: [Link]
Tutorial link: [Link]
Note: git commands must be run inside the folder that contains all the code.
Creating Commits
In Git, version = commit
Version history = commit history
git init Git will start tracking all changes in the current folder
git status Show all changes since the previous commit
GitHub
Repository = a folder containing code where any changes to the code are tracked by git.
(To create a repository, we create a new folder on our computer, and then run git init)
GitHub = a service that lets us save our git repositories online. It also helps us:
- backup our code in case we delete it on our computer
- see the history of our code changes more easily
- alternatives include Bitbucket and GitLab
git push <remote_name> <branch> Upload a branch of your git version history to your
remote repository
git push <remote_name> <branch> -f Force-push the branch to the remote repository (it
will overwrite what's on the remote repository)
git clone <url> <folder_name> Download the repository and give it a different
folder name
git pull <remote_name> <branch> Update the local branch with any updates from
the remote repository (on GitHub)
git pull origin main Downloads any new commits from the main
branch on origin, and updates the local main
branch with those new commits
git pull origin main --set-upstream
^
Sets up a shortcut so that the next time you are on the main branch and run git pull, it will
automatically git pull origin main
Branching
Branching = create a copy of the version history that we can work on without affecting the
original version history. This lets us work on multiple things (features + fixes) at the same time.
Merging
git merge <branch_name> -m "message" Merge the current branch (indicated by HEAD ->)
with another branch (<branch_name>). Saves
the result of the merge as a commit on the
current branch
Merge Conflicts
<<<<<<< HEAD If there is a merge conflict (git doesn't know
code1 what the final code should be), it will add this in
======= your code.
code2
>>>>>>> branch (This is just for your convenience, the <<<<<<<
and >>>>>>> don't have special meaning)
<<<<<<< HEAD
... <-- Code in the current branch (indicated by HEAD ->)
=======
... <-- Code in the branch that is being merged into HEAD
>>>>>>> branch
1. Delete all the extra code and just leave the final code that you want.
<<<<<<< HEAD
code1
======= => code2
code2
>>>>>>> branch
2. If there are conflicts in multiple places in your code, repeat step 1 for all those places.
3. Create a commit.
git add .
git commit -m "message"
3. Create a pull request on GitHub (a pull request lets teammates do code reviews and add
comments).
4. Merge the feature branch into the main branch (by opening the pull request in the browser
and clicking "Merge pull request")
5. After merging, update the local repository (so that it stays in sync with the remote repository
on GitHub).
git checkout main
git pull origin main
We can either:
1. Resolve the merge conflict on GitHub.
3) Merge main into the feature branch (feature4). Notice the direction of the merge: we want
the merge commit to stay on the feature branch so our teammates can review it.
git checkout feature4
git merge master
GitHub Pages
Lets us put websites created with HTML, CSS, and JavaScript code on the Internet
Overall Process
1. Write your HTML, CSS, and JavaScript code on your computer (as normal)
2. Upload your code to a GitHub repository
3. Turn on GitHub Pages from the repository's settings
That's it!
Step By Step
1. Write the code for your website as normal (make sure your website works when you open the
HTML file in your browser)
2. In your browser, go to "[Link]" and sign in
4. If you already have a repository, open it. Otherwise, on the right side click "New"
4a. When creating a new repository:
● Give it a name (any name is fine)
● Make sure it is "Public" (private repositories need to pay a fee to use GitHub Pages)
● Everything else can be left as default
6. Drag and drop the code you want to upload and click "Commit Changes" at the bottom.
❌ Don't drag the folder containing all your code into GitHub
✅ First open the folder, then select all your code and drag and drop into GitHub
10. To visit your GitHub Pages website, enter this URL at the top of your browser:
[your_github_username].[Link]/[repository_name]/[file_name].html
If you don't provide an HTML file, GitHub Pages will look for a file called "[Link]":
[your_github_username].[Link]/[repository_name]
2. We'll link our domain name to GitHub Pages' IP address. Here's the diagram for reference
IP Address
● Is like a phone number (but for the Internet)
● Any device that connects to the Internet is given an IP address. At home, you connect to the
Internet through a router. In this case, only the router will get an IP address that is public
● A device can communicate with another device across the Internet using an IP address
IPv4 example: [Link]
IPv6 example: [Link]
4. Log into your domain registrar > open the "Settings" for your domain.
If you're not using Namecheap, see [Link]
In Namecheap:
● Go to your Dashboard > click "Domain List" on the left
● Find your domain > click "Manage" on the right > click "Advanced DNS"
● Look for this table (that lets us create DNS records):
Create a DNS A Record for each IP address from step 3. Here's the end result:
6. Create a CNAME Record = links a domain name to another domain name.
Set up the subdomain "www" ([Link]). This is recommended by GitHub Pages.
Summary
A Record = [Link] -> ip address
CNAME Record = [Link] -> domain name
Note
A CNAME Record can only be used when there is a subdomain. Some registrars offer an ALIAS
Record which can link the base domain name ("[Link]") to another domain.
Set up Domain Name in GitHub Pages
1. Go to the GitHub repository for your website
3. In the "Custom domain" section, enter your domain name. Click Save
4. Wait for GitHub's process to finish. Then check the box to activate HTTPS
HTTPS encrypts all data flowing between your browser and your website.