Restoreing files from last commit by creating new branch

tarun k 635 Reputation points
2025-11-11T16:29:49.9933333+00:00

Branch was not there and it got deleted , but from some reason we are not able to restore it

but we need to restore it, I heared from last commit we can restore the files of that branch

Please suggest how we can do

Azure DevOps
{count} votes

1 answer

Sort by: Most helpful
  1. Anurag Rohikar 3,185 Reputation points Microsoft External Staff Moderator
    2025-11-19T11:58:46.09+00:00

    Hello tarun k,

    Assuming the branch has been deleted but the commits are still available in the repository history, you can restore the files from the last commit by creating a new branch pointing to that commit. Here’s how you can do it:

    1. Check Repository History for Commit
      • Navigate to your Azure DevOps repository.
      • Go to Repos → Commits.
      • Identify the last commit from the deleted branch. You can search by commit messages, author, or approximate date.
    2. Create a New Branch from the Commit
      • In the Commits view, click the commit you want to restore.
      • Click … (More Options) → New Branch.
      • Provide a name for the new branch (e.g., restored-branch) and select Create.
      • This will create a new branch pointing to the last commit of the deleted branch, effectively restoring its state.
    3. Verify Files
      • Once the branch is created, navigate to Repos → Files and confirm that all files are present as per the last commit.

    Alternative approach:

    1. Find the Commit ID: First, you'll need to identify the last commit you want to restore files from. You can do this by navigating to your repository and checking the commit history. You can use the command:
         git log
      
      This will list all your commits. Look for the commit ID (a string of letters and numbers) related to the changes you want to restore.
    2. Create a New Branch from the Last Commit: Once you have the commit ID, you can create a new branch that starts from that commit. Use the command:
         git checkout -b new-branch-name <commit-id>
      
      Replace new-branch-name with your desired name for the new branch, and <commit-id> with the actual ID.
    3. Restore the Files: Now, your new branch is at the last commit where the files were intact. You can verify the files and make any necessary changes. If you need to revert any specific files to their state at that commit, you can use:
         git checkout <commit-id> -- <path-to-file>
      
    4. Commit Your Changes: After you've restored what you need, make sure to commit your changes:
         git add .
         git commit -m "Restored files from last commit"
      
    5. Finally, push your new branch:
         git push origin new-branch-name
      

    References:

    Hope this helps. Let me know if you have any further questions. If this comment helps, an upvote is appreciated. Thank you!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.