Introduction
to GitHub and Working with GitHub
1.
Introduction to GitHub
What is GitHub?
GitHub is an online platform that allows developers to host
and manage their Git repositories. It's like a social network for developers
where they can share their code, collaborate on projects, and contribute to
others' work. GitHub provides tools for version control, project management,
and collaboration, making it an essential platform for any software development
project.
GitHub vs. Git
- Git: Git is a distributed version control system.
It's the tool that you use on your local machine to track changes to your
code. Git allows you to create repositories, commit changes, create
branches, and merge code, all on your local computer.
- GitHub: GitHub is a platform that hosts Git
repositories online. It allows you to store your Git repositories in the
cloud, making it easy to share your code with others and collaborate on
projects. GitHub also adds extra features like pull requests, issue tracking,
and project management tools, which are not part of Git itself.
Creating a GitHub Account
Before you can start using GitHub, you'll need to create an
account. Here's how:
- Go to github.com.
- Click on the Sign up button.
- Enter your email address and click Continue.
- Create a password and click Continue.
- Choose a username that will be visible to others on
GitHub.
- Verify your account by solving the puzzle or CAPTCHA.
- Select the plan you want (you can choose the free plan).
- Complete the setup by following the prompts.
Once you have your GitHub account set up, you're ready to
start creating and managing repositories!
2.
Working with GitHub
Creating a Repository on GitHub
A repository is where your project's files and version
history are stored. Here's how to create a new repository on GitHub:
- Log in to your GitHub account.
- On the GitHub homepage, click on the + icon in the
upper-right corner and select New repository.
- Enter a name for your repository.
- Optionally, add a description to explain what the project
is about.
- Choose whether the repository will be public
(anyone can see it) or private (only you and collaborators can see
it).
- (Optional) Add a README file, a .gitignore file, or a
license to your repository.
- Click Create repository.
You now have a new GitHub repository ready for your project!
Cloning a Repository
Cloning a repository means making a local copy of a remote
GitHub repository on your computer. Here's how to do it:
- Go to the GitHub repository you want to clone.
- Click the Code button.
- Copy the repository's URL by clicking the clipboard icon.
- Open your terminal or Git Bash on your computer.
- Navigate to the directory where you want to store the
cloned repository.
'cd path/to/your/directory
- Use the git clone command followed by the URL you copied:
'git clone https://github.com/username/repository-name.git
- Git will download the repository to your local machine.
Connecting Git to GitHub
If you have an existing local Git repository and want to
link it to a GitHub repository, follow these steps:
- Go to GitHub and create a new repository (as explained
above).
- Copy the repository's URL.
- Open your terminal and navigate to your local Git
repository.
- Link the local repository to GitHub using the following
command:
git remote add origin https://github.com/username/repository-name.git
- Verify that the remote repository is added:
git remote -v
Pushing Changes
Once your local repository is connected to GitHub, you can
push your commits to the remote repository:
- Make sure your changes are committed:
git commit -m "Your commit message"
- Push the changes to GitHub:
git push origin main
Replace main with the name of your branch if it's different.
Pull Requests
Pull requests (PRs) are a way to propose changes to a
project. When you submit a pull request, the project maintainers can review
your changes, discuss them, and merge them into the main codebase.
- Fork the repository you want to contribute to (see
"Collaborating on GitHub").
- Clone the forked repository to your local machine.
- Make changes and commit them.
- Push the changes to your forked repository on GitHub.
- Go to the original repository on GitHub.
- Click Pull Requests and then New pull request.
- Select the branches you want to compare (your fork and the
original repository).
- Review your changes and submit the pull request.
Collaborating on GitHub
When working on open-source projects or collaborating with
others, you may want to contribute to a project that you don't have write
access to. Here's how:
- Fork a Repository: Forking is creating your own
copy of someone else's repository.
- Go to the repository you want to contribute to.
- Click the Fork button at the top-right of the
page.
- You now have your own copy of the repository in your
GitHub account.
- Clone Your Fork: Clone your fork to your local
machine (as explained earlier).
- Make Changes: Work on the project locally, commit
your changes, and push them to your forked repository.
- Submit a Pull Request: Submit a pull request to the
original repository (see "Pull Requests").
3.
Hands-on Practice
Creating a Personal Repository
Now that you know how to work with GitHub, try creating your
own repository:
- Create a new repository on GitHub (as explained earlier).
- Clone the repository to your local machine.
- Add some code or files to your local repository.
- Use git add, git commit, and git push to push your changes
to GitHub.
Collaborative Project
Form small groups and set up a collaborative project using
GitHub:
- One person creates a new repository on GitHub and adds the
others as collaborators.
- Each group member clones the repository to their local
machine.
- Work on different features or parts of the project in
separate branches.
- Use pull requests to merge changes into the main branch.
- Discuss and resolve any merge conflicts that arise.
4. Common
Git/GitHub Workflows
Feature Branch Workflow
In this workflow, each feature or bug fix is developed in a
separate branch. Here's how it works:
- Create a New Branch:
git checkout -b feature-branch-name
- Work on the Feature: Make changes and commit them
to the feature branch.
- Push the Branch:
git push origin feature-branch-name
- Submit a Pull Request: Once the feature is
complete, submit a pull request to merge it into the main branch.
Fork and Pull Workflow
This workflow is commonly used in open-source projects:
- Fork the repository.
- Clone your fork to your local machine.
- Create a new branch for your changes.
- Push your changes to your fork.
- Submit a pull request to the original repository.
GitHub Issues and Projects
GitHub provides tools to track tasks, bugs, and project
progress:
- Issues: Use GitHub Issues to report bugs, request
features, or discuss tasks. You can assign issues to team members and
label them for easy tracking.
- Projects: GitHub Projects allow you to organize
issues, pull requests, and notes into a Kanban-style board. This helps you
visualize the progress of your project.
5. Best
Practices
Commit Messages
Good commit messages are essential for maintaining a clear
project history. Here are some tips:
- Be Descriptive: Clearly explain what the commit
does.
- Use the Imperative Mood: Start with a verb, e.g.,
'Fix bug,' 'Add feature.'
- Keep it Concise: Aim for a short, clear summary in
the first line, and include more details in subsequent lines if necessary.
Regular Commits
Commit your changes frequently. This helps you keep track of
your progress and makes it easier to identify and revert specific changes if
needed.
Branching Strategy
Use branches effectively to manage different features or
versions of your project:
- Main Branch: The stable version of your project.
- Feature Branches: Work on new features in separate
branches.
- Hotfix Branches: Use branches to fix bugs without
disrupting ongoing development.
By following these best practices and using GitHub
effectively, you'll be well-prepared to manage your final projects and
collaborate successfully with your classmates.