Discard some git, commit, save others?

Suppose I make commits A, B, and C, all of which relate to completely different code files. (Thus, they are independent and the fixation B can exist without changes in, etc.)

Commit A introduced an error, while commits B and C are fine. So I want to return to the state of my code before committing A, and then commit changes to B and C.

What is the best way to do this? One way is to just do git revert <SHA-of-commit-before-A> and then manually add it back to commits B and C - is there a better solution?

+4
source share
2 answers

You do not need to add back B and C : they are already committed and will not be affected by your return, which will add new commits (negative image A ).

Another way is ( if you haven't pressed those commits yet ) to do an interactive reboot ( git rebase -i A^ ), and change the order of your commits, completely dropping A
You will see the following parameters in your rebase (see git Book )

 # Commands: # p, pick = use commit # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # 

But then again, this will change your story and make you git push -force, which is not always a good idea if other people have already pulled from your repo.
In this case, refund remains the safest solution.

+4
source

If you have already made your changes (so that they are visible to others), you need to do the following: git-revert <SHA-of-A> . This explicitly makes the new commit the opposite of A. If no one has seen your tree, you can use git-rebase -i A^ (where A^ means "parent of A") to interactively reorder or omit the commits from A. In this case, you can remove A, and that will do exactly what you described (unwind only to A, and then reuse only B and C).

+3
source

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


All Articles