开发者

Anonymous branch after doing git reset

开发者 https://www.devze.com 2022-12-21 20:11 出处:网络
Background: Trevor was working on a test project solely for the purpose of trying out git. This is a local one-person repository that has not been shared so Trevor did a reset hard in order to obliter

Background: Trevor was working on a test project solely for the purpose of trying out git. This is a local one-person repository that has not been shared so Trevor did a reset hard in order to obliterate some unwanted commits:

    :git reset --hard 6aa32cfecf4
    HEAD is now at 6aa32cf auto commit Sun Feb 28 16:00:10 -0800 2010

Then Trevor went along happily adding new commits to the project. Then, when Trevor looked at the graphical representation of the commit history, Trevor discovered that there appears to be an anonymous branch of the obliterated commits. It does not show up as a branch using git branch, but it does show up in the GUI.

Questions 1: How can Trevor get rid of this "anonymous branch" ... and what is Trevor really looking at? What are some p开发者_开发知识库ointers to help Trevor understand what happened when Trevor did the hard reset so Trevor can better set Trevor's expectations.

Questions 2: Assuming Trevor had shared the project with other people. What would be the alternative to do the same (or similar thing) without doing a hard reset?


As mentioned in The illustrated guide to recovering lost commits with Git, you can recover "lost" commits (as in "no longer referenced by a branch or a tag").
That is why they still show up in gitk.
For instance, a:

$ git fsck −−lost-found

would also display them.

To clean this up (assuming you having nothing to get back from any other delete operations)

 $ git gc --aggressive
 $ git prune

See also git gc: cleaning up after yourself.


If that branch had been shared, a possible alternative would have been a git revert in order to make a new commit cancel the n previous ones.


You can create some new branch by a commit.

If your last commit in your anonymous branch is 123e43 you can do :

git checkout -b my_branch 123e43e

Now your branch is non anonymous. You can merge or rebase it in your master branch

0

精彩评论

暂无评论...
验证码 换一张
取 消