Change a previously committed change to git

I realized that I made a mistake in a previously committed change. There are several other commits that depend on this (wrong).

Can I change it and propagate these changes to a working copy?

+4
source share
4 answers

(With the usual reservations about changing public history ...)

Given the need to modify the commit, one of the easiest ways to do this is:

1) Check the commit you want to change using git checkout <SHA> (find the correct SHA using gitk or git log ).

2) Create a new branch with this commit using git checkout -b <new branch name> .

3) Change the code as you wish.

4) Use git commit --amend to change commit.

5) Redirect β€œto” to move the remaining commits from the original branch. To do this, recall the SHA about changing the commit before changing it from above, and:

  • Go to the source branch with git checkout <original branch name> .
  • Run git rebase --onto <new branch name> <before SHA of the modified commit> .

This will replay the commits after that SHA in the original branch on top of the new branch. You may need to resolve merge conflicts (as usual) during rebase.

6) Delete the new branch using git branch -d <new branch name> .

As usual in this case, look at what happens in gitk to make sure you think, and (for security) mark the source branch before doing rebase.

+3
source

This can be done, but I would prefer not to. Changing the past has side effects. The branch or commit tree will not be redirected forward, and you will have to redefine the branch on the server, if any. In other words, if you are not the last one to see him, and this is only on your computer. This can create more problems, and you risk losing things if something goes wrong.

If you are ready to enter the world of time travel and alternative reality!

Welcome to git rebase

First, create a new branch in which you want to reinstall, this will create a copy of the branch you want to change, in case something goes wrong, just delete rebase and nothing will change. You can simply mark the commit hash.

The easiest way to get started with git rebase -i HEAD~N , where N is the number of commits in the past that you want to change.

It should open an editor, you can change the first word of each line to something like editing. Then save the file and you will be rebooted!

When rebooting, you can do anything you like, add files, delete files, etc. When you commit, if there is no conflict, it should automatically continue. And as soon as everything is done, you will have a restored branch with a commit, or a deleted, edited, renamed one, and that's all you wanted. Then if everything is correct. Delete the old branch (do not reinstall) Click on the new branch with the buffer and voila.

Edit

If this is not very clear when he opens the editor. When you delete a row, the selected commit is deleted. If you delete everything in the file, it will not do anything. You can abort it at any time during the reboot, and the state of the git project should not change. Rebasing is a completely safe thing, because it does not change anything. If you keep links to old commits, they will not disappear. After a reboot, it creates an alternate path. You will have one old path and a new path. The old path can be dereferenced, and all references to this old path must be redirected to the new path.

Attention!

By the way, one thing I would do instead is to simply commit to something that captures the old commit. I believe that rebooting should be used only when necessary. For example, when your colleague accidentally backed up a 4gb database.

+6
source

Start by making changes and fixing as fixup! Original commit message fixup! Original commit message .

Then decide what you want to see in the repo (and what is allowed). If you are not allowed to rewrite history, then stop completely, you can do something possible.

If enabled, start an interactive reboot of the parent of the original commit. You will see the first two records of the original of the original and corrections with correction. Set all other commits to "edit" and run.

The process stops at each subsequent commit, where you compile, rewrite the content in order to know about the changes, work fine. and use rebase --continue . Then press the branch (and perform all the rituals prescribed for cases of rewriting a story).

+2
source

If the latch has already been pressed, you can return it

 git revert <commit hash> 

To get a commit hash, just use the git log command

+1
source

Source: https://habr.com/ru/post/1487705/


All Articles