Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Make new branch and push without history

Tags:

git

git-branch

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".

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.

like image 306
totalZero Avatar asked Oct 20 '25 03:10

totalZero


2 Answers

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

like image 64
dan1st Avatar answered Oct 22 '25 15:10

dan1st


I think what you're trying to do is to squash commits.

Squashing commits essentially lets you simplify your repository by replacing multiple sequential commits with a single one.

How you can do it:

  • You can just create a new branch (for example public).
  • Switch into the branch using git checkout <new_branch>
  • Then you can squash commits by using git merge --squash <branch_you_want_squashed>
  • Then you commit the changes with a new commit message. git commit -m "<your_commit_message>"
  • And finally you just push the changes using git push

If you want to learn more about squashing commits you can learn about it here.

like image 32
Michał Gagoś Avatar answered Oct 22 '25 15:10

Michał Gagoś