I have a bit of a messy branch with 20 or so commits, and I'm preparing for a merge back to master. I've rebased it off master, and looking closer, I realise that there are files being modified in ways that are totally irrelevant to this branch, and not ready to be committed. The changes to those files aren't confined to specific commits.
So basically, I don't want anything to do with those files to be included in this branch if possible. Is there a good way to go about this? My fall back position #1 is obviously to just copy over the latest copy of each of those files then commit. But then the history will still contain the changes, and the Git Gods will frown upon me.
Fall back position #2 is to do the same, then squash the whole branch history down to one commit.
Any improvements on these?
To checkout to a specific version you can do
git checkout <sha1> <file>
wheresha1
is the unique sha1 hash of that version you want the file to be in.Say your history is
Note:
lola
is a non-standard but helpful alias.The commits have modified three different files.
The changes to file
b
are the ones you want to keep, and you want to discard all changes toa
andc
. One way to do this is withgit filter-branch
.The tree filter above checks out the commits in the named range and restores files
a
andc
to the content at the “merge base,” that is, the commit at whichtopic
branched away frommaster
.Now
tmp
has alltopic
’s changes tob
but no changes to any other file.As a safety measure,
git filter-branch
stores a backup of your original ref. When you’re satisfied with your changes and want to delete the backuptmp
, runI ran into a couple of problems as Windows user:
Here is how my command ended up looking: