Home > Blockchain >  How to disable multiple commits in git?
How to disable multiple commits in git?

Time:10-16

So, I've just started using Git, having been a happy (and probably rather superficial) TortoiseHG user, for many a year.

I've got 10-ish commits in a row since the last time a particular thing used to work in the code, and about half of those commits are on my list of suspects for introducing the issue. The challenge is that the last two commits solved a different issue which prevented my test from working, therefore they have to stay active, otherwise I can't even run the test, and things get complicated.

I would like to "disable" the suspects in my local repo, i.e. update my working directory to the state it would have if those 5 commits had not been made, and run some tests, then individually enable/disable them, to see where things went wrong.

I know I can revert individual commits, but that gets tedious and adds extra changes which then need to be committed/reverted as the search progresses. There does not seem to be a function that produces a working directory containing the state if with some subset of commits not applied.

But it would seem like there should be a way to create a local branch (which I can delete once done, and never push to the server), which consists only of the commits which I believe to have been safe, to which I could then add the suspicious commits one by one, to see when things break.

Is that correct? And if yes, how would I do this? If not, is there another way?

I'm currently testing various GUI clients for git on Linux -- so bonus points for solutions which can be applied via the GUI of one of these clients and don't require fancy specific commandline parameters.

CodePudding user response:

I'll suggest making use of an interactive rebase, which will allow you to reorder and even remove the problematic commits.

Command: git rebase -i <last known good commit> Docs: https://git-scm.com/docs/git-rebase

Other options could be creating a patch/stash of the last two commits which you need to make the test work, then using git bisect as normal, where instead of directly running the test, you do:

git stash pop
run test
git stash
git bisect <test result>

Best of luck!

CodePudding user response:

I presume the "HG" in "TortoiseHG" means Mercurial, and that probably explains the gap in mental model here: unlike in Mercurial, a commit in git does not represent a change. Instead, it represents a snapshot of the entire repository at a particular time. Rather than constructing states from a series of changes, git constructs changes by comparing states.

Given that, the concept of "disabling a commit" has no meaning in git - there is only one commit which contributes to the working copy, and that's the one you've currently got checked out.

It is very easy to go through the commits in order and test them - you can check out any commit by its commit hash, and your working copy will reflect the state that was committed. You can also use git bisect to perform a binary search through a long list of commits rather than testing each one in order.

If you want to test combinations of changes, you will need to create temporary branches with those combinations on, and then throw them away. For instance:

  1. Create a temporary branch
  2. Use git reset --hard some_revision_hash to point it at (and check out) a particular revision
  3. Use git cherry-pick to recreate the changes you made with one or more of the subsequent commits
  4. Test
  5. Go to step 3 to add more changes, or step 2 to start from a different base point
  •  Tags:  
  • git
  • Related