DEV Community

Cover image for A Fool-Proof Way to Keep Your Fork Caught Up in Git

A Fool-Proof Way to Keep Your Fork Caught Up in Git

Jacob Herrington (he/him) on September 20, 2019

How do you keep a fork up to date with the main repository on GitHub? It seems like quite a few people have asked me this over the last few months...
Collapse
 
kaushalgautam profile image
Kaushal Gautam

Hi Jacob! Brilliant article, I was dealing with the same thing today.
I have another problem tho. The repo I was contributing to did not have a lot of activity and I had to make many changes and many PRs (rule was one change per commit, one commit per PR). Now I made change 'ChA' and commited 'CmA'. Then I pushed and created a PR. But the problem I guess was that the PR was not accepted very fast (as would generally happen). I move on to next change, 'ChB', commit 'CmB' and push it. But now when I try to open a PR, I am shown the previous PR (which has not yet been accepted) with both commits 'CmA' and 'CmB'. How do I keep them separate?

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

That sounds like you need to make each of those commits on different branches.

If you didn't make a new branch when you created 'CmB', then you might have accidentally added that commit to the same branch as 'CmA' which would put it in the same Pull Request.

Collapse
 
kaushalgautam profile image
Kaushal Gautam

Thank you for the reply, Jacob. Yes, these were all on the master branch. Do you suggest I make separate branches for each of those changes? How does that work for many changes?

Thread Thread
 
jacobherrington profile image
Jacob Herrington (he/him)

What I do, is create a branch for each new feature. I usually have quite a few commits in each PR, but they are all for the same feature on what most people refer to as a 'feature branch' (meaning a branch specifically for that feature).

So if I'm going to fix some documentation, I'd make a branch from master called documentation-fixes.

git checkout master
git checkout -b documentation-fixes
git commit -am "Some doc fixes!"

Then when I got to implement a feature, I'd make a new branch from master:

git checkout master
git checkout -b some-new-feature
git commit -am "Some feature!"
Thread Thread
 
kaushalgautam profile image
Kaushal Gautam

I understand now. Thanks for such detailed explanations! They really help a lot. And congratulations on the new job! :)

Collapse
 
glsolaria profile image
G.L Solaria • Edited

If I have local changes, I prefer to stash, change the tracking branch, pull, change the tracking branch back, re-apply stash, deal with any conflicts, push!!! It makes me feel comfy for some reason! I am scared of rebase.

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

I used to be scared of rebase, but I got used to it and now it's one of my favorite git commands. It can be scary though.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

A rebase a day keeps the doctor away.

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

I didn't realize you could use remotes that way... but you are right - that makes keeping forks updated a LOT easier!