1

If I create an orphan branch for my git repository.

I did git rm -rf .

Does it still have my history and files on other branches found in .git? I can still checkout master branch.

Should I just create another repository and clear out .git?

Checking git log on my orphan branch shows only my initial commit.

admin
  • 125
  • 11

3 Answers3

5

Assuming you mean git checkout --orphan name: what this does is arrange for the next commit to be a root commit. You are now on a branch that does not exist. This is basically the same state you would have in a new, totally-empty repository.

When you have no repository and then create one with git init, that new repository has no commits. This creates a problem, because in Git, a branch name holds the hash ID of one actual, existing commit. Hence this empty repository cannot have a branch. It has no commits. The branches must identify commits, and there are none. So it has no branches. Nonetheless, you're on branch master.

The way Git handles this is to allow you to be on a branch that does not exist. That is what—and in fact all—that git checkout --orphan does: it puts you on a branch that does not exist.

When you are in this state, the next commit you make will create a commit. The creation of that commit enables the branch to exist. Git writes the commit hash ID into the branch name, creating the branch name in the process. The branch now exists and has one commit on it. Because the branch you were on did not actually exist, the new commit has no parent: it is a root commit.

You edited your question to add:

I did git rm -rf .

This merely affects Git's index. The index is the entity Git uses to hold all the files that will go into the next commit. So the index is now actually empty. Except in a new, totally-empty repository, this state is pretty unusual: normally, the index holds all the files copied out of the commit you have checked out. You'll want to put some file(s) into the index, using git add, before making the new root commit.

You also edited your question to cross out a question:

Does it still have my history and files on other branches ...

Yes. History, in Git, is commits; commits are history. Adding a new branch name, pointing to a new root commit, has no effect on any existing branch names and the commits to which they point. No existing commit is harmed by the creation of a new commit.

All the commits that are in your repository continue to be in your repository. Your new root commit is now in your repository, and your branch name now exists, pointing to the new root commit.

torek
  • 448,244
  • 59
  • 642
  • 775
2

I'm not 100% sure what you are trying to do, but if you just did

git rm -rf .

and nothing else, you could potentially just do

git reset --hard

and be back to where you were before the git rm -rf. Is this the "history" you are looking for?

Alternatively you could reset hard to some other branch or commit to go back to some previous state.

topher217
  • 1,188
  • 12
  • 35
  • Maybe you did not commit your orphan branch and after `git rm -rf` you simply lost everything in your working directory without a commit? In that case, I believe all changes on the orphan branch would be permanently lost. – topher217 Nov 08 '19 at 06:56
  • Creating the orphan branch my goal is to clone the repo with only the orphan branch and restrict accessing the other branches – admin Nov 08 '19 at 07:09
  • I see. Does the orphan branch need to contain a certain amount of already existing commits (up to where it diverged from some master or other branch) or is it ok to have only the initial commit with the most recent files (as determined by whichever branch you were on when creating the orphan branch)? – topher217 Nov 08 '19 at 07:19
  • Possible duplicate of [this](https://stackoverflow.com/questions/1778088/how-do-i-clone-a-single-branch-in-git)? – topher217 Nov 08 '19 at 07:23
  • I think what I want is not possible, I want to keep record on my repository branches while keeping the orphan branch restricted when cloned. I'll settle for a separate release directory instead. – admin Nov 08 '19 at 07:31
  • 1
    Maybe I'm still misunderstanding something but it sounds like you can do that based on the info from the link above, i.e. `git clone --single-branch` or `git clone --depth=X` where X is the number of commits you want the history to go back to. See [here](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt) for man page related to this. This repo where you are cloning to can remain on its own access restrictions. – topher217 Nov 08 '19 at 07:35
  • Tested it. Works well, I can't switch to other branches anymore. Regarding on the security on this, after cloning does this still include objects from the other branches? – admin Nov 08 '19 at 08:03
  • I haven't used this functionality myself, just read the docs, but I suppose it depends what you mean by "objects from other branches". If your "master", or whatever branch where you branched off from, contained files that other branches also inherited, then you are thereby "including them", but whatever new files that appear in master, or otherwise, after you made this "orphan", those shouldn't be there. Furthermore, if you wanted to remove some files from this new branch you can just remove them from your working directory before making the init commit for that branch. – topher217 Nov 08 '19 at 13:09
1

In general, when you create a new branch off branch “master” you inherit its commit history. The exception is an orphan (or disconnected) branch. An orphan branch does not have a parent-child relationship to the master branches’ commits.

Most commits have one parent commit, one obvious exception being root commits which have no parent commits. Creating an orphan branch will retain the working tree of the branch it’s based on, but without an ancestor commit.

When you create an orphan branch, run the checkout command with the ‘orphan’ flag and give the branch a name. Run git log --oneline fatal: your current branch 'myorphanbranch' does not have any commits yet

This is because orphan branches do not inherit commits from the parent branch. Now run the status command. git status

You will notice that although all the files were copied to the orphan branch, they are in the staged area. That is because no commits have been performed thus far in the orphan branch.

We can remove all files in the working tree with the git rm -rf . command.

Now we have a branch with no commits and no files. Run the Git ls-files command to the view files in the orphan branch. You’ll notice there are no files in the branch.

Although orphan branches are not commonly used, they are sometimes handy for writing documentation and creating static GitHub pages. They are also sometimes used to merge two unrelated repositories with different histories.

Community
  • 1
  • 1
Sohan
  • 6,252
  • 5
  • 35
  • 56
  • I wanted to use the orphan branch as a release branch to avoid getting my other branches source files to be opened. – admin Nov 08 '19 at 07:08
  • @sudo Yeah.. so if you try to clean the using `rm-rf` command it should solve your problem. If you find this is expected answer please accept – Sohan Nov 08 '19 at 07:15