0

This is my requirement: I have a -> b -> c (HEAD). I am adding a new commit d. Now, it becomes a -> b -> c -> d(HEAD).

Now, I want to revert to c (that is, undoing all changes that were made in d) and make an additional change and form e. The tree should look like a -> b-> c -> d -> e(HEAD). NOTE: I must not lose the d history. I must revert it and add new changes to e.

UPDATE: I can explain with a better example. Let's say I have a->b->c(HEAD), here c is the primary or main commit. I'm trying to automate with a script.

Initial Step: I start with git checkout <SHA1 of c>, tree looks like: a->b->c(DETACHED HEAD)

My Real query starts from here:

  1. I modify few files (I don't add any new files) and add a new commit d. So tree should look like: a->b->c->d(HEAD)
  2. I revert changes in #1 by running Initial Step and modify other files (again, I don't add any new files) and add a new commit e. So tree should look like: a->b->c->d->e(HEAD)

NOTE: Here e = revert of commit d + new changes to c. I can't blindly use git revert HEAD since c is considered to be the primary commit. I was thinking I could use git stash and git stash pop to remove old changes and insert new changes. Can I do something like git checkout stash?

In short: I'm trying to fuzz test commit c for around 50 times (ie) commit and revert for 50 times

dinesh2707
  • 33
  • 4

3 Answers3

3

The git revert command does precisely what you describe as desired -- it creates a new change on the top of the current branch that reverses some previout change. In your case

git revert HEAD

will create a new change (your e) on top of HEAD that reverses what was changed in HEAD (d)

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • This will create a commit that undoes a change. I need to add new changes to the commit `e`. That is, `e` has new changes + reverted code – dinesh2707 Oct 18 '17 at 05:34
  • If you need to add changes to a commit, use `git commit --amend`. If you need to combine the last two commits into a single commit, use `git rebase -i HEAD~2` – Chris Dodd Oct 18 '17 at 14:26
0

What about

First commit changes made to d

$ git commit -d "Your message"

Then create new branch e on c and checkout Something like

 $ git checkout -b e c
jitender
  • 10,238
  • 1
  • 18
  • 44
  • seems like this would work. But it would lead to handling lot of branches. I have updated the question with an example – dinesh2707 Oct 18 '17 at 12:35
  • other option would be `git -stash` instead of commit it would note:- this will create a stack of changes mean it will keep history but you can't directly switch between histories you can get in to history one by one `stash pop` – jitender Oct 18 '17 at 12:44
0

If you want to put additional changes to the revert commit as well, use -n flag in revert command:

git revert -n HEAD

This will revert the changes in HEAD but will not create a commit, so you can make your additional changes and commit when you are done.

1615903
  • 32,635
  • 12
  • 70
  • 99