Skip to content

Instantly share code, notes, and snippets.

@theowoo
Last active March 30, 2020 03:56
Show Gist options
  • Save theowoo/2823a7e7b785b6fde647d9d2f6e5e68d to your computer and use it in GitHub Desktop.
Save theowoo/2823a7e7b785b6fde647d9d2f6e5e68d to your computer and use it in GitHub Desktop.

Push an individual file from a repo to a new repo and keep command history associated with that file:

git clone <original repo url> temp-repo
cd temp-repo
git remote rm origin
git rm -rf .
git checkout HEAD -- <filename>
git commit -m "<commit message>"
git remote add origin <new repo url> # comments
git pull 
git push -f

Optional: Add new repo as submodule of original repo

git submodule add <new repo url>
@t7ko
Copy link

t7ko commented Sep 29, 2016

WARNING: this way you pull in the entire history of your "original repo" into the "new repo". Not just the history of that one file. This creates at least 2 problems:

  • You expose entire project to another project -- e.g. problem if you wanted to publish 1 file of a private project to open source.
  • The "new repo" size is increased by the total size of "original repo", not just the added file. Especially painful if you try to split huge repo into smaller ones.

@ananth-gpfw
Copy link

@t7ko, agreed! Do you have any suggestions for alternative methods?

@djiamnot
Copy link

djiamnot commented Aug 3, 2018

Keep the method but rewrite the history.

git checkout --orphan <new_branch> <your_last_SHA>
git branch -d master # delete the original master
git branch -m master # rename current branch to master
git push -f origin master

If you want/need to keep longer history, you can --orphan an earlier SHA and cherry-pick subsequent commits from the origin master (before you delete it, of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment