Syncing Your GitHub Fork
HTML-код
- Опубликовано: 14 окт 2024
- This is video #9 in the Data School series, "Introduction to Git and GitHub." Relevant links, a command list, and the full transcript are below. Playlist: • Version control with G...
== LET'S CONNECT! ==
Blog: www.dataschool.io
Newsletter: www.dataschool....
Twitter: / justmarkham
GitHub: github.com/jus...
== LINKS RELATED TO THIS VIDEO ==
Diagrams of forking and syncing: www.dataschool....
Syncing a fork: help.github.co...
Git quick reference for beginners: www.dataschool....
== COMMAND LIST ==
show your remotes: git remote -v
add an upstream remote: git remote add upstream [URL]
fetch changes from the upstream: git fetch upstream
merge those changes into your repo: git merge upstream/master
push those changes to the origin: git push origin master
== TRANSCRIPT ==
In this video, we're going to talk about syncing your fork, what that means, and why you might want to do it.
To start, let's go through what we already know about forks. Here's a diagram from my blog, and I'll link to this in the video description. In this diagram, the top level is the GitHub level, and the bottom level is your local computer.
Remember that if you want to copy a GitHub repo, you just fork it, and then you can clone it to your local computer. After making changes and committing those changes, you can push the changes back up to GitHub. We won't cover pull requests in this video, but that's simply how you ask the owner of the original repo to pull your changes into their repo.
Anyway, let's pretend that you forked a repo yesterday, and then today, the repo owner made some changes that you want to incorporate into your fork. Remember from a previous video that forks do not automatically stay in sync, and thus you have to do this manually.
Let's look at another diagram. If you want to sync your fork with the original repo, which we call the "upstream", this actually happens through your local computer. You simply "fetch" the changes from the upstream, you "merge" them into your repo, and then you "push" them up to GitHub. Note that the "fetch" and "merge" steps are often combined into a single step called a "pull", but it's generally recommended that you fetch and merge in two separate steps.
Let's actually do this for one of my repos. Here's the commit history of a repo that I forked, and here's the commit history of my fork. You can see that the original repo has one additional commit since I forked it. I've opened Git Bash, and we're already in the working directory for that repo.
First, let's check our remotes with "git remote -v". We've got an "origin" remote, but we also need an "upstream" remote that points to the original repo. We'll copy the URL from GitHub. You'll probably be using the HTTPS URL, but I set up SSH so I'm using the SSH URL. Then we "git remote add upstream" and paste the URL using the "Insert" key on Windows. Let's check that it worked.
Now we're ready to fetch. You type "git fetch upstream", and then type your password. That command fetched the changes, and stored them in a branch called "upstream/master". You can actually switch to that branch and look at the changes if you like, but we're going to go ahead and merge those changes. You type "git merge upstream/master", which tells git to merge that branch into your working branch, which also happens to be called "master".
If you like, you can also push those changes up to GitHub, so that your GitHub repo is in sync. Just like before, we type "git push origin master", then your password, and you're done! You can see that the change is now in my repo.
Once again you prove to be probably the most valuable data science resource on RUclips. Thanks for taking the time to make these videos. I owe you a PhD.
Thank you very much! 🙏
Thank you so much for this, I have been looking for weeks for how to do this. So many of the others were so confusing, this was clear and clean. Thanks again!
Awesome! Glad I could be of help! :)
Really appreciate your videos on git & github. Very clear.
Wish you would do one on branches with git/github though
Thanks for your kind words! I'll take your suggestion under consideration.
Alberto, I don't know of any way to sync your fork to the upstream without having to use a local repo in-between.
Regarding changes you make to your fork: they will get preserved when you merge in changes from the upstream, unless those changes conflict with one another. In that case, git will ask you to manually fix any merge conflicts, and you will have to decide which changes to keep.
Somethings are made so perfectly, that they never change.
🙏
Great set of videos! Easy to follow, it helped me to get started with git and github and to finished my course assignment! Thanks!
Thank you very much, this serial of videos is highly underrated!
Extremely helpful in illustrating the difference between pull and fetch!!
Denise Barnes Awesome, glad it was helpful! :)
Finally found what I was looking for. THANKS, A LOOOOOT, trying to find this for so long
Great to hear!
thank you best explanation i have encountered.
Thank you! 🙏
Your videos are very clear and helpful, thanks!
+Tim Theeboom Thanks for your kind comments!
Exactly what I was looking for. Thanks.
Great to hear!
Thank you for this VERY CLEAR tutorial!
MrMattz84 You're very welcome!
Now This is Exact Explanation with diagram awesome
Great! Glad it was helpful to you, Vijay!
Great video! Easy to understand with that simple example!
Thanks!
Great videos man, thanks a lot
+H1B1 You're welcome!
How to merge only selected files from upstream?
Here's the scenario: I forked a repo and working on my own customization while original project is independantly developing other features.
Now I wish to add few newly added files from master (original repo) which were not present at the time of my fork. I do not with to merge their other files, which I have already modified as per my requirement.
Excellent, thanks a lot!
Thank you!
great explanation, thank you!
You're welcome!
Wow! Can't be explained any better.
Glad it was helpful to you! :)
Thanks! You got me started with github. :)
Great to hear! :)
Hi @Data School, How could I see the changes fetched before merging them? Can I see like a diff ? Thank you very much. Awesome video.
Thanks! You can definitely do that, though I can't remember how at the moment :)
I'm sure it's covered in Pro Git, which is an excellent Git reference: git-scm.com/book/en/v2
Very simple and instructive, Thanks ;)
+Dyego Rodrigo You're welcome :)
Is there a way to sync from the upstream repo to my GitHub repo without having to use my local repo in-between. This is in case I happen not to be in my computer, like in a library computer from my university...?... Also , the changes that you made on you fork are also merged, correct ? I mean, you dont lose your "new"information or changes done to your fork in the process, correct ?. =) Thanks in advance
It is really helpful. Thanks a lot
Glad it was helpful!
Thanks buddy :)
No problem 👍
This youtube video is super useful, thank you soooo much!!!!
You're very welcome!
Thanks! This video was very helpful.
ExcaliburZeroTech You're very welcome!
Great video, I have a question. If we assume that before fetching from upstream, some of the files in our local repo was changed. In that case, is fetch/merge going to cause a conflict? For instance, I cloned a forked repo, then made some changes in my local repo, and now I want to sync the fork. But since my local repo has some files that are changed from the one I'm fetching from, shouldn't there be conflicts? What should be done in that scenario?
That's right, you might have a merge conflict in that case. Here are some tips for how to resolve it: stackoverflow.com/questions/161813/how-to-resolve-merge-conflicts-in-git
Helped me a lot. Thanks.
You're welcome!
after i type: git merge upstream/master . i get: Already up to date . even tho there are some new changes in the project. ???
You should check that you are really in master and not in some other branch. If you are working in other branch switch to master with: git checkout master
once we do the git fecth upstream, i dont see the branch listed in git branch,, but it is listed in git branch -r.. What is the difference between both??
easy to understand... kudos
Thank you!
U just solved my problem..... Thanks alot....!
Great to hear!
Excellent video you saved me a lot of time Thank you!!!
Anton Szilasi You're very welcome!
Old but perfect
Thank you for the video. I have tried this several times now, but my fork on github still states "This branch is 2 commits ahead" Is there anyway to make sure it is even with the original repo?
Hard to say, sorry!
@@dataschool No worries. Figured it out. I had to "Rebase" to get the "n commits ahead" message to go away. In case anyone runs into this as well the steps to resolve are 1) git remote add upstream 2) git fetch upstream 3) git rebase upstream/master 4) git push origin master --force
Thanks!
Welcome!
Hello Kevin, #1 $git merge upstream/master
#2 git push origin master
After #1 to merge the changes to local master, why is there no need to commit the changes local similar to adding/editing files. Why is #2 be successful to upload changed to github.
viranirav Great question. Because there was no divergent work to merge together, the merge operation does not involve a commit. If that was not the case, a commit would have been required.
after the fetch and merge, does the previous PR and branches will be there ??
+Shekhar Reddy Mittapelly I'm sorry, I don't understand what you mean by "previous PR (pull requests) and branches". Could you explain?
+Data School eg: I have 3 branches made out of the previous master and there are Pull requests which are not yet got merged. If I do fetch and merge as you explained in the video, do the branches and PR exists??
+Data School By the way, could you please make a video explaining PULL command :-)
+Shekhar Reddy Mittapelly Regarding branches, you specify the branch you want to fetch from the upstream, and it merges into your working branch locally. So if you have three branches, you will have to work with them individually.
Regarding pull requests, those are stored on GitHub only and are not affected by fetching and merging.
This video helped me so much!!!!! Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You're very welcome! :)
What is the advantage of doing a fetch and then merge rather then a single pull command?
No great advantage.
Well done, well explained ;)
Thanks!
Excellent!!!! Thanks
You're welcome!
Thank you!
You're welcome!
When it asks for passcode in gitbash what does it mean is it GitHub account passcode or admin passcode or the ssh passcode?
I think it's the SSH passphrase, but I actually can't remember!
Thanks but it will only sync the master branch of my fork. How can I sync ALL its branches?
PHAT HUYNH You have to sync each one individually.
this doesn't work anymore! I get the error:
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm '
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
Please make a new tutorial
If you follow the exact steps in the series, I promise that it will still work! Here's the series: ruclips.net/p/PL5-da3qGB5IBLMp7LtN8Nc3Efd4hJq0kD
Good luck!
Help!
When i type "git fetch upstream",i don't get the password option. My terminal simply goes blank.
I'm sorry, it's hard to troubleshoot git issues from afar. Good luck!
fatal: refusing to merge unrelated histories
feched fork is added as remote.
tried to checkout --track but master branch exists
Sorry, it's hard to troubleshoot git errors remotely, good luck!
thanks
You're very welcome!
You can use my shell script to synchronize, welcome everyone to correct. github.com/pursonchen/sync-fork.git
Thanks for sharing!
Thanks !
You're welcome!