Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ever lose committed work, in git?

Tags:

git

Is it ever possible to lose work in git? Let's assume all my work is committed, and I didn't run git gc. If I try some "funky command" ( NOT rm -rf .git ), and something strange happens to my project, could I recover from it? Is there something in particular I should avoid doing? Or clone the repository elsewhere before attempting it?

like image 344
Geo Avatar asked Sep 01 '11 19:09

Geo


People also ask

Does git ever delete commits?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Can a commit be deleted?

You can simply remove that commit using option "d" or Removing a line that has your commit. In the latest git version there is no more option d. You need just remove lines with commits from rebase to delete them.

How do I get back my committed changes?

In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option.

What happens if you drop commit?

(drop) — If you remove a commit from the interactive rebase file, or if you comment it out, the commit will simply disappear as if it had never been checked in. Note that this can cause merge conflicts if any of the later commits in the branch depended on those changes.


1 Answers

Most errors you make with git can be recovered through the use of the reflog. One exception to this is deleting your branch, because of course that deletes the associated reflog. If that happens you may still be able to find the branch again by looking at the reflog of HEAD, but if you haven't checked the branch out in a while (or never checked out the latest tip of the branch) then it won't be there. But even then, you can try using git fsck --lost-found to find your dangling commits, and trawl through them to find likely candidates for the branch tip.

As you already indicated, if you run something which corrupts the .git directory, then all bets are off.

like image 52
Lily Ballard Avatar answered Sep 25 '22 21:09

Lily Ballard