A Brief Guide to Using GitHub

Download PDF

1. Creating a GitHub Account

  1. Go to the GitHub website: GitHub.
  2. Click on the Sign up button.
  3. Follow the instructions to create your account.
  4. 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:

  1. Open a terminal window.
  2. Set your Git username:
git config --global user.name "Your Name"

Replace Your Name with your actual name.

  1. Set your Git email:
git config --global user.email "your.email@example.com"

Replace your.email@example.com with your actual email address.

  1. Verify your configuration:
git config --global --list

3. Creating a Personal Access Token

  1. Go to your GitHub account settings: GitHub Tokens.
  2. Click on Generate new token (classic).
  3. Give your token a descriptive name.
  4. Select the necessary permissions (e.g., repo).
  5. Click Generate token and copy it securely.

4. Cloning a Repository

  1. Go to the GitHub repository you want to clone.
  2. Click on the Code button and copy the repository URL.
  3. Open a terminal and navigate to your desired directory:
cd path/to/your/directory
  1. Clone the repository using:
git clone https://github.com/username/repository.git

5. Forking a Repository

  1. Navigate to the repository on GitHub.
  2. Click the Fork button at the top right.
  3. A copy of the repository will be created under your account.

6. Creating a New Repository

6.1 Initializing a Repository Locally

  1. Navigate to your project directory:
cd /path/to/projectfolder
  1. Initialize Git:
git init
  1. Add all files to the repository:
git add .
  1. Commit the files:
git commit -m "Initial commit"

6.2 Uploading to GitHub

  1. Create a new repository on GitHub.
  2. Copy the repository URL.
  3. Link your local repository:
git remote add origin https://github.com/username/repo.git
  1. Push your local changes:
git push -u origin main

7. Adding and Committing Changes

  1. Stage changes:
git add .
  1. Commit changes:
git commit -m "Your commit message"

8. Pushing Changes to GitHub

  1. Push committed changes:
git push origin main

9. Branching and Merging

  1. Create a new branch:
git checkout -b new-branch-name
  1. 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

  1. Go to the repository you want to fork on GitHub.
  2. Click the Fork button at the top right of the repository page.
  3. This will create a copy of the repository in your GitHub account.

11.2 Forking a Repository from the Terminal

  1. Install the hub command-line tool:
brew install hub   (macOS)
sudo apt install hub   (Linux)
  1. Fork the repository using:
hub fork https://github.com/ORIGINAL_OWNER/REPO_NAME.git
  1. Clone your fork locally:
git clone https://github.com/YOUR_USERNAME/REPO_NAME.git

12. Adding the Upstream Repository

  1. Open a terminal in your local repository.
  2. Add the upstream repository:
git remote add upstream https://github.com/ORIGINAL_OWNER/REPO_NAME.git
  1. Verify the upstream repository:
git remote -v

13. Fetching and Merging Upstream Changes

  1. Fetch the latest changes from the upstream repository:
git fetch upstream
  1. Switch to your main branch:
git checkout main
  1. Merge changes from the upstream repository:
git merge upstream/main
  1. Push the merged changes to your GitHub fork:
git push origin main

14. Handling Merge Conflicts

  1. Open the conflicting files. Git will mark conflicts with:
<<<<<<< HEAD
Your changes
=======
Changes from upstream
>>>>>>> upstream/main
    
  1. Manually resolve the conflicts by editing the files.
  2. Stage the resolved files:
git add filename.ext
  1. Commit the merge:
git commit
  1. Push the changes to your fork:
git push origin main

15. References

Author: Georgina Woo