Search code examples
gitgit-resetgit-fork

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:

  • roll back the master branch to the state it was when I forked the repo
  • maintain the test-branch as 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).


Solution

  • 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