1

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.

Smoke
  • 141
  • 1
  • 10

3 Answers3

4

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

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Ahh, perfect, the --orphan did the trick of what I was looking for ! thanks a lot, clearly thats what I was missing – Smoke Jul 21 '23 at 23:45
  • The problem with this is that there is no return back to use git merge right? since unrelated histories, If I perform 2 commits back on the master branch I wont be able to merge them into the public one – Smoke Jul 22 '23 at 00:32
  • 1
    No, you cannot merge unrelated histories. You might be able to use `cherry-pick` but merging unrelated histories doesn't work well - after all, it would create a new (merge) commit containing the histories of both sides until the common anchestor. You should decide for one history and use that. – dan1st Jul 22 '23 at 06:18
1

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.

  • That was what I was doing, but didnt work out as expected. When I create public branch and checkout to it and run git merge --squash master it say "already up to date", and public branch still has all the history of master. – Smoke Jul 21 '23 at 23:49
  • That's because the branch is being created based on the master. I might be wrong but I think [this](https://stackoverflow.com/questions/34100048/create-empty-branch-on-github) is the answer you're looking for. – Michał Gagoś Jul 21 '23 at 23:59
0

If you have a first commit, which corresponds to your "Initial Commit", then you can find it by running

git log --reverse

to see your commits in reverse order. Then, find the hash of the exact commit you want to branch out from and run

git checkout <yourhash>

and then

git branch <yourbranch>

You can also rewrite your git history, see https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

If you just need a new empty branch, you can run

git checkout --orphan newroot

but don't forget to create an initial commit :)

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175