How to Use Git and GitHub for Beginners
Git is a version control system that lets you track changes in your code and collaborate with others. GitHub is a platform for hosting Git repositories online, allowing multiple developers to work on projects simultaneously. This guide will help beginners get started with Git and GitHub.
What You’ll Need
To follow along, make sure you have:
- A computer running Windows, macOS, or Linux
- Internet connection
- A GitHub account (Sign up here)
Step 1: Installing Git
Install Git based on your operating system:
For Windows:
- Go to git-scm.com
- Download the Windows installer and run it
- Use default settings and complete the installation
For macOS:
- Open Terminal and run:
xcode-select --install
- This installs Git along with developer tools
For Linux:
- Open Terminal and run:
sudo apt update
sudo apt install git
Step 2: Configure Git
After installing Git, set up your name and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Step 3: Creating a GitHub Repository
- Log in to your GitHub account
- Click the + icon in the top-right and choose New repository
- Enter a repository name and click Create repository
Step 4: Cloning a Repository
To copy the repository to your computer:
git clone https://github.com/your-username/your-repo-name.git
Step 5: Making Changes
Navigate into your project folder and edit or create files. Then run:
git add .
git commit -m "Your commit message"
git push origin main
This stages, commits, and uploads your changes to GitHub.
Step 6: Pulling Changes
To get the latest updates from GitHub:
git pull origin main
Step 7: Viewing Git History
To see your commit history:
git log
Press q to quit the log view.
Basic Git Commands
git status– Check current changesgit diff– View file differencesgit branch– List branchesgit checkout -b new-branch– Create a new branchgit merge branch-name– Merge a branch into current branch
Using .gitignore
Create a file named .gitignore in your repository to exclude certain files or folders from being tracked by Git.
# Example .gitignore node_modules/ *.log .env
Syncing with GitHub
Use the following to keep your local and remote repos in sync:
git fetch
git merge
git push
git pull


No comments:
Post a Comment