Cyberithub

How to Prevent Accidental Commit to Master and Dev Branch in GIT

Advertisements

In this article, we will see the steps to prevent accidental commit to master and dev branch in GIT Repository. If you are a DevOps Professional or a developer then you might be aware that Dev branch is usually used for deployment in Development environment and Master Branch is used for deployment in Production environment. In a GIT branching strategy, access to Dev and Master branch is often prohibited without proper approval. Developers usually check in their code into local feature branch and only after Team Lead approval they are allowed to merge their code into Dev or Master branch depending on the deployment strategy.

It is often observed that sometimes by mistake Developers check in their code in Dev or Master Branch which in turn affect the upcoming or stable code releases. This in turn result into a big issue and hence to avoid such cases it is always a good Idea to put a block on Dev and Master Branch. More on Git Branching and Merging.

GIT provides a feature to put a commit block in required branches by just creating a simple hook. Hook is nothing but just a simple script to perform a specific task. You can also prevent a push, update, rebase to a branch using hook. We will see the steps for that in future articles. Here we are only going to see the steps to prevent accidental commit to Master and Dev Branch.

How to Prevent Accidental Commit to Master and Dev Branch in GIT

How to Prevent Accidental Commit to Master and Dev Branch in GIT

Also Read: Deploy Application War File to Tomcat Using Jenkins in 9 Simple Steps

Step 1: Prerequisites 

a) You should have access to a Git Repository.

b) You should have Dev and Master Branch available in your Repo.

c) You should have git client tool installed in your Server.

Step 2: Go to GIT Repository

First you need to go to GIT Repo directory where you need to create a hook. In our case it is /home/cyberithub/sample-project-repo2/.git/hooks directory. This is the usual GIT Standard path for creating all the hooks.

cyberithub@cloudshell:~ (sample-project)$ cd /home/cyberithub/sample-project-repo2/.git/hooks/

NOTE:

Please note that GIT does not invoke pre-commit hook for any merge command, it only invokes for regular commit command issued by git user.

Step 3: Create a Hook

Here you need to create a small bash script to prevent the commit into Dev and Master Branch. First we need to get the branch name where commit is getting performed. This can be done by using git rev-parse --abbrev-ref HEAD command and the resultant can be stored in a variable called branch in this case. Then we need to use the if statement to match the branch given for commit. As of now we are putting a block on dev and master branch but if you want you can block any other branch as well using below method.

cyberithub@cloudshell:~/sample-project-repo2/.git/hooks (sample-project)$ vi pre-commit
#!/bin/sh

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ "$branch" = "dev" ]; then
  echo "Dev Branch commit is blocked"
  exit 1
fi

if [ "$branch" = "master" ]; then
  echo "Master Branch commit is blocked"
  exit 1
fi

NOTE:

Please note that this is not a protection on your remote Dev and Master Branch. It is only for local Dev and master branch.

Step 4: Provide Execute Permission

Now provide execute permission to pre-commit script using chmod +x pre-commit command as shown below. This will allow the execution of pre-commit script.

cyberithub@cloudshell:~/sample-project-repo2/.git/hooks (sample-project)$ chmod +x pre-commit

Step 5: Verify Commit Access

Now if you try to commit changes in your Dev Branch then you will see below blocked message. This confirms that accidental commit to Dev branch is now blocked.

cyberithub@cloudshell:~/sample-project-repo2 (sample-project)$ git add .
cyberithub@cloudshell:~/sample-project-repo2 (sample-project)$ git commit -m "Dev Commit"
Dev Branch commit is blocked

Similarly if you try to check to commit in Master branch then you will see below blocked message. This confirms that accidental commit to Master branch is now blocked.

cyberithub@cloudshell:~/sample-project-repo2 (sample-project)$ git add .
cyberithub@cloudshell:~/sample-project-repo2 (sample-project)$ git commit -m "Master Branch Commit"
Master Branch commit is blocked

Leave a Comment