1

I made a commit to a development branch that accidentally included some changes which I would like to drop, and some which should go into a separate future commit.

What I'm looking for specifically, is the equivalent of, as if I did an interactive merge on a forked branch with HEAD^ of the current development branch and selected the changes line by line, except I'd like to do it within the original development, branch amending the commit history, so that the commit exists as a single clean set of changes, rather than an interactive-merge from a different branch. Is this possible? Are there any better alternative work flows than the above?

A similar question has been asked here: Break a previous commit into multiple commits But the solutions provided involve adding different files from staging as separate commits, whereas I need to amend changes within the same files.

Community
  • 1
  • 1
user3467349
  • 3,043
  • 4
  • 34
  • 61

1 Answers1

2

One way to do what you want would be roll back the commit while keeping the changes, and then committing the changes in batches.

Undo last commit and keep changes:

git reset --soft HEAD~1

This is in contrast to git reset --hard HEAD~1 which would remove the changes as well with the commit.

Interactively add part of the file:

git add --patch filename.x

See https://stackoverflow.com/a/1085191/429758 for more details about interactively adding parts of the file.

Community
  • 1
  • 1
Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74