Mr.P
Mr.P

Reputation: 91

Prevent users from pushing bloated git commits to the central repository

I am managing a code project. Most of the developers of the code are graduate students, many of whom are new to Git. On multiple occasions, students have accidentally added large files (such as simulation results) to the repository and pushed to GitHub, causing an increase in the size of the repo. Obviously the long-term solution is better education on how to use .gitignore files and good Git practice; however, a foolproof system is still clearly necessary.

So my question is this: is there a way to do a quality check (or at least put a size restriction) on all new commits before they are added to the remote repository?

(Note: I already have protected branches and require the students to submit pull requests to merge. This prevents students from breaking the code, but it does not prevent large files from being added.)

Upvotes: 2

Views: 342

Answers (1)

Marina Liu
Marina Liu

Reputation: 38116

Options available for this situation as below:

1. Set branch policy to protect the main branch

As the action you already take, you can use pull reuqest to protect the main branch. And you can also prevent large files adding into commit histories. Detail steps to prevent the large files being added in commit history as below:

  • Assume there has a Pull Request to merge devlop branach into master branch and large files were committed in develop branch.
  • View the pull request to delete the large files by committing in develop branch or by adding .gitignore in master branch to ignore the files need to be merged from develop branch.
  • Merge the pull request with squash strategy so that only one commit will be rebased on master branch without the large files.

    enter image description here

  • Delete develop branch after merging.

Then the large files won’t be added in your git repo.

2. Use client side hook

Since server-side hooks are not available (as max630 mentions), you can use client-side hooks to check the commit or push.

Such as you can use pre-push hook to check the size or files to be psuhed.

The drawback is when a new local repo is created, the git hook does not configured by default.

3. Rewrite commit histories to remove the large files

You can rewrite the github repo’s commit history by git filter-branche command for a period of time.

Such as remove test.zip file from all the commit histroy by:

git filter-branch --index-filter 'git rm --ignore-unmatch --cached -r test.zip --prune-empty -f -- --all

Upvotes: 2

Related Questions