Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Git Make new branch and push without history

I have been trying to google for a solution but wasnt able to find exactly what I’m looking for, hence the new question.

I have a private git repo with only 1 branch (master) that has ~90 commits.

I want to create a new branch public, that points to another repo – which is public – but not want to show all the commits / history that master have, instead show only 1 commit like "Initial commit".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So far I was able to add a new remote, and set it to track the new branch, but when I push, it sends all the commits that master had.

>Solution :

You first need to create a new branch locally. By default, this will use your current HEAD as a base.

Creating a branch without history

However, you can also create a new branch without any history using git checkout --orphan:

# create a new branch without a history and check it out
git checkout --orphan yournewbranch

# edit your files

# create a commit with these files
git add .
git commit
# push that commit and create the remote branch
git push -u your_remote yournewbranch

Alternatively, you can just create a new repository with that branch and push that:

git init -b yournewbranch
git add .
git commit
git remote add origin https://yourgitserver.com/your/repo
git push -u origin yournewbranch

Add branch from other repository locally

If you already have the remote branch and want to add it to your repository, you can just checkit out using git checkout:

git checkout -b yournewlocalbranch remotes/yourremote/remotebranchname

This assumes the new branch exists on the remote yourremote with the name remotebranchname and you want the branch to be named yournewlocalbranch

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading