Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git revert commit/push but keep changes

Tags:

It goes like this:

  • I have files A and B that I modified

  • I'm only suppose to commit and push A, but accidentally committed both and pushed both A and B

  • I did a "git push old-id:master" so on github it shows "Master is now old-id" where old-id is the last commit before me, so i think it's back to before i committed.

Question:

  • On my local, how do i undo the commit that has A and B, and commit only A, and push only A?

Note: I do need to keep the changes for both A and B on my local though. The end result should be:

  • Local - new A and new B
  • Github - new A and old B
like image 746
James Gu Avatar asked Apr 17 '12 03:04

James Gu


People also ask

How do you revert a pushed commit without losing changes?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .

Does git revert keep local changes?

Git reset doesn't discard all local changes The git reset –hard command will revert uncommitted changes that exist in files that have been added to the index, whether those files are newly created files, or files that were added to the index in the past and have been edited since the last commit.

What happens when a commit is reverted?

A revert operation will take the specified commit, inverse the changes from that commit, and create a new "revert commit". The ref pointers are then updated to point at the new revert commit making it the tip of the branch.


1 Answers

$ git reset <old-id>    # Undo the commit after old-id $ git add A             # stage A for a new commit $ git commit            # make the new commit $ git push              # push it 
like image 136
Amber Avatar answered Oct 07 '22 07:10

Amber