Git and GitHub: Step-by-Step Guide
Let’s explore how to interact with Git and GitHub, focusing on pushing, pulling, and cloning files between your local machine and GitHub.
1. Setting Up Git
Before you start, ensure you have Git installed. You can download it from git-scm.com.
Configure Git with your name and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
2. Creating a New Repository on GitHub
- Go to GitHub: Log in to your GitHub account and navigate to the repositories tab.
- Create a New Repository: Click the “New” button to create a new repository. Provide a name and optional description, and choose to make it public or private.
3. Cloning a Repository
To clone a repository means to create a local copy of the repository from GitHub.
# Clone using HTTPS
git clone https://github.com/username/repo-name.git
# Clone using SSH
git clone git@github.com:username/repo-name.git
4. Working with Files
Add files to your repository or make changes to existing ones.
# Create a new file
echo "Hello, GitHub" > file.txt
5. Checking the Status
Check the status of your working directory and staging area.
git status
6. Adding Changes to Staging Area
Add files to the staging area.
# Add a specific file
git add file.txt
# Add all changes
git add .
7. Committing Changes
Commit the changes in the staging area to your local repository.
git commit -m "Add greeting to file.txt"
8. Pushing Changes to GitHub
Push your local commits to GitHub.
# Push using HTTPS
git push origin main
# Push using SSH
git push origin main
Example Workflow: From Local to GitHub
Initialize a Local Repository:
mkdir MyProject
cd MyProject
git init
Create and Edit Files:
echo "Hello, GitHub" > hello.txt
git add hello.txt
git commit -m "Add hello.txt with greeting"
Push to GitHub:
git remote add origin https://github.com/username/MyProject.git
git push -u origin main
Pulling Changes from GitHub
To pull changes from a remote repository and integrate them into your local repository:
git pull origin main
Example Workflow: Collaboration
Clone the Repository:
git clone https://github.com/username/MyProject.git
cd MyProject
Make Changes and Push:
echo "New line" >> hello.txt
git add hello.txt
git commit -m "Add new line to hello.txt"
git push origin main
Fetch and Merge Changes:
git fetch origin
git merge origin/main
Detailed Explanation of Commands
git log --oneline
Shows a simplified log of commits.
Example:
git log --oneline
Output:
94ebf5f (HEAD -> ramukaka) ramukka
421b92c (tag: rel1.0, master) cherry2
8aa52e5 file10
...
61c4cfc (origin/master, origin/HEAD) adding other user
61c4cfc
: Commit hash.(origin/master, origin/HEAD)
: Indicates this commit is pointed to by bothorigin/master
andorigin/HEAD
.adding other user
: Commit message.