Mornor Mornor - 1 year ago 58
Git Question

Git change code from a specific pushed commit on specific branch

I still have some difficulties to understand completely the process to modified an already pushed commit.

I am not working on the master branch (don't know if that matter), and I committed and pushed wrong data (say, clear password...) in the Commit_2 (see below).

I would like to change files from this commit only, and don't change the others.

Of course, I have read the documentation git-rebase, cherry-pick etc.. as well as some very nice answers from SO, like here or here, but I am still not sure how to do it.

So, basically, how can I change files from, and only from Commit_2 ?

commit 54sdf4f4d5f4dsf4555q5aaaa44fr
Author: me <>
Date: Wed Sep 14 10:25:09 2016 +0200


commit ghgzf4x1522qx1x5f222y4d
Author: me <>
Date: Tue Sep 13 15:01:42 2016 +0200


commit dfsdc4ycx45ds4fyc6d4fysfsd
Author: me <>
Date: Tue Sep 13 14:57:08 2016 +0200


Answer Source

One approach might be to revert Commit_2, which would add a new commit which would completely undo the changes to every file modified in Commit_2. This would be perfect if you wanted to remove all changes, but you want to retain some of the changes.

Run the following command to see which files were changed in Commit_2:

git show --pretty="" --name-only dfsdc4yc

Now identify which files you want to keep, and then for each file type the following:

git checkout 54sdf4f4 file/to/restore

where 54sdf4f4 is the SHA-1 hash of Commit_3. This will effectively redo everything which was done to files you want to keep.

Now you can commit this change. You could amend the revert commit since you haven't yet pushed your local work, via:

git commit --amend -m 'Undo of certain files from Commit_2'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download