Pracede
Pracede

Reputation: 4361

How to go back to a previous commit and make change in this previous commit?

I am working with git. I had a branch from a master since some days.

$ git log
commit 412455451515dd5d5dldldld454585fgdgjd      // #7
Author: me
Date:   Fri Mar 03 14:27:55 2015 +0100

    feature7A

commit 95299628812ae425c06accc61a0ef4203cc  // #6
Author: me
Date:   Wed Mar 03 11:20:35 2015 +0100

     feature6A

commit 82f1691fcfd6348ddf5462be42bf9983b2f  // #5
Merge: 052c908 6c2c93d
Author: me
Date:   Tue Mar 02 16:36:01 2015 +0100

   feature5A

commit 6c2c93da125a66f7dbb5b14a96feed819f70671d     // #4
Author: me
Date:   Tue Mar 01 14:30:48 2015 +0100

   feature4A

I went back to the commit 82f1691fcfd6348ddf5462be42bf9983b2f // #5

git checkout 82f1691fcfd6348ddf5462be42bf9983b2f 

I correct a bug in this commit. I want to recover this changes in commit 412455451515dd5d5dldldld454585fgdgjd // #7 . Want is the best practice :

  1. Stash the change Switch to commit #7 Apply the change with git stash pop

  2. Merge the commit #5 and #7.

Thanks

Upvotes: 0

Views: 50

Answers (2)

Gauthier
Gauthier

Reputation: 41945

Cleanest, best, most honest, is to apply the changes in the present. Commit #5 had a bug, so be it. Every developer write bugs, it is not a problem to let the history show it. There is no point in rewriting history to hide it.

Best practice is to:

$ git stash
$ git checkout #7
$ git stash apply

Check that your fix still works

$ git commit -a

Mention possibly in your commit message that this fixes a bug introduced in commit #5.

Upvotes: 1

Félix Cantournet
Félix Cantournet

Reputation: 1991

You are about to Change recorded history. Do this only if you haven't shared your modifications. Beware!

If you have shared this, then don't panick, but also don't do the following. refer to whoever is responsible for the repo and get proper help, that is consistent with the workflow in your project.

// in the following i'm assuming that you were working on master.
git checkout 82f1691f -b recovery
// This checks out the commit you want to modify and creates a branch there that we will delete afterwards.
//Hack hack hack.

git add my_Modified_File.h  //You can replace this 2 lines by using git gui
git commit --amend          //and selecting the menu commit->fix the last commit

git checkout master
git rebase recovery

Upvotes: 1

Related Questions