Francois Botha
Francois Botha

Reputation: 4839

Git - ignore first few commits but push other subsequent commits to branch

I often find myself in the situation where I clone a repository, create a local branch and then have to do some initial setup work, which changes some files, before I can continue with the actual changes that I want to push later.

For example: I have cloned a Visual C++ project and created the local branch. Now I have to change the include and lib directories to correctly reference libboost. This changes the .vcxproj file, but it is obviously a change that I don't want to send back to origin/master. Only the subsequent changes are real changes.

I have considered using git update-index --assume-unchanged <file>, but that will ignore the entire file and hence it rules out the possibility of other changes to the file that I'd want to commit.

What are solutions are there? Is git rebase perhaps the solution?

Upvotes: 0

Views: 535

Answers (3)

jthill
jthill

Reputation: 60275

Filters can make the process painless. Here's the basic idea, you get sed to save the upstream's current values and replace them with yours on checkout, and save your current values and replace them with upstream's on checking. Can you provide a sample of the changes you want to keep from leaking?

Upvotes: 1

FelipeC
FelipeC

Reputation: 9488

Yes, most likely git rebase is what you want.

There's many ways to do it. One way would be to start with the changes you don't want to send, then when you are done use git rebase --interactive so you can reorder the commits and put those unrelated changes at the end.

Something like:

  • Project change (project)
  • Fix 2 (fixes)
  • Fix 1
  • Last commit (master)

I'm manually adding branch names (master, fixes and project), although you can do it without branches. For example, if you want to push all the commits of the 'project' branch as a 'fixes' branch, except the last one, even though you don't have that local branch, you can do:

git push origin project^:fixes

Another option is to have a permanent 'project' branch that has the local changes to '.vcproj.default', and then have your other branches be based on top of it:

  • Fix 2 (fixes)
  • Fix 1
  • Project change (project)
  • Last commit (master)

After you are done with your 'fixes' branch, you can rebase your branch before pushing it:

git rebase --onto master project fixes

This will basically pick the commits from 'project' to 'fixes' ("Fix 1" and "Fix 2"), and rebase them on top of master, essentially removing the "Project change" commit from the branch.

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 157967

I'm not sure whether I got your question correctly, but I think it is not a good idea to put a config file which contains local settings directly into the repository. Instead I would add a file, lets say .vcproj.default to the repository which contains default definitions for all settings. After cloning a repository you just

cp .vcproj.default .vcproj 

and then change the settings you need to. The file .vcproj itself I would exclude using the .gitignore file.

Upvotes: 1

Related Questions