I would like to define a new \"root\" branch in this git repository. By \"root\" branch I mean a branch that is entirely independent of all the other branches in the reposi
Is there some way I can create a completely bare branch in this repository.
The actual command to use (with Git 2.28+) is:
git switch --discard-changes --orphan newBranch
# or
git switch -f --orphan newBranch
git switch is available from Git 2.23 (August 2019), and replaces the old confusing git checkout command.
But I would recommend Git 2.28 (Q3 2020), because git switch --discard-changes --orphan has been optimized in that version.
See commit 8d3e33d, commit 8186128 (21 May 2020) by brian m. carlson (bk2204).
(Merged by Junio C Hamano -- gitster -- in commit ded44af, 09 Jun 2020)
builtin/checkout: simplify metadata initialization
Signed-off-by: brian m. carlson
Reviewed-by: Derrick StoleeWhen we call
init_checkout_metadatainreset_tree,we want to pass the object ID of the commit in question so that it can be passed to filters, or if there is no commit, the tree.We anticipated this latter case, which can occur elsewhere in the checkout code, but it cannot occur here.
The only case in which we do not have a commit object is when invoking git switch with
--orphan.Moreover, we can only hit this code path without a commit object additionally with either
--forceor--discard-changes.In such a case, there is no point initializing the checkout metadata with a commit or tree because
- (a) there is no commit, only the empty tree, and
- (b) we will never use the data, since no files will be smudged when checking out a branch with no files.
Pass the all-zeros object ID in this case, since we just need some value which is a valid pointer.
Test:
git switch master
echo foo >foo.txt
git switch --discard-changes --orphan new-orphan2
git ls-files >tracked-files
# test_must_be_empty tracked-files