-1

I just made a push on the production site and all the changes were on master. But i realized the code that i pushed needs to be altered so i need to revert the last commit but take all that code from that commit and put it in another branch so i can push it later....any way to do this

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

3 Answers3

3

There are many ways to fix this.

Here's what I'd do (with a bit of extra insurance)

Assuming your HEAD is in 'the wrong place' (no pun intended), you can

 git branch rescue
 # you are still on master
 git reset HEAD^ # soft resets the last commit
 git push -f     # to undo the push - beware of other users of the push-branch

Now edit / rescue you commit in-place (because the last commit's changes are now pending again). If there is an emergency, you can always checkout the rescue branch, which is simply a copy of the HEAD when you started this post.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • do i do this on my local ....basically the local is where i am doing the push and the production is where i do "git pull origin master" – Matt Elhotiby Nov 14 '11 at 16:13
  • You do it where you wanted to fix the problems (so, presumably the local). Did you already `pull` the production side? If not you're good, but otherwise, production will have to be `--force` pulled as well – sehe Nov 14 '11 at 16:14
  • also ...i realized it was about 3 commit ....so is there a way to alter what you showed above to revert the last three commit instead of the last commit – Matt Elhotiby Nov 14 '11 at 16:19
  • Yup. `git reset HEAD~3`. After fixing the central repo (using the push -f), just do another pull on production. You specify `--force` because it will see that it isn't a fast-forward change – sehe Nov 14 '11 at 16:22
  • so after i do this git reset HEAD~3 on the local and then i run git push -f on local also then on the production i do git pull origin master --force or do i do a push from the local first – Matt Elhotiby Nov 14 '11 at 16:25
  • you push what you want to pushed: in your case, _after_ reset the last three commits. Keep in mind that the changes of those last commits will be pending in your working copy. If you did the `rescue` branch _first_ you'll always have that backup, so you can't screw it up too badly. – sehe Nov 14 '11 at 16:33
  • also how do i now take all these commits and put them into a new branch – Matt Elhotiby Nov 14 '11 at 16:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4984/discussion-between-sehe-and-tamer) – sehe Nov 14 '11 at 16:33
2

I recommend to fix already pushed commit with the correcting revert commit. You may also safely start your experiments from the reverted commit. See also Git Community Book, Undoing in Git

git revert HEAD
git push

Rewriting already published commits is considered as a bad practice (quote from the same section):

Fixing committed mistakes

If you make a commit that you later wish you hadn't, there are two fundamentally different ways to fix the problem:

You can create a new commit that undoes whatever was done by the old commit. This is the correct thing if your mistake has already been made public.

You can go back and modify the old commit. You should never do this if you have already made the history public; git does not normally expect the "history" of a project to change, and cannot correctly perform repeated merges from a branch that has had its history changed.

See also section "The Perils of Rebasing" from Pro Git, Rebasing why this is not good.

Konstantin Tenzin
  • 12,398
  • 3
  • 22
  • 20
0

2 steps:

1) do a branch off the current master.( Saving your code so you can do the fix. )

2) Roll back 1 Commit in master. git reset --hard HEAD~1

Community
  • 1
  • 1
RayLoveless
  • 19,880
  • 21
  • 76
  • 94