Week 4Day 4 - "I pressed delete..."
Handling the pressure
We have had some awesome fun working with branches, and hopefully you can see how utterly powerful Git is.
Sometimes though we can get ourselves into trouble and it is here that Git can also come to our rescue.
Let us learn how to delete a branch.
We are going to delete our wacky branch now as we have already merged it and no longer require it.
john@satsuki:~/coderepo$ git branch -d zaney
error: The branch 'zaney' is not fully merged.
If you are sure you want to delete it, run 'git branch -D zaney'.
john@satsuki:~/coderepo$ git branch -D zaney
Deleted branch zaney (was 7cc32db).
john@satsuki:~/coderepo$ git branch -v
* master 9710177 Added another file
wacky 9710177 Added another file
wonderful cfbecab Fantastic new feature
john@satsuki:~/coderepo$
Ooops.
Despite the firm warning from Git, we have inadvertently deleted the zaney branch by mistake.
This does happen.
When people are in the thick of it, they do make mistakes and it is comforting to know that Git is able to recover from this, but how? We have deleted the branch reference that pointed to the commit that was at the tip of the zaney branch by using the -d and -D parameters.
So the question is, does that commit still exist any more? Maybe we did more damage than we thought.
By deleting all references to the commit, how do we get it back?
All is most definitely not lost.
Even though we have removed all references to the commits in question, they still exist in the repository.
They will continue to do so unless we run a clean up on the repository, which will be covered later, or if the items are left to age for more than at least fourteen days.
This means that we have up to two weeks to try to recover the lost commits.
Of course in practice one would hope that we would perform the recovery much earlier than that.
We already know the commit ID that the branch was pointing to.
Git has been very kind and told us it, just before it carried out the delete.
We are looking for commit 7cc32db.
If we run the command below, we will create and recover our branch in one go.
john@satsuki:~/coderepo$ git branch zaney 7cc32db
john@satsuki:~/coderepo$ git branch -v
* master 9710177 Added another file
wacky 9710177 Added another file
wonderful cfbecab Fantastic new feature
zaney 7cc32db Made another awesome change
john@satsuki:~/coderepo$
Our branch has been restored and points to the same place as it did before we deleted it.
As each commit points to its parent, we now have the complete history of zaney restored and the branch can be used as normal.
To complete this action, let us delete the wacky branch as originally intended.
john@satsuki:~/coderepo$ git branch -d wacky
Deleted branch wacky (was 9710177).
john@satsuki:~/coderepo$ git branch -v
* master 9710177 Added another file
wonderful cfbecab Fantastic new feature
zaney 7cc32db Made another awesome change
john@satsuki:~/coderepo$
As you can see, when we deleted wacky we were not warned about unmerged changes.
This is because the wacky branch is at the same point as the master branch.
|