Working collaboratively with git
Tags: beginners, tutorial, git, github
Published: 2023-04-07 12:14:21

Git is very useful when working with other developers. It helps keep track of changes, allows you to undo mistakes, integrate changes from other developers and work together as a team.
For example:
You and your friend are working on a project collaboratively and making changes, writing, rewriting and editing
As you both are working on different parts of a file and constantly editing, it would be preferable to track the changes instead of manually keeping all the versions of the file in your system (like project_part1, project_final, project_final2… it gets really messy and it gets easy to lose track) 
Add another person and their changes to the equation it gets worse to manage. In this post you will see a workflow on how 2 developers can use git to work collaboratively. 
For a basic introduction to git, checkout my previous [post.](https://dev.to/adikejre/git-demystified-4pda)

## Initial Setup

We can start with creating an empty repository in Github and cloning it to our machine as below.

![c3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x537jafqrmrvm21ywjr4.png)


In the base directory we create an HTML file with some content to get started with and commit and push the changes to Github.

![c5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0be62qtwou7m83m7u9g.png)

We can now create a branch to work on for our changes as shown following so that we dont push unecessary changes to the the main branch.

![c24](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9qvbafqg8kut1fmw9ao.PNG)

We use the commands `git branch <branch_name>` and `git checkout <branch_name>`as shown below.

![c8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zi7n6ku8psl7ny1r7y2l.png)

We can see the `test1` branch in github with our changes and an option for raising a pull request which we will see shortly


![c10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmnu2ecz3z4gmsh608nz.png)

<h3> Developer 2 adding changes </h3>
Now as your friend wants to contribute to the project, they can clone the project to their system, create a new branch `test2` from `main` and start working on their changes


![c11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bf48s1hiov2ps7h0doh.png)

They add the following changes to the `<style>` in the html file and commit and push the changes to the `test2` branch.


![c12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umvo6mjdoiwoi89tea1q.png)

## Creating Pull Request

The changes made by the two developers now exists in their respective branches. They want to add their changes to the `main` branch so that both of their changes are 'merged' .
To do that a pull request is created which is a merge request to add your changes to the main branch.

<h4> Pull Request from test1 branch </h4>
For the first pull request, we want to merge changes from `test1` branch to the `main` branch. We compare the changes as following a create a pull request for the content changes:


![c17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kuyy8r60wqogcvxfh2b.png)

On creating the pull request, we can see there are no merge conflicts and the branch can be merged into main.

![c18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ch0h2use951heonifi8u.png)

And now in the main branch we see the changes getting reflected!

![c19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljmzrnwjnjnh7wgivj2q.png)

<h4> Pull Request from test2 branch </h4>
Similarly we create a pull request from `test2` to `main` for the styling changes.

![c20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlkv01gd87a9begkpqgs.png)
The branch can be merged as there are no conflicts and we merge pull request.

And now we see in our `main` branch, the changes from both the branches is reflected!

![c21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd612lvwa0ku78s7tysl.png)

Now if we want to continue working on our `test1` branch and pull the latest changes from main, we can do it painlessly with the `git pull` command as shown below.


![c22](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfjpisv5klmc54awc1m2.png)


![c23](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbqsdkos0g9i76exgug6.png)
<br>
This is how we can work collaboratively with git. It scales really well when working on projects in large teams and is a really powerful tool for ensuring mistakes are minimized and all changes are appropriately reviewed before merging. Thus, following this simple workflow can make it very easy to work collaboratively and definitely will make you want to consider using this in your next project!