开发者

Is nothing truly ever deleted in git?

I'm currently learning git, usually I'm a bit skeptic of VCS since I have开发者_开发问答 a hard time getting used to them.

I deleted a branch called "experimental" with some tmp files, I saw the files removed in my working directory so I scratched my head and wondered if this is normal, can I bring it back in case I need it again, etc.

I found the SHA making the commit of the tmp files and recreated the branch with the provided sha and saw it again with all the files and their current content.

Everything I do in the working directory can be reverted once I commit it?

Might seem like a silly question to many people, but it kinda intrigues me so I want to know the limits


Almost every action can be reversed if you have committed the corresponding changes to your files. Even if you force-delete a branch or do a hard reset you can find the loose commit objects by using git reflog or git fsck.

However, loose objects are occasionally cleaned up by git gc, so you can't recover them after some time.


The main point in any serious VCS is to keep the entire history of a project in a permanent and immutable way. So you can at anytime go to an arbitrary revision of your work.

There is a special behavior of git when it comes to its storage, in that it can remove objects if there are no references to it. References are:

  • each HEAD of a branch
  • tags (I'm not sure if an unreferenced annotated tag still acts as an active reference)
  • each commit which is referenced by another commit
  • the content of a branch reflog

This means that all commits, which are part of a branch are kept, also each object(=mostly commits) which are tagged. There is also a so called reflog on each branch(unless deactivated), where git keeps references to all objects you created in the last days. When an object is not referenced by any branch, tag or reflog, git gc removes it form the database. This is typical the case when you created a hacking branch, committed some stuff there, and removed this branch without merging it into another branch.


Everything I do in the working directory can be reverted once I commit it?

Yes you can. As an example, try git reflog. This will give you a list of commits made on the current branch. Pick one by its sha1 sum, then type git checkout 45db2a.... That checks out that commit. Be sure to git checkout HEAD or git checkout -b newbranch from that point if you want to make changes.

You can also use git to cherry pick these commits on top of your current HEAD (i.e. if they're done in a different branch, you can pull them in). This question discusses it far better than I can.


git reset --hard can remove changes irreversibly

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