Is it possible to rebase specific files in git?

26,746

Solution 1

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.

Solution 2

You can revert a file to what is last committed with git checkout [file path]. For example:

git checkout /views/index.html
Share:
26,746
merlin2011
Author by

merlin2011

I am a student. A perpetual student. For the convenience of those who Google and copy &amp; paste resulting URL, feel free to try my plugins for Firefox and Chrome. I have recently released my primary project Arachne, a lightning-fast cooperative threading library. Please give it a whirl and create an issue if you see any problems. I have also written a few simple tools, such as one for tmux automation, an improved version of the venerable Unix column, and a tool for adding color to text in the terminal based on user-specified patterns.

Updated on August 04, 2022

Comments

  • merlin2011
    merlin2011 over 1 year

    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?

  • Jonathan Ben-Avraham
    Jonathan Ben-Avraham almost 3 years
    This is a really good answer, +1, but needs improvement. You need to state what the drawbacks are for the "revert" and "checkout' options.
  • ZenoArrow
    ZenoArrow over 2 years
    I managed to do it by using a mix of git and PowerShell, for example the following will get the files that have been added or updated between two commits, excluding files with specified file extensions, and check them out in the current branch... foreach ($changedFile in (git --no-pager diff --name-only --diff-filter=AMCR sha1 sha2 | ? { ($_ -notlike '.sln' -and $_ -notlike '.yml')})) { git checkout develop $changedFile }