1

I am following an online course which includes some exercises using notebooks hosted on a github repository.

I forked their repository in github and then cloned it to work locally on the exercises. I did not take the precaution of creating a separate branch before making my changes - which in retrospect may not have been a such a great idea.

Now I have found a typo on one of the files and would like to send the authors a pull request so they can review and fix it. I only want to include in the pull request the correction for this typo, but not everything I modified in the files to solve the exercises.

My intention was to revert to the original version of the repo that I downloaded (before I made any changes), from that point create a new branch, fix the typo there and send a pull request from that branch but I am struggling. For context: I did a course on git and github a couple of months ago but have very little practical experience.

I tried to do that by issuing git log --oneline --graph -all to ID the relevant COMMIT_ID then git checkout COMMIT_ID to revert there. Unfortunately this didn't work as I expected. git informed me I was in 'detached HEAD' state and proposed to create a new branch with the committed changes using git switch -c NEWBRANCH. I tried, but from this new branch I can still see all the modified files (just they are not added to the commit yet, but the changes are still there), and in addition the remote repo is not the original one but the one I forked to my github. I could manually change the remote, manually undo the changes in the files... but I have the strong feeling I am doing something wrong. There must be an easy way to do this.

Could anyone help me understand what I am doing wrong, and find a better way to do it?

mhered
  • 184
  • 1
  • 1
  • 8

2 Answers2

0

After some research I combined advice from these two sources: How do I “git pull” and overwrite my local changes? and Sync your Git Fork to the Original Repo into the following solution:

  1. Check configuration of remotes and configure the original repo as upstream (it was not done):
$ git remote -v
$ git remote add upstream https://github.com/[Original Owner Username]/[Original Repository].git
  1. create a new branch 'issue_fix' and overwrite its contents with the upstream repository
$ git checkout -b issue_fix
$ git fetch upstream
$ git reset --hard upstream/main
  1. fix the issue in the 'issue_fix' branch and commit
$ git commit -am "Commit message. Fixes issue #XX"

push to a corresponding newly created branch in github

$ git push --set-upstream origin issue_fix

Then open the pull request from github.

This seemed to work fine, but I would still be interested in listening to advice.

mhered
  • 184
  • 1
  • 1
  • 8
  • Two more great resources that describe what would have been a better workflow from the beginning: [Your first open source contribution: a step-by-step technical guide](https://medium.com/@jenweber/your-first-open-source-contribution-a-step-by-step-technical-guide-d3aca55cc5a6) as well as [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) – mhered Jul 28 '21 at 20:30
0

After adding and fetching upstream, you can easily create a new fix branch with the more recent (Git 2.23+) git switch command, less confusing than git checkout:

git switch -c issue_fix upstream/main
# work and commit
git push -u origin issue_fix
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250