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?
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 .
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 yetgit reset --hard
- Will delete changes that haven't been checked in to git yetgit clean -f
- Will delete files not tracked by gitgit checkout /path/to/file
- Can revert changes that aren't checked in to gitgit checkout <rev> -f
- Can overwrite changes that aren't checked in to gitrm -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 repositoriesgit push <remote> :<branch>
-OR- git push <remote> --delete <branch>
- Deletes remote branchesCan 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 branchgit gc
- Permanently deletes old commits that aren't reachable from any branchCan 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)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With