camillobruni
camillobruni

Reputation: 2318

Insert a commit without merging

I have the current situation

A ----- D
 \
  B - C (master)

Now I would like to insert the D commit without merging, e.g. not changing the contents of the future commits B and C.

Basically rewriting the parents of the B commit (and hence all the other commits upstream).

In the end I would like to have without manual interaction:

A - D - B - C

Upvotes: 1

Views: 305

Answers (3)

camillobruni
camillobruni

Reputation: 2318

I figured out the proper way to introduce a new commit to the history without changing its contents (beside the commit SHA of course) is to use an intermediate git graft.

In the following example I assume that there were no previous grafts:

# use a graft to temporarily change the parent of B to be D
echo "$SHA_B $SHA_D" > .git/info/grafts
# apply the grafts, changing the SHAs of the commits B...C
git filter-branch
# remove old pending commits
git gc
# remove the no longer needed grafts
rm .git/info/grafts

This leaves all commit contents (files and folders) untouched.

Upvotes: 1

Adam Dymitruk
Adam Dymitruk

Reputation: 129724

batch mode would be done on things you have in your current branches history. In this case you are looking at either the history of D or the history of your master. The simplest thing to do here is:

git rebase D

(assuming you have master checked out already) Why do you need this to be done in batch mode?

Upvotes: 1

Borealid
Borealid

Reputation: 98539

The action you're looking for is rebase in interactive mode.

git checkout C
git cherry-pick D # Now: A - B - C -D
git rebase -i A
<reorder commits>
git log

The --interactive / -i option will open up the history in your editor, allowing you to jumble things up as much as you like. You can even squash multiple commits together or eliminate some entirely.

If you want to be totally mundane, you could also do it this way:

git checkout A
git cherry-pick D B C

Upvotes: 1

Related Questions