Final Version of Github
Final Version of Github
This book is for educational purposes only. The author and publisher are not
responsible for any errors or omissions, or for any outcomes related to the use
of this material.
First Edition
Published by GithubProjects
Copyright 1
!"#$ %&&' #$ ()(#*+,)(
And to the beginners who take the first step, no matter how daunting it seems.
Keep learning, keep growing, and let Git be your guide on this exciting journey.
Dedication 1
Table of Contents
Table of Contents
Introduction
Appendix
For example:
Imagine youʼre writing a book. Every time you finish a chapter, you save it.
Later, you realize you liked the second chapter better the way it was earlier.
With Git, you can quickly go back to that earlier version without redoing
everything.
Git is not just for developers. Writers, designers, and anyone who deals with
files can use it to keep track of their work.
GHI Track ChangesJ Git keeps a detailed history of all changes you make. If
something goes wrong, you can easily undo the mistake.
KHI Work TogetherJ Git allows multiple people to work on the same project
without accidentally overwriting each otherʼs work. This makes teamwork
smooth and stress-free.
MHI Experiment SafelyJ You can try out new ideas in a "test version" of your
project without worrying about breaking anything.
PHI Backup Your WorkJ Git saves a copy of your work. Even if your computer
crashes, your project is safe.
For example:
If you and your friend are working on a website, you can use Git to save
both your changes separately. Git will then merge them together, so nothing
gets lost.
Introduction 1
How Git Simplifies Development and Version Control
Before Git, developers often faced challenges like:
Keeping multiple versions of the same file with names like !"#$%&'(./ ,
!"#$%&'()*+,- , and !"#$%&'()*+,-()*+,- .
Facing problems when two people tried to edit the same file.
Helping you experiment without risk. If something doesnʼt work, you can
easily undo it.
For example:
Youʼre building a to-do list app. You add a new feature where users can set
reminders. Later, you realize the feature is buggy. With Git, you can quickly
go back to the version before you added the feature and fix it.
Setting Up GitU How to install Git and configure it for your projects.
Branching and MergingU How to create test versions of your project and
combine them safely.
Introduction 2
Remote Repositories How to connect your project to platforms like GitHub
and share it with others.
Weʼll use real-life examples and simple commands to explain everything. For
instance:
Youʼll practice saving your work, making changes, and going back to older
versions.
Youʼll see how to work with a friend on the same project without any
confusion.
By the end of this book, you will have the confidence to use Git for your own
projects. Letʼs get started!
Introduction 3
Chapter 1: Setting Up Git
Installing Git (Windows, Mac, Linux)
To use Git, you need to install it on your computer. Hereʼs how you can do it:
For Windows:
@:; Run the downloaded file and follow the installation steps.
B:; During installation, choose the default settings. When asked about your
preferred editor, select one (you can use Notepad or Visual Studio Code if
unsure).
L:; Once installed, open the Command Prompt or Git Bash to check if Git is
installed. Type this command:If you see a version number, Git is installed.
!"# $$%&'(")*
For Mac:
@:; Type the following command to install Git using Homebrew (if you donʼt
have Homebrew, install it first):
!"# $$%&'(")*
For Linux:
@:; Use your package manager to install Git. For example, on Ubuntu, type:
git --version
Example:
If you are setting this up for the first time, it might look like this:
Repository Once youʼre happy with the changes, you save (or "commit")
them to the repository. The repository keeps a history of all changes.
Example Workflow:
Use the following commands to add and save the changes:Now, your
changes are saved in the Git repository.
By the end of this chapter, you should have Git installed, configured, and
understand how the Git workflow operates. You are now ready to start using Git
in your projects!
For example:
Imagine youʼre writing a school project. You save different versions like
!"#$%&'()*+,#&- and !"#$%&'().+,#&- . Version control does this automatically
BCD Distributed SystemN Unlike older systems, Git allows everyone to have a
complete copy of the project. This means you can work offline and still
track changes.
GCD FlexibilityN Git supports many workflows, so you can use it for small or large
projects.
PCD Community SupportN Git is widely used, and platforms like GitHub make it
easy to collaborate and share projects.
For example:
?@A RepositoryB A repository (or "repo") is where Git stores your project. It
keeps all the files and the history of changes.
Example: You create a project folder called !"#$%&'() and turn it into a
Git repository. Git will now track changes in this folder.
L@A CommitB A commit is like saving your work in Git. It records the changes
you made to your files with a message describing what you did.
M@A BranchB A branch is like a separate version of your project. You can use
branches to experiment with new features without affecting the main
project.
N@A MergeB Merging combines changes from one branch into another. This is
useful when your experiment is successful, and you want to add it to the
main project.
Summary of Commands:
Initialize a repo: ,+' +!+'
Initializing a Repository
If youʼre starting a new project, you need to tell Git to start tracking it by
creating a repository.
Navigate to the folder where your project files are stored using the cd
command:
cd path/to/your/proje
git init
Example:
cd MyWebsite
git init
Example:
Note:
Local Git:
git init
git add .
git commit -m "Initial commit"
Using GitHub:
Summary of Commands:
Initialize a repo:
git init
Clone a repo:
With these basics, you can now create new repositories, clone existing ones,
and work locally or with GitHub. In the next chapter, weʼll explore how to track
changes effectively.
Make changes to your file, for example, add some content to index.html .
Example:
You create a file called index.html and add some text. Then, stage the file:
Now,
index.html is staged, ready to be saved (committed).
git add .
Use the git commit command with the m flag to add your message:
Example:
A good message explains what the commit does. This makes it easier to
understand the history of your project later.
git log
commit 1a2b3c4d5e6f
Author: John Doe <john@example.com>
Date: Thu Nov 15 10:00:00 2024
Update index.html with a welcome message
commit 6f5e4d3c2b1a
Author: John Doe <john@example.com>
Date: Wed Nov 14 15:30:00 2024
Add index.html with greeting
git status
Example:
If you havenʼt staged or committed your changes,
git status might say:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be commi
tted)
index.html
Summary of Commands:
Stage files:
git add .
Commit changes:
git log
git status
Now you know how to track changes in Git by staging, committing, and viewing
your project's history. These commands form the foundation of version control
and are used in almost every project.
The main branch (often called main or master ) is your primary project.
Other branches are "copies" where you can experiment without risk.
When your experiment is successful, you combine it with the main branch.
Example Scenario:
Youʼre building a website. The main branch contains the live version. You
create a branch called new-feature to test a new design. If it works, you
merge it back into the main branch.
Example:
Switching to a Branch:
To start working on your new branch, switch to it using the
git checkout or git switch command:
or
Example:
git branch
The active branch will have an asterisk (*) next to its name.
Example Output:
* main
new-feature
Merging Branches
Once youʼre done working on a branch, you can combine its changes into
another branch (usually the main branch). This is called merging.
Example:
Fast-Forward Merge:
If no changes were made to the main branch, Git will do a "fast-forward"
merge. This means it simply moves the main branch pointer to the latest
commit in the
new-feature branch.
Open the file with the conflict. Git marks the conflicting sections like
this:
<<<<<<< HEAD
This is the content from the main branch.
=======
This is the content from the new-feature branch.
>>>>>>> new-feature
Edit the file to keep the changes you want. Remove the conflict markers
( <<<<<<< , ======= , >>>>>>> ).
Summary of Commands
Create a branch:
Switch to a branch:
Merge a branch:
Branches are one of the most powerful features of Git. They make it easy to
experiment, work on multiple features, and collaborate with others. With these
basics, youʼre ready to handle branching in your projects.
Copy the repository URL from the remote platform (e.g., GitHub).
Use the git remote add command to link your local repository:
Example:
Here, origin is the default name Git uses for the remote repository.
git remote -v
u sets the default branch to push to. After the first push, you can
simply use git push .
Example:
Youʼve made changes to your project and want to upload them to GitHub:
git add .
git commit -m "Update homepage design"
git push -u origin main
Output: Your changes are now visible on GitHub in the main branch.
This downloads the changes from the main branch of the remote
repository and merges them with your local branch.
Example:
Output:
If there are conflicts, Git will notify you to resolve them before completing
the pull.
This downloads the updates but doesnʼt merge them automatically. You can
review the changes and decide when to merge.
Example:
Summary of Commands
Connect to a remote repository:
By learning to work with remote repositories, you can share your projects,
collaborate with others, and keep your work backed up online. This is
especially useful for teams or open-source contributions.
Forking a Repository
A fork is a personal copy of someone elseʼs repository. You can fork a
repository, make changes to your copy, and then suggest those changes to the
original repository.
Steps to Fork a Repository:
Example:
git add .
git commit -m "Add a new feature"
Compare your branch with the main branch of the original repository, add a
description of your changes, and submit the pull request.
Once the pull request is approved, click the Merge Pull Request button.
Assign labels (e.g., bug, enhancement) and click Submit New Issue.
Example:
Working on an Issue:
If you want to fix an issue, comment on it to let others know youʼre working
on it.
Make your changes, commit them, and create a pull request to resolve the
issue.
Summary of Commands
Fork a repository:
Use the GitHub interface to create a personal copy of the repository.
Work on an issue:
Create a branch, make changes, and link the pull request to the issue.
Example:
node_modules/
*.log
secrets.txt
Command:
git diff
Example:
You change the content of index.html . Running git diff might show:
- <h1>Welcome to my website</h1>
+ <h1>Hello, Git!</h1>
the changes.
Steps to Revert a Commit:
git stash
This saves your changes and restores the working directory to the last
commit.
Example output:
Summary of Commands
Ignore files:
View differences:
Revert changes:
Stash changes:
These commands are incredibly helpful for managing files, reviewing changes,
and fixing mistakes. Mastering them will make your Git workflow much
smoother and more efficient.
Example:
Fix it:
Merge Conflicts
How to Fix:
Open the file with the conflict. Git marks the conflicting sections like
this:
<<<<<<< HEAD
This is content from your branch.
=======
Edit the file to resolve the conflict and remove the markers ( <<<<<<< ,
======= , >>>>>>> ).
How to Fix:
ls ~/.ssh
cat ~/.ssh/id_rsa.pub
ssh -T git@github.com
What it Means You made changes, but Git isnʼt tracking them.
How to Fix:
Example:
Untracked files:
use "git add <file>..." to include in what will be com
mitted
Use git commit --amend to modify the last commit. This is useful for fixing
commit messages or adding missed files.
Example:
Reset the commit but keep the changes in your working directory:
Example:
Commit Frequently
Save your work often with meaningful commit messages. This makes
debugging easier.
Use branches for new features or bug fixes. Merge them into the main
branch only when ready.
Use .gitignore
Review Changes
git diff
Summary of Commands
Undo a commit:
Revert changes:
Git errors can be intimidating at first, but with practice, youʼll learn to resolve
them quickly. By following these troubleshooting tips and best practices, you
can avoid common pitfalls and keep your workflow smooth.
Cherry-Picking Commits
Sometimes, you may want to apply a specific commit from one branch to
another without merging the entire branch. This is called cherry-picking.
You fixed a bug in one branch and want to apply the same fix to another
branch.
git log
Example:
You fixed a typo in the dev branch and want to apply the same fix to main :
Rebasing Branches
Rebasing rewrites the commit history of a branch. Itʼs useful for keeping your
branch up-to-date with the main branch without creating unnecessary merge
commits.
When to Use Rebasing:
You need to integrate the latest changes from the main branch into your
branch.
Resolve any conflicts if they occur. After fixing conflicts, stage the changes:
Example:
Warning:
Example:
git tag
Example:
You just released version 1.0 of your project. Tag the current commit:
Annotated Tags:
Create a script for the desired hook. For example, create a pre-commit script:
#!/bin/sh
echo "Running pre-commit hook"
chmod +x .git/hooks/pre-commit
Example:
#!/bin/sh
if grep -q "TODO" *; then
echo "Commit failed: Remove TODO comments."
exit 1
fi
These advanced features can make your Git workflow more efficient and
powerful. While they may seem complex at first, practicing these commands
will help you master them over time.
Pro Git by Scott Chacon and Ben Straub is free and covers everything
from basic usage to advanced workflows.
YouTube Tutorials:
Channels like The Net Ninja and Traversy Media have beginner-friendly
Git tutorials.
Online Courses:
Platforms like freeCodeCamp and Udemy have Git courses tailored for
all levels.
Cheat Sheets:
Gitflow Workflow:
Key Branches:
How It Works:
Create feature branches for new work, merge them into develop when
complete, and merge develop into main for releases.
Example Commands:
This is simpler than Gitflow and works well for small teams or projects.
Key Concept:
Each feature or fix gets its own branch. Once itʼs done, itʼs merged into
the main branch.
How It Works:
Use short-lived branches for tasks, merge them into main , and delete
them.
Example Commands:
Find a Project:
Use platforms like GitHub, GitLab, or Open Source Guide to find projects
that match your interests.
Pick an Issue:
Use the GitHub interface to compare your branch with the main branch
and submit a pull request.
Learn Advanced Topics Dive deeper into Git features like rebasing, hooks,
and submodules when youʼre ready.
Git is a versatile tool, and mastering it will greatly enhance your ability to
manage projects and collaborate effectively. Keep exploring, experimenting,
and building!
Practice Use Git daily, even for small projects. The more you use it, the
more natural it becomes.
Experiment Try out advanced features like Git workflows and hooks to
improve your efficiency.
Remember, every expert was once a beginner. Git may seem overwhelming at
first, but with practice, it becomes second nature. Keep building, collaborating,
and improving—youʼre on your way to mastering version control!
Working Directory The folder on your computer where your project files
are located.
Pull Request A request to review and merge changes from one branch into
another.
HEAD A pointer that indicates the current branch and commit youʼre
working on.
Appendix 1
Cheat Sheet of Common Git
Commands
Initialize a Repository:
git init
Clone a Repository:
git status
Commit Changes:
git log
Create a Branch:
Switch to a Branch:
Merge a Branch:
git stash
git stash apply
This is your quick reference guide to Gitʼs key concepts and commands. Keep it
handy as you continue to practice and explore Git!