0

I had forked from a main repo from our github and based upon requirement made changes in an existing file in my branch. Later I have committed and pushed my changes to my private branch. When I send a pull request to merge changes in main branch I was told to move my code to a new file based upon the functionality I have written.

I am not sure how to revert back the pushed file in my repo as to what it is in original / main repo?

Please let me know how to achieve the same.

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Possible duplicate of [How do you undo the last commit?](http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit) – Atri Dec 24 '15 at 05:20
  • 1
    Please review the answers below and let us know if any of them match your requirements (which aren't too clear to me). – Tim Biegeleisen Dec 24 '15 at 05:40
  • The easiest way for me was to save my changes locally / remotely. Delete my private repo in github - delete my local cloned repo (box where I have cloned my code) - cloned again and work on this new env. – Programmer Dec 24 '15 at 05:56
  • This sounds like overkill, and quite painful actually. For next time, if you ask a more focused question you will get a better answer. – Tim Biegeleisen Dec 24 '15 at 06:18
  • Agreed but cause my changes where in one major file - doing this saved me time than trying many ways .. anyway please let me know whats the best option would have been cause I believe I would have ran into problem if my code base was really huge and had many committed changes and wanted to revert back a)one file b)complete code base to what it is like in master branch – Programmer Dec 24 '15 at 06:29

3 Answers3

0

Doing git reset --soft HEAD~ will revert the last commit on your branch.

Then you can make the required changes and commit. Merge your changes to master.

Atri
  • 5,511
  • 5
  • 30
  • 40
  • One caution here: If he also had significant code changes in the last commit, then this might wipe them out. – Tim Biegeleisen Dec 24 '15 at 05:29
  • 1
    @TimBiegeleisen *I am not sure how to revert back the pushed file in my repo as to what it is in original / main repo*. I think that is what is intended. – Atri Dec 24 '15 at 05:32
  • I tried that but still in my private repo branch I am seeing my changes? – Programmer Dec 24 '15 at 05:46
  • @Prakash whatever the last commit you had on your current HEAD, it would be reverted. Maybe your HEAD is not on the commit that you want to revert. Check `git log` – Atri Dec 24 '15 at 05:52
0

I don't think you necessarily have to revert anything you have done, assuming you want to keep the code you already pushed. You can simply move the file to a new location and possibly change the name, then push this change to your remote branch. GitHub will automatically detect that your remote branch has been updated and the pull request can then be completed.

Once you have moved the file and renamed it, you can have two options. The first is to simply commit the work and push:

git commit -m 'Renamed file and moved to a new location'
git push origin yourBranch

This will create a new commit on top of your remote branch. The other option would be to amend the HEAD commit, and force push that to the repository:

git commit --amend -m 'Renamed file and moved to a new location'
git push --force origin yourBranch

This will simply update the commit on the HEAD of your remote branch. Regardless of how you want to resolve this, your feature branch will appear in the master branch as a single merge commit, and your pull request can be completed.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Remove your local undesired commits

// sha1 to your last desired commit that you want to keep
git reset HEAD <sha1>

// create new branch 
git checkout <new_branch>

// delete old branch
git branch -D <old branch>

// Overwrite remote branch with the removed code
git checkout <old branch>
git push origin <new_branhc>
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167