Home > Blockchain >  Ways to undo the commits of 2 months ago in Git after push the commits?
Ways to undo the commits of 2 months ago in Git after push the commits?

Time:12-17

I saw the solutions on undo the last / last x commits in Git.
What I want to know is how to undo the commits that are pushed 2 months ago. I want to undo just the commits in the middle of the commits, but want to keep the other commits.

for example:

commit 1  ----  2 mins ago
commit 2  ----  1 day ago
....
commit 105  ---  2 months ago
commit 106  ---  2 months ago
commit 107  ---  3 months ago
....

In the example, I want to reverse commits 105 and 106, but keep the other commits as they are.

CodePudding user response:

git revert will create a new commit that undoes the changes that made in the target commit. So in your example, you would run git revert 105 106 and this will make 2 new commits that remove the changes that were made in those commits.

If you only wanted to undo the changes from only one file in the those commits, you can do git reset HEAD~2 (This assumes that you have 2 reverted commits). Then you can git checkout the files that you don't want to have the changes undone. Once that is done, you can git add and git commit to undo the changes to only the specific files.

CodePudding user response:

There are many ways to do this: My solution is this:

  1. Create a new branch till commit id you want to be included. All the commits you don't want will not appear (1, 2, 105, 106 in your example). Please refer to https://www.techiedelight.com/create-branch-from-previous-commit-git/
  2. Then use cherry-pick to copy commit ids 1 and 2 to the new branch. Please refer to: How to copy commits from one branch to another?
  • Related