Nothing really deleted in git?

I am currently studying git, usually I am a little skeptical about VCS, as it is difficult for me to get used to them.

I deleted the branch called “experimental” with some tmp files, I saw the files deleted in my working directory, so I scratched my head and wondered if this is normal, can I return it in case I need it, and etc ..

I found a SHA that commits the tmp files and recreated the branch with the sha provided and saw it again with all the files and their current content.

Everything that I do in the working directory can be returned as soon as I complete it?

It may seem like a silly question to many people, but it interests me, so I want to know the limits

+7
source share
4 answers

Almost all actions can be undone if you make the appropriate changes to your files. Even if you force delete a branch or perform a hard reset, you can find free commit objects using git reflog or git fsck .

However, free objects are sometimes cleaned up by git gc , so you cannot restore them after a while.

+7
source

Everything that I do in the work directory can be returned after I commit it?

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

You can also use git for cherries to select these commits on top of the current HEAD (i.e. if they are made in another branch, you can pull them in). This question discusses it much better than I can.

+2
source

The main point of any serious VCS is to keep the entire history of the project constant and unchanged. Therefore, you can at any time proceed to an arbitrary revision of your work.

There is a special git behavior when it comes to storing it, since it can delete objects if there are no references to it. References:

  • every head branch
  • (I'm not sure if an annotated tag without links still acts as an active link)
  • each commit referenced by another commit
  • reflog branch contents

This means that all commits that are part of the branch are saved, as well as every object (= basically commits) that are marked. There is also a so-called reflog for each branch (if not deactivated), where git stores links to all objects created in recent days. When an object does not refer to any branch, tag or reflog, git gc removes it from the database. This is typical of the case when you created a hacker branch, there were some things and deleted this branch without merging it into another branch.

+2
source

git reset --hard may permanently delete changes

+1
source

All Articles