Git Simplified: Working Collaboratively with Git/Github
Tags: github, git, operations, tutorial
Published: 2021-10-12 17:18:12

This is the follow-up article of [Git Simplified: A Beginner guide to git and GitHub
](https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8). In that article, I have explained basic git terminologies, workflow, and some basic git commands from initializing a project on your computer/laptop to publishing to Github. If you are a beginner I will highly recommend you to do check [it](https://dev.to/shahabbukhari/git-simplified-a-beginner-guide-to-git-and-github-19o8) first.

## Working Collaboratively:
Working in a team and maintaining a code base and tracking the changes of each and everyone **manually** is hard(and a terrible choice). This is just one of many ways to collaborate on a project using GitHub. But it’s one I would suggest if you’re just starting out working with a team and haven’t established a git-flow yet or know where to start in establishing one.

### Get a copy:
Most of the time if you working on a large-scale/open-source project you don't have write access to the code. for working on your desired project first you have to fork the project into your remote repository.
![](https://imgur.com/RllznR1.png)
Now first fork a project. what it does is create a copy of the original project into your repositories.
![](https://imgur.com/bkXig50.png)

### Setup a local copy:
Now clone a repository into your local machine. 
```sh
git clone <remote repo link>
```
And if you forked the repository and you don't have write permission there is another step you have taken.
```sh
git remote add <remote name for source repo> <source repository clone link>
```

### Update a local copy:
When you are working in a team you are required to update your local copy with the changes your partners/colleagues have made.
```sh
git pull <remote name> <branch name>
```
**git pull** is a Git command used to update the local version of a repository from a remote. **git pull** is a combination command, equal to **git fetch** *+* **git merge**.
![](https://i.stack.imgur.com/nWYnQ.png)
- ***Fetch*** the changes from remote repositories and store them in your local repository.
- ***Marge*** those changes store in your local repository with your working directory

You can achieve the same result in two different steps with!
```sh
git fetch <remote name> <branch name>
git merge <remote>/<branch>
```
### Compare changes:
As we know git tracks all the changes you have made. If you want to compare your changes in **working directory** with **staging area**, **local repository** or even you can compare your changes with two different **branches** in git with **git diff** command.
Let's take an example from our previous task when we **fetched** the data from the remote repository in our **local repository**. 
For this purpose, I have made a repository [git-test](https://github.com/shahabbukhari/git-test) in my GitHub and **cloned** that repo in my local machine. 
```sh
git clone https://github.com/shahabbukhari/git-test.git
```
After that, I made some changes in **README. MD** file remotely. now i will fetch the changes 
```sh
git fetch origin master
```
![](https://imgur.com/Qlstrdq.png)
Now I have a change from **remote repo** in my **local repo** (repo aka repository). Now, let's compare those changes.
```sh
git diff origin/master
```
![](https://imgur.com/uhhnO4Y.png)
I know it looks scary but let's only focus on the first and the last two lines.

 - In 1st line ```diff --git a/README.MD b/README.md``` where ```a/README.md``` and ```b/README.md``` are the versions of the file in which changes are made ```a/README.md``` is the version of **README. MD** file present in your **working directory** and ```b/README.md```is the version of **README. MD** file present in your **Local repository**.
 - In the last two lines starting with ```+``` and ```-```. Which usually means ```+``` line is added ```-``` line is removed but as we are comparing  ```a/README.md``` (working directory version) with ```b/README.md``` (local directory version).  Now it's showing we have removed ```-# git-test -This I have made this repository to show the implementation of git diff method. ``` lines because they are present in the local repository but not in the working repository. 

**PS**: ``git diff`` is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files, and more. for complete understanding check this [Guide.](https://www.atlassian.com/git/tutorials/saving-changes/git-diff)

## Update Remote:
After adding and reviewing changes now it's time to submit your code back to your own repository(which is a copy of the original repository). 
```sh
git push <remote name> <branch name>
```

## Make a pull request
After Creating changes to your remote repository now you need to create a pull request to be able to merge your code with the source repository where the maintainer/owner will merge your code with the original repository after reviewing it.
![](https://imgur.com/iwyA7og.png)
![](https://imgur.com/wg8YAPq.png)


## Congratulation🥳:
Congratulations, if you read the complete article and stick till the end. For practice used my [**git-test**](https://github.com/shahabbukhari/git-test) repository. fork, clone, make changes, push and create a pull request
![celebrate](https://media.giphy.com/media/iFgD1JXOY4086dq6cl/giphy.gif)

## Thanks for reading.
Feel free to connect with me on [LinkedIn](https://www.linkedin.com/in/syed-shahab-shah-bukhari-33169a183/). If you wanted to check what I am working follow me on [Github](https://github.com/shahabbukhari) and Follow my [blog](https://dev.to/shahabbukhari) for more beginner's centric content(because I am not an expert).