Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause data loss in git?

Tags:

git

I don't want to have to tip-toe around in git, I'd like to "move fast and break things" as they say at FaceBook. Actually, that's almost the whole point of Version Control I think. What do I really need to watch out for?

I'm guessing git rm, especially with -r can be dangerous.

What about when branching, what leads to overwrites?

like image 619
Bret Fontecchio Avatar asked Jan 10 '14 15:01

Bret Fontecchio


People also ask

How do I recover a .git file?

In this case, you can restore the file using either git checkout or git reflog . You can find the hash-ID of the previous commit from the command: git log . In case you don't have the hash ID, you can use the command git reflog .


1 Answers

In general, it's very hard to cause data loss in git. Git almost never truly deletes anything that's been checked into the repository, even when running commands that remove commits from history or delete branches.

The only thing you really have to worry about is commands that remove files that haven't been checked in to git. In general, git will require the --force (-f) or --hard flags for those commands.

Here's a quick listing of potentially dangerous commands and what to watch out for when using them:

Can permanently delete data not committed to git:

  • git rm -f - Can remove files that you haven't checked in yet
  • git reset --hard - Will delete changes that haven't been checked in to git yet
  • git clean -f - Will delete files not tracked by git
  • git checkout /path/to/file - Can revert changes that aren't checked in to git
  • git checkout <rev> -f - Can overwrite changes that aren't checked in to git
  • rm -rf .git - Don't delete your .git directory! That's what stores all your local history.

Can delete data on remote repositories (reversible, but you may not have the level of access necessary to recover commits on remote repositories):

  • git push -f - Removes history from branches on remote repositories
  • git push <remote> :<branch> -OR- git push <remote> --delete <branch> - Deletes remote branches

Can permanently delete already deleted data that would otherwise be recoverable (similar to emptying the trash on your operating system):

  • git prune - Permanently deletes commits that aren't reachable from any branch
  • git gc - Permanently deletes old commits that aren't reachable from any branch

Can delete local commits (they're pretty easy to recover):

  • git reset <revision> - Can remove history from a branch (it's locally recoverable though for about two weeks or so, unless you run git prune)
  • git branch -D <branch> - Deletes a branch that hasn't been merged yet (locally recoverable)
  • git branch -f <branch> <rev> - Can delete history from a branch (locally recoverable)
like image 200
Ajedi32 Avatar answered Oct 20 '22 10:10

Ajedi32