Image

Handy Git Commands

Resetting Forked Branch to Parent

Sometimes you have made changes to your main branch on a forked repository and you want to revert those changes to keep it in sync with the parent. There is no way to do this on the GitHub website, but it can be done with Git with a few commands.

Prerequisites

git clone <your-forked-repository-url>
cd <your-cloned-repository-directory>

Set and fetch upstream changes.

git remote add upstream <upstream-repo-url> #Upstream should be the parent repository
git fetch upstream

Checkout your main branch (or other designated branch) and reset it to the upstream.

git checkout main
git reset --hard upstream/main

Now your local codebase is in sync with the upstream codebase. What remains is to sync this up with your remote (forked) codebase. This will likely require a forced push since you are pushing up a local codebase with less history than what currently exists remotely.

git push -f

That’s it. Your remote repository should now be in sync with the upstream parent.

Resetting Local Branch to Remote

If you have made changes to a branch that shouldn’t have new commits added to it directly and you want to reset it to its remote part, you can hard reset your local branch to remote.

git reset --hard origin/<branch>

© Filip Niklas 2024. All poetry rights reserved. Permission is hereby granted to freely copy and use notes about programming and any code.