简体   繁体   中英

Change an old commit in GIT

There was a big change committed (which included changes in many files and addition and removals of files) by copying code from somewhere and on top of that much work has been done including many merges from other branches. Now we have got to know that wrong version of code was copied earlier.

Is there any way that I can change that commit by copying the right version of code now?

I had a look at this , but there it is a linear history. Also I had a look at this , but example is only for adding new file.

In my case I'm sure there will be conflicts after I change the old commit. I'm also OK to add a new commit instead of changing existing one.

I agree with websterridge, modifying history is probably not the way to go; that only makes sense if you want to permanently erase all traces of the old commit (and has its set of problems).

The best solution is probably to create a new commit with the necessary changes. The idea is to create a diff between the version that was committed, and the version that should have been, and apply that diff to the current version.

You could do it like this (untested):

  • Create (and check out) a branch on top of the old commit where the wrong code was committed: git checkout -b fix-branch SHA_OF_OLD_COMMIT
  • Copy the right version of the code that should have been committed into the working directory. You can just overwrite the wrong files without worrying about conflicts, because you have gone back to the old commit with the original code import.
  • Commit this: git add --all . ; git commit -m "Fix import of wrong code." git add --all . ; git commit -m "Fix import of wrong code."

Now you have the right code in git, but on branch "fix-branch".

You can now use git's tools to get the right code into your current branch (let's say master): either rebase fix-branch on top of master, or merge it into master. In this case a merge is probably better, because that way the repository history contains the original version of the new, correct code import before conflict resolution.

Either way, you'll probably get a lot of conflicts, but there's no way around this.

Good luck!

As justified as it sounds, changing history is a bad idea and complicated. Plus folks already have the current history in their stream.

The better practice is to just bite the bullet and correct with a new commit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM