cancel
Showing results for 
Search instead for 
Did you mean: 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 

Hard reset programatically

camilo_s
New Contributor II

Is it possible to trigger a git reset --hard programatically?

I'm running a platform service where, as part of CI/CD, repos get deployed into the Databricks workspace. Normally, our developers work with upstream repos both from their local IDEs and from the Databricks Code Editor. It sometimes happens though that, due to their mixed (and sometimes inadequate) use of IDE and Databricks GUI, the repo deployment process fails during CICD (error code GIT_CONFLICT).

In that case, we'd like to catch this error and do a hard reset to overwrite the Databricks Repo with the status of the Upstream repo (which should be the single source of truth anyhow).

Is it possible to do this programmatically?

2 REPLIES 2

Kaniz
Community Manager
Community Manager

Hi @camilo_s, When dealing with Git repositories programmatically, you can indeed trigger a git reset --hard to revert to a specific commit.

Let’s break down the process:

  1. Understanding git reset --hard:

    • The git reset --hard command discards all changes in your working directory and staging area, reverting your repository to a specific commit.
    • It’s a powerful command, so use it with caution, especially in production environments.
  2. Programmatic Approach:

    • To achieve your goal of overwriting the Databricks Repo with the status of the Upstream repo, follow these steps:
      1. Identify the commit hash of the desired state in the Upstream repo.
      2. Programmatically execute the following commands:
        • First, reset your local branch to the desired commit:
          git reset --hard <full_hash_of_commit_to_reset_to>
          
        • Then, force-push this state to the remote repository:
          git push --force
          
      3. This will update the branch history on both your local machine and the remote server.
  3. Considerations:

    • Be aware that force-pushing can disrupt other collaborators who have already synced with the branch containing the bad commits. They’ll need to rebase or reset their local branches accordingly.
    • If you can’t force-push due to restrictions (e.g., non-fast-forward pushes are denied), consider creating a new commit that reverts the changes introduced by the bad commits. This maintains history while achieving the desired state1.

Remember to adapt these steps to your specific CI/CD pipeline and ensure proper testing before applying them in production. Git’s flexibility allows you to programmatically manage your repository effectively! 🚀234.

 

camilo_s
New Contributor II

Hi @Kaniz, thanks for your reply.

My question was about programmatically doing what Databricks does on the Databricks Repository when I click on the Reset (hard) option in the UI; apologies for not emphasizing this clearly enough.

I tracked the network activity in the browser when clicking on that button and noticed it calls an endpoint https://<WORKSPACE_URL>/graphql/projectGitReset_ProjectGitModal which unfortunately doesn't seem to be public (it's not documented anywhere).

I realize git reset --hard can be a destructive operation, but in our use-case remote repositories should be the single source of truth at any time, which is why it'd make sense to be able to programmatically hard reset a repo to a given branch when performing a repo update. Without this, an unstaged change in a Git repository will block Update a repo endpoint operations, which is what we'd like to avoid.

Screenshot 2024-05-06 at 15.04.46.png

 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!