Skip to main content

GitHub Branching and Collaboration

Purpose of Branches

Branches in GitHub allow for parallel development on different features or bugs without impacting the main or master branch. This approach enables multiple developers to contribute to a project simultaneously.

  • Files in GitHub are stored on branches
  • The master branch contains the deployable version of the code
  • New branches are created for changes or new features

Creating a New Branch

To create a new branch:

  1. Access the branch: master drop-down menu
  2. Enter the name for the new branch in the new branch field
  3. Click Create branch

Best Practices

  • Use descriptive names for branches

Making Changes in a Branch

Changes to files within a branch are made by:

  1. Selecting the file
  2. Clicking the pencil icon
  3. Editing the file
  4. Committing the changes

Committing Changes

  • Ensure code is stable before committing
  • Commit messages should be meaningful and follow best practices (e.g., no period at the end, under 50 characters, use active voice)

Pull Requests

Pull requests propose changes from one branch to another (typically to master) and require approval.

To create a pull request:

  1. Click Pull request > New pull request
  2. Choose the new branch to compare
  3. Review the changes
  4. Add a title and description
  5. Click Create pull request

Important Information

  • Pull requests can be made with unfinished code
  • Approval can be by the author or assigned team members

Merging Changes

To integrate changes from a pull request:

  1. Click Merge pull request
  2. Click Confirm merge
  3. Delete the feature branch post-merge as it becomes obsolete

Objectives and Introduction

GitHub, hosting over 100 million repositories, offers tools for collaboration such as forking and cloning repositories, essential for both team and independent project development.

Cloning a Repository

Cloning creates a local copy of a repository.

Steps to Clone

  1. Navigate to the repository on GitHub
  2. Click Code > clipboard icon under "Clone with HTTPS"
  3. In a terminal, navigate to the target directory and run git clone <Pasted_URL>

Keeping Sync

  • Use git add, git commit -m <message>, and git push to sync changes with the remote repository

Forking a Repository

Forking creates a separate copy of a repository for independent changes.

Steps to Fork

  1. Go to the repository to fork
  2. Click Fork at the top-right

Using a Fork

  • Forks allow for independent project development or contributions back to the original project

Legal and Best Practices

  • It's often required to keep a copy of the license file when forking

Collaborating Using Git Commands

Remote Repositories are managed with commands like git push (send changes), git fetch (pull changes without merging), and git pull (fetch and merge).

Terminology

  • Origin: Your fork of a repository
  • Upstream: The original repository

Syncing a Fork

  1. Use git remote -v to view remote repositories
  2. Add the original repository as upstream with git remote add upstream <Original_Repo_URL>
  3. Update with git fetch upstream and merge using git merge upstream/master or git pull upstream

Roles in Git Projects

Developer

Utilizes commands like git-clone, git-pull, git-push, git-format-patch, and git-send-email for collaboration.

Integrator

Manages incoming changes, using commands like git-am, git-pull, git-format-patch, git-revert, and git-push.

Repository Administrator

Sets up and maintains repository access, using tools like git-daemon, git-shell, git-http-backend, gitweb, and GitHub Actions for automation.

Summary

  • The roles of Developer, Integrator, and Repository Administrator are key to managing a project, each with specific Git commands and tools for effective communication and collaboration.