-
-
Save seanbuscay/5877413 to your computer and use it in GitHub Desktop.
cd repository | |
git checkout --orphan orphan_name | |
git rm -rf . | |
rm '.gitignore' | |
echo "#Title of Readme" > README.md | |
git add README.md | |
git commit -a -m "Initial Commit" | |
git push origin orphan_name |
git reset --hard
also removes everything to its initial state
according to this answer on StackOverflow, git reset --hard
cannot be helpful in this specific case.
Interesting, it's worked for me. For example:
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (master)
$ ls | wc -l
5
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (master)
$ git checkout --orphan orphan-branch
Switched to a new branch 'orphan-branch'
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (orphan-branch)
$ ls | wc -l
5
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (orphan-branch)
$ git reset --hard
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (orphan-branch)
$ ls | wc -l
1
Rob@Robtimus MINGW64 /d/Code/Java/github/connect-sdk-java/connect-sdk-java-spring-boot-starter (orphan-branch)
$ git status
On branch orphan-branch
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.classpath
.project
.settings/
target/
nothing added to commit but untracked files present (use "git add" to track)
After the checkout, I omitted the git status
because it showed every file being marked as a new file. Maybe that's why it didn't work on SO, because files were not part of the index.
Note that using git switch --orphan <branch name>
automatically removes ALL files from the working tree.
@shadiakiki1986 perhaps you're looking for gist:7df6c8a50c04e5b80d84f28938b3e258
Commenting on KartikSoneji (Jul 15) above: git: 'switch' is not a git command. See 'git --help'.
@anfxanfx
git switch
was introduced in git 2.23, you might need to update your git client.
https://git-scm.com/docs/git-switch
For those who don't have access to a bleeding-edge version of git, I have a simple work-around: make an empty commit when the repo is new, tag it, and then use that as the base for any orphan branches.
git init
git commit --allow-empty
git tag empty
then git checkout --orphan
new_branch
empty
.
As a bonus, you can now use git rebase -i empty
, to amend or remove what would otherwise have been the "initial" commit.
@KartikSoneji the documentation still (as of 2.29.0) says:
THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
On git 2.23, all I needed was:
git switch --orphan <branch_name>
git commit --allow-empty -m "<commit_msg>"
git push origin <branch_name>
shadiakiki1986
you're looking for "checkedout", not "checkout'ed" 👍
@SeanHorner see gist:7df6c8a5…
(I note that checkout
is a git verb, so in this context it's not so unreasonable to conjugate it as checkouts and checkouted - just as long as nobody says those out loud.)
@SeanHorner see gist:7df6c8a5…
(I note that
checkout
is a git verb, so in this context it's not so unreasonable to conjugate it as checkouts and checkouted - just as long as nobody says those out loud.)
Ah, didn't think about it in that context, was using my linguist brain, not coding brain xD. Very interesting (and thorough) discussion in the linked gist 👍
Note that using
git switch --orphan <branch name>
automatically removes ALL files from the working tree.
Note that whatever is ignored by .gitignore won't be removed.
Note that using
git switch --orphan <branch name>
automatically removes ALL files from the working tree.Note that whatever is ignored by .gitignore won't be removed.
Note that git switch --orphan <branch name>
automatically removes ALL tracked files from the working tree (as it should, since we don't want files from other branches — it's an orphan branch). It will not remove
- untracked files
- modified (not yet committed) tracked files (because it refuses to proceed in this scenario)
This is independent of .gitignore
. However, there are many other ways to exclude files (e.g. .git/info/exclude
, git update-index --skip-worktree
etc). If you use any of those, possibly together and along .gitignore
, you probably have bigger things to worry about.
git reset --hard
also removes everything to its initial state