4 ms·
> What if you make a mistake and remove a branch that you didn't want to remove? In Git, a branch is just another name, like an alias, for a specific commit. T
by dmdeller 14y ago
> What if you make a mistake and remove a branch that you didn't want to remove?
In Git, a branch is just another name, like an alias, for a specific commit. Think of it like DNS, where the branch is the domain name and the commit is the IP address.
'quick-fix' => d34db33f
When you commit to a branch, it just updates the name to point to the new commit. The new commit points to the old commit, that one points to its previous one, etc., until you reach the beginning. It's a linked list.
When you 'delete' a branch, you're just deleting the alias named 'quick-fix'. You're not deleting the commit, d34db33f, or any of the other commits that it points to (assuming that d34db33f is still pointed to by other commits, i.e., you merged it into master or another long-lived branch). You can get it back again at any time:
git branch quick-fix d34db33f
This will create a new branch named 'quick-fix' that points to the commit d3adb33f - the same commit the previously deleted 'quick-fix' branch was pointing to - which effectively restores the branch exactly as it was before it was deleted.
A merge is just like any other commit, except it points to two parent commits instead of one.
I usually suggest to new git users that if they're really afraid of losing track of old branches, they can make them into tags after deleting. A tag is really just a read-only branch; it's an alias for a commit that you can't add new commits to. Once the user gets more comfortable with git, they usually don't feel the need to do this any more (since, as above, you can always just get the branch back by looking at the history - in practice, you almost never need to do this).