Home > Mobile >  Discard branch in forked repo and make a copy of same branch from upstream into fork
Discard branch in forked repo and make a copy of same branch from upstream into fork

Time:12-04

I have a forked repo with branch develop. When I make changes I usually pull from the upstream develop in to my forked branch and continue. Now I made a series of commits in my forked repo that I don't want to merge. So

  1. How can reset my branch to be what the upstream branch is - like a sync or
  2. How can I discard my branch and make a new copy of the upstream develop branch in my forked repo?

thank you!

CodePudding user response:

(Since branches have virtually no cost in git you might want to create a backup branch before doing the reset (e.g. git branch develop.old.backup develop). You can always just delete it later if you want to)

The answer to 1) is a hard reset: git checkout develop; git reset --hard origin/develop (assuming the upstream repository is named origin). NB, the hard reset is a command that discards data and commits, which is what you ask for in this particular case, but it should be handled with respect1.

Alternative 2) would be

git checkout main  # or any other branch/commitish
git branch -D develop
git checkout -b develop origin/develop

1 I remember reading a humorous piece titled something "How to remove files as root in just 15 step" or something like that with exaggerated double/tripple checking of file masks and conditions before running an rm command as root. I was unable to find it now when searching for it.

  • Related