14

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?

1
  • 1
    There are two main things you want to avoid if you are not sure about them: Pushing with the force flag -f, and triggering the garbage collection using git gc. Everything else is completely recoverable in a timely manner.
    – poke
    Jan 10, 2014 at 17:47

8 Answers 8

12

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)
2
  • by checked in, do you mean added? Jan 11, 2014 at 0:49
  • @BretFontecchio By checked in, I mean committed. (E.g. git commit.) It is true though that files added with git add are usually recoverable also, just not nearly as easily as files that were committed.
    – Ajedi32
    Jan 13, 2014 at 14:16
4

My biggest thing for learning git was commit early and commit often. If you have a log of your changes in version control, there is a way to restore it if you screw up. I had many moments over the last year where I thought I lost data, but searching through Stack Overflow taught me some neat tricks. Keep your data hosted on a remote server (like GitHub or BitBucket), so that if you completely destroy your repo it is still somewhere. If you do a git branch -D <branch> and delete a branch, all commits on that branch will be purged from the repo.

The only thing I can really warn you about is never rewrite history if you don't know exactly what you are doing. Things that can do this are git-reset and git-rebase. Never do a git push <remote> <branch> -f unless you know what you are doing, since that will force overwrite all commits with your local repo. If you have changed your branches history locally or if someone else contributed to the repo, this could cause major problems.

@meager made a good point too: if you delete a file that is not yet tracked/committed by git, you will have no way of recovering it.

As a side note, don't be scared of using git-reset and git-rebase, they just need to be used properly. For instance, I sometimes use git-reset to reset my working tree to the latest commit (undo all changed files) with git reset --hard HEAD or to undo the last commit message while keeping my working tree git reset --soft HEAD^. Git rebase can also be helpful to squash/rewrite multiple commits in your history. Just note that these can methods can lead to data loss, and you shouldn't do them if you have already pushed to a remote repo (since then you will need to do a git push -f.

4

Depending on what you think Git may or may not be tracking, Git can "lose" all sorts of information you might expect it to hold on. Branches and tags can easily be lost in the shuffle if you don't have a good understanding of Git internals or how it differs from other systems.

See How to use git to lose data

1
  • 5
    You could improve the answer by adding an example or two.
    – Magnilex
    Jan 7, 2015 at 17:19
3

git rm isn't that dangerous since you can retrieve your files afterwards, from a previous commit.

As a general rule of thumb, take care of the -f option: it forces Git to do something it doesn't want to do. (ex : branch -f or push -f)

0

As a handy tip, if you think you've deleted branches, annotated tags or reset to an earlier commit, you haven't lost them, your local changes are all recorded, and you can see them with git reflog.

It's interesting to look at it just to see what it records.

It lists the commit shas which you can use to restore branches to that state.

0

None of the above. It's very difficult to cause data loss in Git. Dataloss happens outside of Git, when you remove files which Git isn't yet tracking. Any perceived "data loss" that occurs inside Git is recoverable, provided you attempt recovery before garbage collection happens, which is a window of weeks.

Commit your changes frequently, in small steps. Don't worry about producing good commit messages or a pretty DAG; you'll squash all that stuff down before you merge your feature branch anyways. Until you've committed your work, that work is in peril of being lost.

5
  • 3
    It's very difficult to loose data if you know what you're doing. If you don't know Git and apply commands without understanding what they do, you'll loose data.
    – CharlesB
    Jan 10, 2014 at 16:52
  • 3
    @CharlesB Only if you inadvertently delete files which you haven't comitted yet, as I clearly state. You cannot truly lose data in Git once it's committed, unless you go through some pretty extreme measures to force early garbage collection, or wait a long time to try and recover it.
    – user229044
    Jan 10, 2014 at 16:56
  • 1
    @CharlesB Reading your answer, you have some pretty funny ideas of what it means to "lose" data. Temporarily misplacing a commit and having to go into the scary reflog ("Git Wizardy"? This is an extremely common use case) isn't lost data. Deleting a branch doesn't lose data. These are all things that anybody can recover from with a little bit of Googling.
    – user229044
    Jan 10, 2014 at 17:02
  • 3
    Data that isn't easily recoverable is only almost lost, yeah. It can be recovered, but I call it wizardry because it's uneasy. Sure you can Google it, but... some people won't have the idea to do so. So in this case it's lost, from the user point of view (which is the one that matters)
    – CharlesB
    Jan 10, 2014 at 17:19
  • 2
    Really? I would argue that data that can't be recovered is lost, and data that can be recovered is not lost. That's not a "point of view" thing, it's not subjective. The data is there. It is not lost. We're not talking about sending a hard drive to a data forensics person here, we're talking about learning how to use your version control system to check out a commit.
    – user229044
    Jan 10, 2014 at 17:56
0

As meagar said git rm is a deletion recorded in a new commit, so it's recoverable and can be used without fear.

git reset --hard can be especially harmful since it resets the "current commit" (HEAD in Git jargon) to another one. So if the previous HEAD wasn't referred in a branch or a tag, it is virtually lost (at least without wizardry). It also causes your uncommitted changes to be lost.

The same goes for deleting branch and tag: it can cause a line of commits to be purged from the repository. In those cases, where commits are hidden in the repository, you can recover them but it's technical and not very easy, so you'd better know what you're doing.

As in any other situation where your data is precious (and source code is), it is highly desirable to have a mirror of your repository, and to regularly push to it. It can be another local repository, a private GitHub repo, or just the backup of your repository using your current backup system. This way you can always recover things.

As others say here, watch out for untracked file that are indeed important. Untracked/ignored files should be only the one that are generated from the files under version control: executables and such.

0
0

There is a risk when you wrongly solve conflicts : In eclipse we had an issue, when conflict solving of a file. a.txt was claimed for conflict, while b.txt was pulled/fetched and shown in the index. If a user now removes the file b.txt from index back to unstaged - and only appends his solved a.txt, and commits and pushes - the commit will have the state of b.txt from the the users PARENT commit - not anymore the version he would have fetched. THE PROBLEM is, that this change will not be shown - the file is not listed in the commit. You cannot directly discover this issue. (Only if you check the content of the file - in case of a binary you can only check the BLOB.) A litle effort, you need two users, two repositories + one bare and two files. We discovered this in eclipse/egit - not sure if it is also an issue from console. You can check the blob's with git ls-tree <commit>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.