DEV Community

Cover image for Using Git and GitHub for collaboration
Muslimat Mojeed
Muslimat Mojeed

Posted on

Using Git and GitHub for collaboration

Overview of Git collaboration

Watching tutorials on Git and applying the knowledge to personal projects can be interesting, but collaborating using Git could present another challenge that one may face when the need arises.

As we continue to explore the field of software development, teamwork is essential to creating reliable and creative solutions. As projects get more complicated and involve more people, efficient teamwork becomes essential to maintainability and smooth development.

This article aims to extensively explain the guidelines of using Git for collaboration. It will delve into setting up a hosting platform like GitHub for collaboration, various branching strategies, collaborative Git workflows, different methods of cloning, as well as troubleshooting and resolving conflict techniques in Git collaboration.

It's important to note that knowledge of version control system(Git) and GitHub is required before reading this article, as this article mainly focuses on guidelines of using Git for collaboration.

Steps for setting up GitHub for collaboration

  1. Creating a collaborative Git repository: Creating a collaborative Git repository (remote repository) is the foundational step towards effective teamwork. Whether your team is small or large, setting up the repository correctly ensures a smooth collaborative workflow. Quick guide on how to create a git repository:
  • Choose a hosting platform: Choose a hosting platform for your Git repository first. Popular options include Bitbucket, GitHub, GitLab, and so on. Because every platform has different features, take your team's demands and preferences into account. In the case of this article, we will be using GitHub.

  • Create a new repository

  • Initialize a README file: Add a README file to your repository for project documentation. This provides summary of goals and set-up instructions.

  • Invite collaborators: After creating the repository, add collaborators that will be contributing to the project to a repository.
    To do this, navigate to settings, select collaborators, click on 'Add people'.

invite collaborators

  • Configure repository settings: Repository changes can be made in the settings. Hosting platforms like GitHub enables you to choose who can see, clone, and contribute to the repository as well as set up access controls. It also enables branch protection rule to prevent unintentional pushes towards vital branches

branch protection rule

2.Establish Branching strategies:
Branching strategies in Git collaboration refer to the practices and conventions used to manage code changes and collaboration among team members within a Git repository. These strategies define how branches are created, named, and merged to facilitate efficient development, testing, and deployment workflows. Here are some of the ways to establish branching:

  • Main based Branching: All developers work are done directly on the main branch. Developers make changes directly on the main branch. The main branching strategy simplifies the process of making changes, but it has disadvantages such as lack of isolation, limited review and testing of codes, difficulty in reverting some changes and increases the risk of unintended impacts on the codebase.
# Ensure you're on the main branch
git checkout main

# Make changes to the code directly on the main branch
# Add, commit, and push changes
git add .
git commit -m "Fix bug"
git push origin main
Enter fullscreen mode Exit fullscreen mode
  • Feature branching: Each new feature is developed in a separate branch. Developers create feature branches off the main branch, work on their changes, and merge them back when ready. This provides isolation for development, allowing multiple features to progress simultaneously without interfering with each other.
# Create a new branch for a feature
git checkout -b feature/new-feature

# Make changes to the code
# Add, commit, and push changes to the feature branch
git add .
git commit -m "Implement new feature"
git push --set-upstream origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Collaborative Git workflow

The collaborative Git workflow is a methodology for multiple developers to work together on a project using git as the version control system.

When it comes to Git collaboration, developers work together via a shared repository that is frequently housed on websites like Bitbucket, GitHub, and GitLab. A central hub where team members can push and pull changes is this repository. For various activities, like adding a feature or repairing a bug, developers make branches, which they later merge back into the main source.

Consider a scenario of three talented developers—Alice, Bob and Dave—were tasked with building a new web application for their client. Alice, with her strong leadership skills, volunteered to take on the role of team lead for the project. The following are the ways Alice and her teammates worked on building the project:

  1. Alice sets up the repository, initialize a README file and invited her teammates as collaborators.
  2. She designated the main branch as the primary development branch, setting the stage for collaborative work.
  3. Bob and Dave each cloned the repository to their local machines and created feature branches for their respective tasks. Bob and clone can clone in three ways. It's either they clone using HTTPS,SSH Keys or Command Line Interface
  git clone git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Bob and Dave decided to clone with SSH for efficiency and security reasons. The use of SSH, HTTPS or CLI for cloning will be discussed subsequently.

Image description
You need to note that as a collaborator, you don't fork a repository. Forking creates a copy of the repository in a new repository on your GitHub page. However, in the case of collaboration, you want to collaborate directly on the same GitHub repository with your teammates.

4.Over the following days, the team members worked diligently on their assigned tasks within their feature branches.

# Make changes to the code
Enter fullscreen mode Exit fullscreen mode

5.They made frequent commits, pushing their changes to the remote repository to keep their codebase up to date.

# Add, commit, and push changes to the feature branch
git add .

git commit -m "commit changes"

git push --set-upstream origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

6.Once they completed their tasks, they pushed their changes. Bob and Dave created pull requests from their feature branches to the main branch on GitHub. On the GitHub page, they noticed a button labeled "Compare and pull request" after clicking on the pull request option. Bob and Dave clicked on this option to push the changes from their local repositories to the remote repository.

..

7.This took them to the “Open a pull request" page. From here, a brief description of what each of them changed was written. Each of them clicked on the “Reviewers” tab and Alice being the team lead was selected as the “Merge Master that is she will be in charge of merging the branches”. After that, they clicked on "“Create pull request”* from their ends.

...

