Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rebase specific files in git?

Tags:

git

rebase

This is similar to this question on merging, except that I am attempting to rebase branch A onto branch B, instead of merging branch B into branch A.

It is possible to achieve a similar effect in branch A using a combination of git checkout and git commit on individual files, but that does not have the same effect on the history as a rebase.

Is it possible to rebase only specific files so that all future rebases will not need to touch these files, without rebasing the history of all files?

like image 478
merlin2011 Avatar asked Sep 17 '14 02:09

merlin2011


1 Answers

Sort of. You might do interactive rebase, and when presented with the rebase script in your text editor, change all the actions from "pick" to "edit" then save and quit. Rebasing will now begin and it will stop after each commit applied making it possible for you to:

  1. Delete the changes in the files you're not interested in.

    This is doable by something like

    git reset <reference_commit> -- pathname
    

    where the <reference_commit> is the name of a commit which contains the files you don't want the rebasing to modify.

  2. Apply these changes by running git commit --amend -C HEAD.

  3. Run git rebase --continue.
  4. Rinse, repeat.

Any time git rebase applies the next commit being rebased you might be presented with a conflict instead of just a chance to edit the already recorded commit. This does not differ much from the normal editing — you just need to pay more attention to the current status of the index.

In any case, what you want to do, smells pretty strange to me: rebasing, normally, is "forward-porting your local changes on top of the updated base (upstream code)" which means if your changes can't work with updated upstream (and why else you'd want to keep some files from updating?) there's no sense in rebasing—simply because you'll not be rebasing, you'll be doing something else, and this might bite you later.

like image 121
kostix Avatar answered Oct 16 '22 21:10

kostix