pkaramol
pkaramol

Reputation: 19402

git: Reset to initial state of forked repository

I forked a repository (which has one master branch) in my local namespace.

Ever since, I have several commits on top of master, and I also have an additional branch (test-branch).

What I want to perform is:

A potential way of going about this is to checkout master, view the log history, find the sha of the last commit that was not mine and perform a git reset <sha_of_last_commit_of_forked_repo>.

Is there an alternative way?

fyi I have configured two remotes: origin and upstream (the upstream is the repo I forked from, with push).

Upvotes: 2

Views: 8000

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45769

You mention that the upstream has evolved. You also said that's ok, but just so you're aware of the options:

You can find the last time you integrated changes from upstream into your code - which might be when you initially made the fork (i.e. would be if you haven't merged in additional upstream changes or rebased your work).

git merge-base master upstream/master

You can then reset to the result of that command

git reset --hard `git merge-base master upstream/master`

If you have rebased your work or merged in additional changes, then it's possible you've gotten to the point where git just isn't keeping track of the "original" state of your fork any more; it's not something git thinks is important, because your fork is just another clone of the upstream as far as git is concerned.

So for future reference, if you create a fork and you think you might sometime later need to refer to "the last upstream commit when I forked", you should tag it.

Upvotes: 1

Sajib Khan
Sajib Khan

Reputation: 24184

You can do hard reset your local with upstream's master.

$ git checkout master

$ git fetch upstream                 # update local upstream history but not merged with working tree
$ git reset --hard upstream/master   # replace local 'master' history = upstream/master history

Now, you can create and checkout to a new branch test-branch from master.

$ git checkout -b test-branch

Upvotes: 3

Related Questions