Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Move commits before branches

Tags:

git

I have a commit history shown below:

  B-X
 /
A-C
 \
  D

I'd like to apply X to all branches B, C, D. Though cherry-pick does the trick, it makes history like this:

  B-X
 /
A-C-X
 \
  D-X

Now commit X is duplicated three times, which is very inconvenient if I have many branches. Ideally, I'd like the history to look like this:

    B
   /
A-X-C
   \
    D

Where X appears only once. This history is much cleaner. Which command should I use to achieve that?

like image 368
Yang Avatar asked Aug 07 '26 09:08

Yang


1 Answers

The first main point is that the fix you are requesting will rewrite history, which is only something you should do if no-one else is using the branch. For instance if you move D to be after X, then anyone else who had previously checked out D will get very confused when they pull. You should only do this if no-one else is using it, or they know and expect the change.

// Make a new temporary branch starting at A, and move X into it.
git checkout -b tmp A
git cherry-pick X

// For each of B, C and D, rebase them on top of the temporary branch.
git checkout <branch>
git rebase tmp
like image 72
loganfsmyth Avatar answered Aug 09 '26 22:08

loganfsmyth