Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a specific commit but keep all the commits afterward

I do manage git in an organized way, as per my knowledge. I commit every specific changes, so that I can get back to a single bit of feature whenever I want. In this process, I've added a feature (commit #74fd458) about 10 days ago, where I added a binary file and made changes in 3 tracked files.

After that I made at least 15 commits.

dfs4334 - new commit 15
2yu44m6 - new commit 14
...
47jc063 - new commit 1
74fd458 - Target Commit (seems unwanted now)
560dfsf - old commit 1
lfg956d - old commit 2
...

Now I came to a situation that, that specific commit (read 'feature') is unnecessary. So, thing is very simple, open a commit visualizer and remove those specific lines made in that specific commit, remove that binary file that's added then, and you are done. But, you know it's messy.

Q: Is there any way I can keep all the changes I made afterward, but remove, or revert, or pick a specific commit out of the branch?


PS. As scenario can change anytime, that specific feature can be added later. So I don't want to remove that feature completely from version control, I just put those things out, but to keep them somewhere so that I can merge them anytime, if necessary.

like image 860
Mayeenul Islam Avatar asked Dec 18 '16 07:12

Mayeenul Islam


People also ask

How do you remove a commit without losing changes?

Removing the last commit If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard": git reset HEAD^ which will evict the commits from the branch and from the index, but leave the working tree around.

Can I delete a git commit but keep the changes?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I exclude a commit from a pull request?

Do a fetch in your local repository to remove the reference to the removed branch. Create a new branch for the new commits. Reset the original branch to the last commit that was approved in the Pull Request. Push the original branch back to Bitbucket.


1 Answers

Is there any way I can keep all the changes I made afterward, but remove, or revert, or pick a specific commit out of the branch?

git revert is probably what you are looking for.
From the documentation:

Revert some existing commits

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them

In your case, both the original commit and the (let me say) revert commit will be part of the project's history.
You can revert the revert commit later if you need it, that is if you need to apply the original commit once more.

See the linked documentation for further details.

like image 138
skypjack Avatar answered Sep 21 '22 10:09

skypjack