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.
Initial Setup
We can start with creating an empty repository in Github and cloning it to our machine as below.
In the base directory we create an HTML file with some content to get started with and commit and push the changes to Github.
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.
We use the commands git branch <branch_name>
and git checkout <branch_name>
as shown below.
We can see the test1
branch in github with our changes and an option for raising a pull request which we will see shortly
Developer 2 adding changes
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
They add the following changes to the <style>
in the html file and commit and push the changes to the test2
branch.
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.
Pull Request from test1 branch
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:
On creating the pull request, we can see there are no merge conflicts and the branch can be merged into main.
And now in the main branch we see the changes getting reflected!
Pull Request from test2 branch
Similarly we create a pull request from test2
to main
for the styling changes.
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!
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.
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!
Top comments (0)