Recovering Deleted Branches/Commits In Git-TFS

May 29, 2017

no comments

In Git TFS there is not “git gc” implemented in the server side. At first sight this can be a big problem because once you push a change to the database it will remain in the database forever (yes, forever). The only way to remove it is to delete the whole repository. Nevertheless this fact allows us to recover at any moment any thing that we have deleted. Let’s see how achieve this…

 

1) Get the Internal Repository ID:

SELECT Name, InternalRepositoryId
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitRepository]
ORDER BY InternalRepositoryId

RecoverDeletedBranchOrCommitInGitTFS_1.png

 

2) Search the deleted branch in the reflog using the following query:

SELECT DISTINCT Name
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitRefLog]
WHERE InternalRepositoryId = 25

RecoverDeletedBranchOrCommitInGitTFS_2.png

 

3) Then you can search for all the commits related to the specified branch using:

SELECT *
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitRefLog]
WHERE Name = 'refs/heads/results' AND InternalRepositoryId = 25

RecoverDeletedBranchOrCommitInGitTFS_3.png

 

4) You can access to any deleted commit using the “ObjectId” (SHA1) by removing the “0x” at the beginning:

     •For Example: 0xCD23C7685DABEDAAF9197E69C6FDFDF12E72DD9C
       RecoverDeletedBranchOrCommitInGitTFS_4.png

 

5) Find the commit where the branch was removed using:

SELECT *
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitRefLog]
WHERE Name = 'refs/heads/results' AND InternalRepositoryId = 25 AND NewObjectId =0x0000000000000000000000000000000000000000

RecoverDeletedBranchOrCommitInGitTFS_5.png

 

6) Now you are able to recover the branch by:

     • Checkout the last commit of the deleted branch (as detached head)
       RecoverDeletedBranchOrCommitInGitTFS_6.png

 

     • Creating a new branch from the commit checked out
       RecoverDeletedBranchOrCommitInGitTFS_7.png

 

7) That’s it, your branch was recovered

RecoverDeletedBranchOrCommitInGitTFS_8.png

 

Note:
Do you want to know who delete the branch and kill him?
Follow the steps below to find him…

 

1) Use the query of the step (3) to retrieve the Push Id for the delete commit (newObjectId = 0x000…)

SELECT *
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitRefLog]
WHERE Name = 'refs/heads/results' AND InternalRepositoryId = 25

RecoverDeletedBranchOrCommitInGitTFS_9.png

 

2) Use the “PushId” to retrieve the “PusherId” of the user that perform the commit using the following query:

SELECT *
FROM [Tfs_DefaultCollection].[dbo].[tbl_GitPush]
WHERE PushId = 4758

RecoverDeletedBranchOrCommitInGitTFS_10.png

 

3) And finally get the user details using the following query

SELECT TOP 1000 *
FROM [Tfs_Configuration].[dbo].[tbl_Identity]
WHERE Id like 'B9050C32-6258-4DCA-A6FD07820EB0'

RecoverDeletedBranchOrCommitInGitTFS_11.png

 

Add comment
facebook linkedin twitter email

Leave a Reply