DEV Community

Cover image for Git Simplified: Working Collaboratively with Git/Github
shahabbukhari
shahabbukhari

Posted on • Updated on

Git Simplified: Working Collaboratively with Git/Github

This is the follow-up article of Git Simplified: A Beginner guide to git and GitHub
. 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 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.

Now first fork a project. what it does is create a copy of the original project into your repositories.

Setup a local copy:

Now clone a repository into your local machine.

git clone <remote repo link>
Enter fullscreen mode Exit fullscreen mode

And if you forked the repository and you don't have write permission there is another step you have taken.

git remote add <remote name for source repo> <source repository clone link>
Enter fullscreen mode Exit fullscreen mode

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.

git pull <remote name> <branch name>
Enter fullscreen mode Exit fullscreen mode

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.

  • 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!

git fetch <remote name> <branch name>
git merge <remote>/<branch>
Enter fullscreen mode Exit fullscreen mode

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 in my GitHub and cloned that repo in my local machine.

git clone https://github.com/shahabbukhari/git-test.git
Enter fullscreen mode Exit fullscreen mode

After that, I made some changes in README. MD file remotely. now i will fetch the changes

git fetch origin master
Enter fullscreen mode Exit fullscreen mode


Now I have a change from remote repo in my local repo (repo aka repository). Now, let's compare those changes.

git diff origin/master
Enter fullscreen mode Exit fullscreen mode


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.

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).

git push <remote name> <branch name>
Enter fullscreen mode Exit fullscreen mode

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.

Congratulation🥳:

Congratulations, if you read the complete article and stick till the end. For practice used my git-test repository. fork, clone, make changes, push and create a pull request
celebrate

Thanks for reading.

Feel free to connect with me on LinkedIn. If you wanted to check what I am working follow me on Github and Follow my blog for more beginner's centric content(because I am not an expert).

Discussion (0)