8.When Alice logged into her GitHub account, she received notifications informing her that she had been assigned as a reviewer. A yellow bar appeared at the top of the pull request button, indicating that one of her teammates had "requested her review on this pull request." Alice, acting as the team lead, clicked on the "Add your review" button reviewed each pull request carefully, providing feedback and suggestions for improvement.

Image description

9.After thorough review and approval from Alice, She scrolled to the bottom of the pull requests and she clicked on the "Merge pull request". The branches was finally merged into the main branch.

Image description

10.Alice sees a “Pull request successfully merged and closed” message. She clicked on the button “Delete branch" to delete the feature branches.

Image description

Note: Deleting a branch is optional.

To learn a bit more about using Git for collaboration, check out this tutorial video below:
Git for collaboration

Ways of cloning

Repositories can be cloned using HTTPS, SSH or the Command Line Interface(CLI).

Image description

1.Cloning with HTTPS: When you clone a repository using HTTPS, you're getting its contents through a secure connection. You use your username, password, or a personal access token to prove you have the right to access the repository. This method is widely supported and easy to use, so most people find it convenient.

git clone https://github.com/username/repository.git
# Replace `username/repository` with the GitHub username and repository name
Enter fullscreen mode Exit fullscreen mode

2.Cloning a repository using SSH: It involves using the Secure Shell (SSH) protocol for authentication and communication. When you clone a repository using SSH, you authenticate with the GitHub using an SSH key pair: a public key stored on the platform and a private key stored on your local system. SSH cloning is secure and efficient, especially for users who prefer no-password authentication and have configured SSH keys for Git operations. Let's walk through the process step by step:

Generating SSH Keys:

  • Open Terminal (Command Prompt on Windows): This is where you'll run the commands to generate your SSH keys. Run the ssh-keygencommand in the terminal
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Replace `"your_email@example.com"` with your actual email address.
# This command generates a new SSH key pair using the RSA algorithm with a 4096-bit key size.
Enter fullscreen mode Exit fullscreen mode

Choose a Location and Passphrase (Optional): After running the command, you'll be prompted to choose a location to save the SSH keys. Press Enter to accept the default location (~/.ssh/id_rsa) or specify a different location if desired.

Passphrase: A passphrase is a sequence of words, characters, or symbols used to encrypt and protect sensitive information, such as private keys or encrypted files. It acts as an additional layer of security beyond just having the private key or encrypted data itself. When setting up certain security mechanisms, like SSH keys, users have the option to add a passphrase to enhance security. You can also choose to set a passphrase for added security. This is optional but recommended.

View and Copy the Public Key: Once the keys are generated, you'll see a message indicating where the keys were saved. To view your public key, use the following command:

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

This will display your public key in the terminal. You'll need to copy this key to your clipboard and add the key to your GitHub as well.

Adding SSH Key to GitHub:

  • Log in to git hub.
  • Navigate to the repository on GitHub and click on the "code" button labelled.
  • Add SSH Key: Click on the "add a new public key" button.
  • Give your SSH key a descriptive title and paste your public key into the "Key" field.
  • Click the "Add SSH key" button to save your key.

Using SSH Keys for Git:
Clone Repositories:
Clone the repository by copying the SSH URL after adding the SSH key. In the terminal, navigate to the directory where you want to clone the repository and run the following command:

git clone git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Once the repository is cloned, you can perform Git operations as usual, such as making commits, pushing changes, and pulling updates.
Note that each team member generate their own SSH key pair in which this allows them to authenticate individually and securely access the Git repositories hosted on GitHub. While each team member has their own separate SSH key pair for authentication, they can still collaborate and work together on the same repositories.

3.CLI cloning: Cloning a repository through the command-line interface (CLI) involves using a terminal or command prompt to run Git commands. Regardless of whether you choose HTTPS or SSH cloning, the process is initiated and managed using Git commands in the CLI. This allows you to clone repositories, manage branches, make commits, and perform other Git operations directly from the terminal, providing flexibility and control over your development workflow.

Troubleshooting and resolving conflict

Troubleshooting and resolving conflicts is an essential aspect of collaborative development. It is essential to adhere to certain procedures while troubleshooting and resolving issues in Git collaboration in order to guarantee a seamless resolution process. Here's a guide on how to effectively address conflicts that may arise during the development process:

  1. Identify the merging conflict: Merge conflicts arise when Git is unable to automatically reconcile code discrepancies between two commits, usually as a result of numerous developers making simultaneous changes to the same lines in a file. Reviewing code changes, discussing discrepancies, and understanding the root cause of the conflict is the initial step in resolving merge conflict.

2.Resolving merge conflict: Opening the disputed file, making the required modifications, staging the new content with git add, and generating a new commit with git commit are the procedures involved in resolving merge conflicts.

3.Collaborative Conflict Resolution: Working in separate isolated branches to prevent conflicts and the use of the Git merge command to merge feature branches together and resolve conflicting changes.

4.Collaborate on solutions:
Collaborate to come up with a few possible solutions to the conflict. Promote original thought and investigate other strategies that successfully deal with the fundamental problems.

The Best Ways to Prevent Conflicts
To reduce conflicts, team members must communicate about the portions of the files they are working on, pull changes before committing or pushing, and use the appropriate Git commands and tools.

Conclusion

This article has explored the guidelines of using Git for collaboration, challenges of using Git in a collaborative context and offers advice on common issues and industry best practice for effective teamwork in software development.

I hope a lot was gained from this article. Be free to drop your suggestions or questions in the comment box.

Further Reading

If the article was insightful, you can research and explore these topics:

  • Git best practices for team collaboration.

  • Extensive study on Branching strategies etc.

Top comments (0)