Wizzardzz
Wizzardzz

Reputation: 841

Git do changes on two separate commits

I currently have a branch that has two commits :

456754 Just in case work

435678 Mendatory work

Unfortunately I have to do some more work on that branch, but I absolutely need the changes to stay separate in two commits (in case we do not need the just in case work after all). I know this is a stupid situation to use a single branch and try to separate stuff like that but the decision is not mine here.

How can I add work to these two separate commits ? As the commits suggests, Just in case work has the changes that Mendatory work has but not the other way round.

I hope this is clear, thank you

Upvotes: 0

Views: 40

Answers (2)

Matěj Koubík
Matěj Koubík

Reputation: 1147

You can use fixup feature to "ammend" some commit to a previous one without actually changing the history.

commit fixup

You just add the intended changes to the stage (by gui or cli) and then run git commit --fixup=<hash of ammended commit> (or in some GUIs you can right click the commit in tree and select fixup). Then you have a history like:

- commit A
- commit B
- fixup! commit A
- fixup! commit B
- fixup! commit A
- ...

You just have to make sure there are no conflicts between fixups of different commits.

rebase autosquash

When you are happy and want to actually ammend all the changes to these two commits, you run git rebase -i --autosquash master and actualy modify the history. You may have to resolve possible conflicts at this point.

More resources on this

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

This feels a bit hackish to me, but you could try:

# from HEAD of your current branch
git reset --hard HEAD~1
# now do the work which belongs on 435678
git commit --amend -m 'work for 435678'

# now reapply the 456754 commit and do the other work there
git cherry-pick 456754
# work work work
git commit --amend -m 'work for 456754'

This would leave you with the "same" two commits, which, however, each now contain the changes you want. "Same" is used in quotes here, because we have rewritten the history of this branch, and the two commits will have new SHA-1 hash values after doing the above steps.

Upvotes: 1

Related Questions