First, you need to regularly backup your git repository (actually, any important data -in particular your source code- should be backed up; hardware is failing, people are making mistakes). A popular way of doing that could be to often git push --all
to some external repository, e.g. on github.
But if you discipline yourself -and you really should- to git commit
and git push
quite often (e.g. typically after every hour of development at most, or when fixing a single bug or adding a small feature), you practically won't lose much data. You'll always be able to come back to any state after any commit (and that is the most powerful feature of git
). So in the worst case you lose everything after your last commit (& push), which is not a big deal, since you have the habit to git commit
(& git push
) quite often.
If you don't have a network connection, still git commit
very often, but do git push
-on the network, into a remote repository- for backup purposes (at least daily).
Indeed, a git checkout
would overwrite any non-committed file (notice the terminology: for subversion, svn checkout
is not doing a similar thing: the svn checkout
& git checkout
commands look similar, but are very different).
The important thing is to git commit
(and git push
) very often (and that is your responsibility). Of course use git branches.
Use very often the git status
command. Be careful about the files you are ignoring in .gitignore
(which you should manage with git
). Once in a while (perhaps weekly, and certainly before any important software release), consider perhaps git clone
-ing your repository (in some fresh directory) and build your software in the cloned repository to be sure that everything required is there.
I want to know those before losing any important thing accidentally.
So if you git commit
(& push
) often, you won't lose any important data. At most you'll lose everything you did since your last git commit
. Not a big deal. Of course you should never touch "manually" your .git/
hidden directory used for the git repository. And you should git push
daily to some remote repository, in particular to avoid losing your work if your hardware breaks (e.g. if you drop or burn or get stolen your laptop).
Don't expect a software system to decide when you should git commit
. That is your responsibility and you should do that very often (but at appropriate times). The nice thing about git
is that a git commit
is really very cheap and quick. So you should use it very often.