Home > Back-end >  How do I introduce a file into a remote repository whilst removing all prior history?
How do I introduce a file into a remote repository whilst removing all prior history?

Time:04-20

I have a script whose changes I have been regularly committing to my local repository (the requirement at the time was not for this script to be included as part of a shared repository - it was intended to exist solely on my local machine).

This script is now required to be included as part of a shared remote repository; however, whilst developing this script, I included some private customer data as part of testing.

As such, I now need to include this file as part of the shared remote and want to know what the best way to go about it is in order to remove all prior history/commits.

My initial thoughts are that I can make a copy of the file / relocate the file, then delete the repository from my local machine, pull the remote repository in and then introduce the new file into the freshly-pulled repository (in order to push back again).

I don't know if this is a good way to go about it or if there is some other, more efficient way to do this and am happy to hear of any solutions.

CodePudding user response:

The first thing to do would be to squash all commits on your local repository into one, you can do this by using:

$ git rebase -i --root

then choose squash for all commits.

Once you've done that, rename your local branch:

$ git branch -m new_file_branch

Then, add the remote repository and fetch the remote branch (replace the ... with the link to your repo):

$ git remote add origin ...
$ git fetch origin master

Then, checkout the branch and merge the branch with the new file:

$ git checkout origin/master
$ git merge new_file_branch

(the above assumes that the main branch of the remote branch is master; if that's not the case, replace master with the name of the branch).

CodePudding user response:

The method you suggested is a simple and good way to go, while also being very safe. I would do that, instead of risking of exposing some data by accident.

  •  Tags:  
  • git
  • Related