How to push new branch without history

Git

Git Problem Overview


I have git repo with two unrelated branches, master and configs. I've created configs, purged all the files and then placed in configuration files only. Now I want to push this on remote repo, but as it were new, empty branch (without logs of all my changes and old revisions of original branch).

How can I purge all the history and push it?

Git Solutions


Solution 1 - Git

Purging all the files doesn't get rid of the history. You need to create a branch that has no history first, and the add your config files. These days git checkout has a --orphan option that makes a branch with no history. Here's the information on the --orphan option:

> --orphan <new_branch> > > Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits. > > The index and the working tree are adjusted as if you had previously run "git checkout <start_point>". This allows you to start a new history that records a set of paths similar to <start_point> by easily running "git commit -a" to make the root commit. > > This can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code. > > If you want to start a disconnected history that records a set of paths that is totally different from the one of <start_point>, then you should clear the index and the working tree right after creating the orphan branch by running "git rm -rf ." from the top level of the working tree. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc.

Here's a link to the documentation for checkout. You can also run git help checkout as well.

Once you've created your branch without history, then when you push it to the server, it won't have that history either. FWIW, it helps me to think of git push as "make the remote branch look the same as my local one". So if you have history, and push, it will have history. If you don't, then the pushed branch won't.

Solution 2 - Git

How can I purge all the history and push it?

git checkout --orphan <name_you_choose_for_orphan_branch>
git commit

Then you can run your

git push <remote-name> <branch-name>


cf. this one-liner

Solution 3 - Git

I needed to do this to send a freelancer some code without sharing private information or deleting my history.

In case any of you are using SourceTree, this is how I managed to do it. Please let me know if I did something wrong or am forgetting something as I am very unfamiliar with source control management.

  1. I went to the terminal button on source tree.
  2. I created a new 'orphan' branch by using the command: 'git checkout --orphan clean-ver' where clean-ver is my branch name
  3. I closed the terminal and saw that my commit log was cleared because I was in a new orphan branch. I staged all my source code and committed the changes.
  4. I set up my bitbucket repository in either repository>add remote or repository>repository settings>add.
  5. I closed that, and clicked on the push button
  6. I selected my new 'clean-ver' branch said ok.

When I checked bitbucket, it seems that this seemed to have worked and discarded my commit history.

Hope this helps someone out.

Solution 4 - Git

git checkout --orphan <name_you_choose_for_orphan_branch>
git rm . -r -f

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomas PruzinaView Question on Stackoverflow
Solution 1 - GitJohn SzakmeisterView Answer on Stackoverflow
Solution 2 - GitGeremiaView Answer on Stackoverflow
Solution 3 - GitThinkBonoboView Answer on Stackoverflow
Solution 4 - GitolawalejuwonmView Answer on Stackoverflow