Git and GitHub Documentation
1.
Introduction — What are Git and GitHub?
Git
is a distributed version control system that helps you manage changes to your
files and collaborate with others. GitHub is a cloud-based platform that hosts
Git repositories, allowing for easy collaboration and sharing of code.
2.
Installing Git
2.1
If you are using Linux
To
install Git on Linux, open your terminal and run:
Bash
sudo
apt-get update
sudo
apt-get install git
2.2
If you are using a Mac
To
install Git on a Mac, you can use Homebrew. First, install Homebrew if you
haven't already, then run:
Bash
brew
install git
2.3
If you are using Windows
Download
the Git installer from the Git website and run the installer.
3.
Getting Started on GitHub
- Create
an account on GitHub.
- Create
a new repository by clicking the "New" button on your dashboard.
4.
Configuring Git
Set
your username and email to associate commits with your identity:
Bash
git
config --global user.name "Your Name"
git
config --global user.email "your.email@example.com"
5.
Creating a Git Repository
To
create a new repository:
Bash
mkdir
my-repo
cd
my-repo
git
init
6.
Creating and Editing Files
Create
a new file using a text editor:
Bash
echo
"Hello, World!" > hello.txt
7.
Adding Files to Your Git Repository
Add
files to your repository:
Bash
git
add hello.txt
8.
Finding Out What’s Happening
Check
the status of your repository:
Bash
git
status
9.
Making Changes and Tracking Them
After
editing a file, track changes:
Bash
git
add hello.txt
10.
Synchronizing Your Local Git Repository with GitHub
Push
your changes to GitHub:
Bash
git
commit -m "Initial commit"
git
remote add origin https://github.com/username/my-repo.git
git
push -u origin master
11.
Deleting and Renaming Files
To
delete a file:
Bash
git
rm hello.txt
git
commit -m "Deleted hello.txt"
To
rename a file:
Bash
git
mv oldname.txt newname.txt
git
commit -m "Renamed oldname.txt to newname.txt"
12.
Undoing Changes
12.1
Before You Commit
If
you want to undo changes:
Bash
git
checkout -- filename
12.2
After You Commit
To
undo the last commit:
Bash
git
reset --soft HEAD~1
12.3
Being More Selective
To
unstage a file:
Bash
git
reset filename
12.4
Retrieving an Old Version
To
view previous commits:
Bash
git
log
13.
Branching
13.1
Checking Differences Between Branches
To
see differences:
Bash
git
diff branch1..branch2
13.2
Merging Branches
To
merge a branch:
Bash
git
checkout master
git
merge feature-branch
13.3
Deleting Branches
To
delete a branch:
Bash
git
branch -d branch-name
14.
Tags and Releases
To
create a tag:
Bash
git
tag v1.0
14.1
Summarizing Your Changes
To
see changes:
Bash
git
log --oneline --decorate
15.
Downloading a Repository
Clone
a repository:
Bash
git
clone https://github.com/username/my-repo.git
16.
Managing Multiple Copies of a Repository
16.1
Dealing with Simple Conflicts
If
a conflict occurs, resolve it and commit the changes.
16.2
Dealing with More Complex Conflicts
Manually
edit the conflicting files and then:
Bash
git
add resolved-file
git
commit
17.
Summary
Git
is essential for version control and collaboration, while GitHub provides a
platform for hosting and sharing repositories. Familiarize yourself with
commands to effectively manage your code.
18.
Command Summary
18.1
Creating a Local Git Repository
Bash
git
init
18.2
Synchronizing Your Local Repository with GitHub
Bash
git
push origin master
18.3
Changing and Adding Files
Bash
git
add filename
18.4
Looking at Differences
Bash
git
diff
18.5
Check Out a Repository from GitHub
Bash
git
clone repository-url
18.6
Creating Branches for Developing and Testing New Features
Bash
git
checkout -b new-feature
18.7
Undoing Changes
Bash
git
checkout -- filename
18.8
Tags and Releases
Bash
git tag v1.0
Step-by-Step Guide to Using GitHub
Step
1: Create a GitHub Account
- Visit
GitHub:
Go to github.com.
- Sign
Up:
Click on "Sign up" and follow the prompts to create your
account.
Step
2: Create a New Repository
- Log
In:
Sign in to your GitHub account.
- New
Repository: Click the "+" icon in
the top right corner and select "New repository."
- Repository
Details:
- Repository
Name:
Choose a name for your repository.
- Description:
(Optional) Add a description.
- Public/Private:
Choose whether your repository will be public or private.
- Initialize:
Optionally check "Initialize this repository with a README."
- Create
Repository: Click the "Create
repository" button.
Step
3: Install Git
- Download
Git:
Go to git-scm.com and download
the installer for your operating system.
- Install:
Follow the installation instructions specific to your OS.
Step
4: Configure Git
Open
your terminal or command prompt and run:
Bash
git
config --global user.name "Your Name"
git
config --global user.email your.email@example.com
Step
5: Clone Your Repository
- Copy
Repository URL: On your GitHub
repository page, click the green "Code" button and copy the URL.
- Open
Terminal: Navigate to the directory where
you want to clone the repository.
- Clone:
Run:
Bash
git
clone <repository-url>
Replace <repository-url>
with the URL you copied.
Step
6: Create a New File
- Navigate
to Directory: Go to your cloned repository
folder:
Bash
cd
my-repo
- Create
a File:
Create a new file using your preferred text editor, e.g.:
Bash
echo
"Hello, World!" > hello.txt
Step
7: Add and Commit Changes
- Stage
Changes:
Use git add
to stage your new file:
Bash
git
add hello.txt
- Commit
Changes:
Commit your changes with a message:
Bash
git
commit -m "Add hello.txt"
Step
8: Push Changes to GitHub
- Push
Changes:
Send your local changes to GitHub:
Bash
git
push origin main
(Replace main
with master if your default
branch is named that.)
Step
9: Check Your Repository on GitHub
- View
Changes:
Go back to your GitHub repository page and refresh. You should see hello.txt
listed.
Step
10: Create a Branch (Optional)
- Create
Branch:
To create a new branch:
Bash
git
checkout -b feature-branch
Step
11: Make Changes on the Branch
- Edit
File:
Make changes to a file or create a new one.
- Stage
and Commit: Stage and commit your changes as
before:
Bash
git
add filename
git
commit -m "Made changes in feature-branch"
Step
12: Push the Branch to GitHub
- Push
Branch:
Send your branch to GitHub:
Bash
git
push origin feature-branch
Step
13: Create a Pull Request
- Go
to GitHub: Navigate to your repository on
GitHub.
- Pull
Request:
Click on the "Pull requests" tab, then click "New pull
request."
- Select
Branch:
Choose your feature branch and compare it to the main branch.
- Create
Pull Request: Click "Create pull
request" and add any comments.
- Merge
Pull Request: Once reviewed, click "Merge
pull request."
Step
14: Delete the Branch (Optional)
After
merging, you can delete the branch:
- Delete
Branch:
Click the "Delete branch" button on GitHub.
Step
15: Stay Updated
- Fetch
Updates:
Regularly fetch updates from the remote repository:
Bash
git
fetch
- Pull
Changes:
To pull the latest changes:
Bash
git
pull origin main
No comments:
Post a Comment