DEV Community

Mingming Ma
Mingming Ma

Posted on

Collaborative Git Workflow: Learning git fetch and git pull

In this blog post, I'm going to share my developing process about git fetch and git pull in the process. We will also discuss how git fetch allows us to safely test and review code from others.

When reviewing and testing a PR, you might need to work with a different repo that you didn't clone originally. How can you do this? The answer is git fetch.

Assuming Bob is working on the forked repo, you can add their fork as a remote to your local repo:

git remote add Bob https://git-url-of-Bob-fork.git
Enter fullscreen mode Exit fullscreen mode

Here I assigned the remote name as 'Bob' for his fork's URL, you can choose any name you prefer.

With this remote added, you can fetch his work into your local repo:

git fetch Bob
Enter fullscreen mode Exit fullscreen mode

git fetch downloads all their commits and branches to your local repo but doesn't automatically merge anything. This is crucial because it won't interfere with your current branch or code.

Next set up a tracking branch in your local repo, so you can track the work on Bob's branch:

git checkout -b <branch-name> Bob/<branch-name>
Enter fullscreen mode Exit fullscreen mode

This creates a branch in your repo that points to the same commit as the Bob's repo and branch. You can now use this branch to review and test Bob's work.

If Bob make updates to their code, you can easily fetch these changes into your tracking branch:

git checkout <branch-name>
git pull Bob <branch-name>
Enter fullscreen mode Exit fullscreen mode

At last once you're satisfied that the feature, we can merging it manually by

git fetch Bob
git checkout main
git merge Bob/<branch-name>
git push origin main
Enter fullscreen mode Exit fullscreen mode

Note that merging the work and pushing it to the main branch will automatically close the pull request.

To sum it up, Git's adaptability and strength prove incredibly beneficial when engaging in collaborative projects. The use of git fetch allows you to examine and evaluate others' code without interfering with your own work. If you haven't used it before, give it a try!

Top comments (0)