2

I have been working on a Git project that will soon be shared with other developers. There is a section of my project that has information I'm not allowed to share.

If my project history looks like A -> B -> C -> D -> E -> F, and the information contained in commits C and D is private, is it possible to modify my project history so that it looks like A -> B -> E' -> F' (where E' and F' are E and F without the confidential information) before sharing?

It is my understanding that if I perform a manual merge between F and B to create F', then the final commit will be what I want but it won't preserve the project history. I think it will look like A -> B -> F' instead of A -> B -> E' -> F'.

TimD1
  • 982
  • 15
  • 26
  • Possible duplicate of [Remove sensitive files and their commits from Git history](https://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history) – phd Aug 08 '17 at 01:06

3 Answers3

1

I believe what you're looking for is a rebase.

If commits C and D are self-contained, ie if they aren't needed for commits E and F to be applied cleanly, this should be fine.

The appropriate syntax would be:

$ git rebase --onto B D F 

Where B, D and F are either the hashes or the references to those commits.

Be careful when doing this if those commits have already been pushed to a public repository though, as rewriting history for commits that have already been made public is usually dangerous and is discouraged.

thegards
  • 33
  • 7
0

There are simple and complex ways of accomplishing this. Here is a very simple approach, which lets you easily grep diffs for troublesome snippets to verify their absence.

Create a new repo to share. (You could store the history-less F' snapshot in it and be done, except that you wish preserve some history.)

Use git format-patch in the old repo to store diffs for A, B, E', F' in /tmp.

Use git am to apply those diffs to the new repo. Publish it.

J_H
  • 17,926
  • 4
  • 24
  • 44
0

There is a powerful tool that allows you to remove sensitive information, irrespectively of what commits were introduced it: BFG, The Big Repo Cleaner.

BFG will allow you to selectively remove sensitive information from the history of your repo. For example, you can use it to remove a file that you added by mistake to your repository. It will also allow removing specific strings from the history of files in your repository.

The advantage of BFG over other methods is that it scrubs history without affecting commits that are not of concern. However, it will change the commit ids of some commits already in the repository. See its FAQ for details.

dmg
  • 4,231
  • 1
  • 18
  • 24