A Brief Guide to Using GitHub
Download PDF
1. Creating a GitHub Account
- Go to the GitHub website: GitHub.
- Click on the Sign up button.
- Follow the instructions to create your account.
- Verify your email address after signing up.
2. Configuring Git with Your Email and Username
Before using Git, it's important to configure it with your details:
- Open a terminal window.
- Set your Git username:
git config --global user.name "Your Name"
Replace Your Name
with your actual name.
- Set your Git email:
git config --global user.email "your.email@example.com"
Replace your.email@example.com
with your actual email address.
- Verify your configuration:
git config --global --list
3. Creating a Personal Access Token
- Go to your GitHub account settings: GitHub Tokens.
- Click on Generate new token (classic).
- Give your token a descriptive name.
- Select the necessary permissions (e.g.,
repo
).
- Click Generate token and copy it securely.
4. Cloning a Repository
- Go to the GitHub repository you want to clone.
- Click on the Code button and copy the repository URL.
- Open a terminal and navigate to your desired directory:
cd path/to/your/directory
- Clone the repository using:
git clone https://github.com/username/repository.git
5. Forking a Repository
- Navigate to the repository on GitHub.
- Click the Fork button at the top right.
- A copy of the repository will be created under your account.
6. Creating a New Repository
6.1 Initializing a Repository Locally
- Navigate to your project directory:
cd /path/to/projectfolder
- Initialize Git:
git init
- Add all files to the repository:
git add .
- Commit the files:
git commit -m "Initial commit"
6.2 Uploading to GitHub
- Create a new repository on GitHub.
- Copy the repository URL.
- Link your local repository:
git remote add origin https://github.com/username/repo.git
- Push your local changes:
git push -u origin main
7. Adding and Committing Changes
- Stage changes:
git add .
- Commit changes:
git commit -m "Your commit message"
8. Pushing Changes to GitHub
- Push committed changes:
git push origin main
9. Branching and Merging
- Create a new branch:
git checkout -b new-branch-name
- Merge a branch:
git merge branch-name
10. Viewing Commit History
git log --oneline
11. Forking a Repository
11.1 Forking a Repository on GitHub
- Go to the repository you want to fork on GitHub.
- Click the Fork button at the top right of the repository page.
- This will create a copy of the repository in your GitHub account.
11.2 Forking a Repository from the Terminal
- Install the
hub
command-line tool:
brew install hub (macOS)
sudo apt install hub (Linux)
- Fork the repository using:
hub fork https://github.com/ORIGINAL_OWNER/REPO_NAME.git
- Clone your fork locally:
git clone https://github.com/YOUR_USERNAME/REPO_NAME.git
12. Adding the Upstream Repository
- Open a terminal in your local repository.
- Add the upstream repository:
git remote add upstream https://github.com/ORIGINAL_OWNER/REPO_NAME.git
- Verify the upstream repository:
git remote -v
13. Fetching and Merging Upstream Changes
- Fetch the latest changes from the upstream repository:
git fetch upstream
- Switch to your main branch:
git checkout main
- Merge changes from the upstream repository:
git merge upstream/main
- Push the merged changes to your GitHub fork:
git push origin main
14. Handling Merge Conflicts
- Open the conflicting files. Git will mark conflicts with:
<<<<<<< HEAD
Your changes
=======
Changes from upstream
>>>>>>> upstream/main
- Manually resolve the conflicts by editing the files.
- Stage the resolved files:
git add filename.ext
- Commit the merge:
git commit
- Push the changes to your fork:
git push origin main
15. References
Author: Georgina Woo