Git Forking & Fetch: How to Keep your Fork in Sync with an Upstream Repository

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you've forked a repo and now how do you keep it up to date with the main repository that you forked from I was asked this question after I made my last get github crash course for free code camp so I thought I'd make this quick video on just an easy way to keep your repository in sync with the repo that you forked from hi my name is Gwen if you don't know me I run this channel Faraday Academy and I make lots of different types of programming content and also do coding live streams here and over on my twitch channel which is also a Faraday Academy so if you learn something from this video then be sure to like comment and subscribe so you can get notifications about future videos so this is my Fork of the main view j/s repository I basically forked this I think a couple months ago to use it for a demo but you can see that since I forked it made a copy of it for myself the UJS itself has received several updates that I don't have in my fork yet basically the view of j/s project represented by purple here has continuously committed code changes and my fork represented by green hasn't been able to receive any of these updates so how do I keep my fork in sync with the view j/s repository in this video I'm gonna go over what I think is the best method for doing this I'm gonna clone this repository my fork of the repository locally and then figure out how to cut it in sync with the upstream now if you were really working on developing and making changes to your fork you probably already have it cloned locally but I don't so I'm just gonna do that real quick okay so now it's cloned I'm going to move into that directory and now I can see I'm in a git directory here so I'm gonna do git remote - V and I can see where I cloned from it gives me the - RLS where I would basically pull code changes from and where I would push code changes to that I make locally now by default these are both the same thing and you probably want to keep these both the same thing but we also need to add the URL to the main view j/s repository here that we forked from so we can get the changes for there so I'm gonna do git remote add and you can see the name over here origin now origin is just an alias or a name for this particular remote so it's kind of like a shorthand for typing out this whole URL since I cloned the repo from this URL it automatically saves a remote called origin for me that points to that URL now I also want to do that for the repo that I forked from and a popular convention for aliasing the source repo is to call it upstream I've also seen some people call it URL and other things I think upstream makes a lot of sense because since we forked our code from there our code is downstream from the original repo so I'm gonna a Leo set here as get remote add and then give it the alias upstream and then the last command-line argument I need is the actual URL that I want to save into this upstream alias and then I can go to view j/s and copy the get location paste it here and so I can do git remote add upstream so now I'm doing git remote add upstream and the URL I'm gonna hit enter now it doesn't give me any feedback there but if I do get remote V again then it added URL under the alias upstream so I have my fork under the name origin and then I have the original under the name upstream so I want to update origin with changes that happened in upstream now I can pull down all of the remote changes from upstream by using git fetch upstream and now VJ s is quite an active repository so there's a ton of new branches that it just fetched from the upstream repository to my local machine but you can see that the branch names here are upstream / upstream / upstream / feature SSR etc so they're all upstream / so they aren't actually merged with my local branches yet to merge in changes with my local branch I would have to do git merge and then I am on the dev branch right now in my local fork so if I wanted to merge the dev branch from upstream into this dev branch I could say upstream / dev and merge that into my dev branch and now it's trying to merge the code but before it merges because it's going to make a commit to merge the changes it's asking if we want to add a commit message here and just like a standard commit if there's a reason why you're doing this or you need to explain the what or the why you can add that here in most cases if you are emerging you might just want to exit this screen and use this default commit message that they give you which is merge remote tracking branch upstream dev into death that's usually descriptive enough so I'm going to exit this screen just use the default message I can exit by doing : Q enter and now all of the changes from the upstream dev branch have been merged into my local dev branch and you can do that merging with whatever branch you wanted to so let me check out another branch get check out master and now you can see I get this warning because now I have upstream master and origin master locally so my git is confused at which one I mean when I say check out the master branch so there are a few ways to tell at which branch I meant one of the easiest things to do would just be just type in origin slash master or upstream slash master and then I could hit enter and check out that branch I can also tell get to either prefer origin or upstream by using like it says right here the - oops - - track option and now for at least the dev and the master branches if I check out either of these get now knows that I always want to prefer the origin when I'm checking out branches locally but if I check out a different branch then get is gonna ask me the same question again do I want upstream origin so to resolve this confusion you can set a preference for the whole repository and your git config and say edit and it takes you into your git config here and after core I'm gonna add a line and here I'm basically set my checkout preference to default to origin so now I saved this with : W and then I can quit with : Q and now I can check out any branch without denoting whether I want origin or upstream and it's definitely nice to set that option inside your git config if you're working in a repo where you are trying to keep the origin in sync with the upstream repository so you're fetching both locally of course that configuration I set is only for this git repository if I wanted to do the same thing globally I could do git config the same edit flag and then just - - global and this pulls up my global get configuration and I could set my preferred remote in here the same way but I don't want to do that so I'm gonna quit here let me get back to the dev branch so get check out dev and now it's still saying that my local branch is ahead of origin dev it says by seven commits here sorry it's at 6 before but I accidentally made another commit off-camera so don't worry about the number of commits here and I can update my forked repository under the alias origin by doing git push and now you can see --get hopes telling me that my repository is no longer behind the main view jeaious repository it's actually head because I made some merging commits here so that's basically how you keep a repository up-to-date after you fork it you can just fetch changes and then merge changes and then push changes to your fork and then of course you get changes that you make in your fork back into the base repository by making a pull request and then your changes get merged back in if your pull request gets accepted and merged so that's basically it for this tutorial I hope you liked it thanks so much for watching I hope you enjoyed it don't forget to leave me a comment like and subscribe and I will see you in the next video you
Info
Channel: Faraday Academy
Views: 22,348
Rating: undefined out of 5
Keywords: how to update a forked git repo, update git fork, git remote repositories, git forking, git fetch
Id: deEYHVpE1c8
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Mon Jul 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.