0

I'm working on Git-hub tool, there mistaken I push some file with local branch code which I don't want to merge in master branch. How to revert back "Push file" back to local system Or if there way to remove not require files from Github itself.

Any suggestion.

CoDe
  • 11,056
  • 14
  • 90
  • 197

1 Answers1

0

You may forcibly override a branch on a remote git server, including github:

git push -f <server> <good_state>:<target_branch>

Use with care.

Perhaps more comprehensive explanations are required.

Suppose you have a repo on github. In the local clone of the repo there're commits:

A -> B -> C -> D -> E

in a branch <Branch> (it could be master, or whatever), where E commit is "bad" (wrong, contain some erroneous or sensitive data etc). You have already pushed this branch to github and even created a pull request in some "parent" project.

First of all, remove the pull request, to prevent accidental merge of the E commit.

Then, drop E from your published commits on github:

 git push -f github <D>:<Branch>

Now on github the <Branch> ends with <D> commit as if there wasn't <E> at all. Now you can fix <E> locally, by issuing git commit --amend, or simply drop it with git reset --hard <D>. Whenever you're ready, you may push the new commits again and repeat PR.

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • you mean change something and create new Pull Request and delete existing one ? – CoDe Apr 11 '16 at 10:59
  • If you have already created a pull request for the bad commit, then yes, probably you should delete it. You may also remove bad commits in your personal clone of a repository on github by issuing the forementioned command. – user3159253 Apr 11 '16 at 11:01
  • But then what about my changes that has in open un-merged branch. – CoDe Apr 11 '16 at 11:27
  • Edit them locally (use `git commit --amend`, `git reset` etc), make sure that they're correct before pushing and then push the proper versions to github. – user3159253 Apr 11 '16 at 11:59
  • There is bit confusion I guess. First I just wanted to remove file from Github Server Repo not from local. So if I follow you, then first I should delete open branch but but then what about changes which were part of that open branch. How I would be able to get those changes back in my local repo as modified file. #Anysuggestion here. – CoDe Apr 12 '16 at 05:16
  • If you run `git push -f github :` where `` is a good commit, prior to commit(s) containing a bad file, then your goal will be achieved. Bad commits remain in your local repository. – user3159253 Apr 12 '16 at 05:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108879/discussion-between-shubh-and-user3159253). – CoDe Apr 12 '16 at 05:45