Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the tips for Git maintenance

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about Git maintenance tips. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The maintenance of the Git warehouse is usually to reduce the size of the warehouse. If you import a repository from another version control system, you may need to remove unnecessary files after import.

The advanced techniques used in the steps and tools in this article involve destructive operations. Make sure you read and back up your repository carefully before you begin. The easiest way to create a backup is to clone your repository with the-mirror flag, and then package and compress the entire cloned file. With this backup, if you accidentally damage key elements of your warehouse during maintenance, you can restore it through the backed-up warehouse.

Keep in mind that warehouse maintenance can be devastating for warehouse users. It would be a good idea to communicate with your team or followers in the warehouse. Make sure everyone has checked their code and agreed to stop development during warehouse maintenance.

Understand deleting files from the history of Git

Recall that the clone repository clones the entire history-- including all versions of each source code file. If a user submits a large file, such as a JAR, that file will be included in each subsequent clone. Even if the user eventually deletes the file in a later submission, the file still exists in the history of the repository. To completely delete this file from your warehouse, you must:

Delete the file from your project's current file tree; delete the file from the warehouse history-rewrite the Git history, delete the file from all submissions containing the file; delete all reflog history that points to the old submission history; reorganize the warehouse and use git gc to garbage collect data that is not currently in use.

Git's "gc" (garbage collection) will delete all actual useless or referenced data in the warehouse through any of your branches or tags. To make it work, we need to rewrite all Git repository history that contains unwanted files, and the repository will no longer reference it-- git gc will discard all useless data.

Rewriting repository history is tricky, because each commit depends on its parent commit, so any small change will change its ID for each subsequent commit. There are two automated tools that can do this:

BFG Repo Cleaner is fast, simple, and easy to use, and requires a Java 6 or later runtime environment. Git filter-branch is powerful, troublesome to configure, slow to use when it is larger than a warehouse, and is part of the core Git suite.

Remember that when you rewrite history, whether you use BFG or filter-branch, you need to delete reflog entries that point to the old history, and finally run the garbage collector to delete the old data.

Rewrite history using BFG

BFG is specially designed to delete unwanted data such as large files or passwords from the Git repository, so it has a simple flag to delete those large history files (not in the current submission):-strip-blobs-bigger-than

$java-jar bfg.jar-- strip-blobs-than 100m

Any files larger than 100MB (not included in your most recent submission-because BFG protects your latest submission by default) will be deleted from your Git repository history. If you want to specify a specific document by name, you can also do this:

$java-jar bfg.jar-- delete-files * .mp4

BFG is 10-1000 times faster than git filter-branch and is usually easier to use-- check out the complete instructions and examples for more details.

Use git filter-branch to rewrite history

The filter-branch command can rewrite the history of the Git repository, just like BFG, but the process is slower and more manual. If you don't know where these large files are, the first step is to find them:

Manually check the large files in your Git warehouse

Antony Stubbs has written a BASH script that does this very well. This script can check the contents of your package files and list large files. Before you start deleting files, do the following to get and install this script:

Download the script to your local system. 2. Put it in an easy-to-find location with access to your Git repository. 3. Make the script executable:

$chmod 777 git_find_big.sh

4. Clone the warehouse to your local system. 5. Change the current directory to your warehouse root directory. 6. Run the Git garbage collector manually:

Git gc-auto

7. Find out the size of the .git folder

$du-hs .git / objects45M .git / objects

Pay attention to the file size for later reference. Run the git_find_big.sh script to list the large files in your warehouse.

$git_find_big.shAll sizes are in kB's. The pack column is the size of the object, compressed, inside the pack file.size pack SHA location592 580 e3117f48bc305dd1f5ae0df3419a0ce2d9617336 media/img/emojis.jar550 169 b594a7f59ba7ba9daebb20447a87ea4357874f43 media/js/aui/aui-dependencies.jar518 514 22f7f9a84905aaec019dae9ea1279a9450277130 media/images/screenshots/issue-tracker-wiki.jar337 92 1fd8ac97c9fecf74ba6246eacef8288e89b4bff5 media/js/lib/bundle.js240 239 e0c26d9959bd583e5ef32b6206fc8abe5fea8624 media/img/featuretour/heroshot.png

Large files are JAR files, and the size column of the package is the most relevant. Aui-dependencies.jar is compressed to 169kb, but emojis.jar is compressed to 500kb only. Emojis.jar is an object to be deleted.

Run filter-branch

You can pass a filter to this command to rewrite the Git index. For example, a filter can delete each retrieved submission. This usage is as follows:

Git filter-branch-- index-filter 'git rm-- cached-- ignore-unmatch _ pathname_' commitHASH

The-index-filter option modifies the index of the repository, and the-cached option deletes files from the index instead of disk. This is faster because you don't need to check each revision before running the filter. The ignore-unmatch option in git rm prevents the command from failing when trying to remove the file pathname that does not exist. By specifying a commit HASH value, you can remove the pathname from each submission that starts with that HASH value. To remove it from the beginning, you can omit this parameter or specify HEAD.

If your large files are in different branches, you will need to delete each file by name. If the large files are in a separate branch, you can delete the branch itself directly.

Option 1: delete files by file name

Use the following steps to delete a large file: 1. Use the following command to delete the first large file you find:

Git filter-branch-index-filter 'git rm-cached-ignore-unmatch filename' HEAD

Repeat step 1 to find each of the remaining large files. 3. Update the reference in your warehouse. Filter-branch will create a backup of your previous reference under refs/original/. Once you are sure that you have deleted the correct file, you can run the following command to delete the backup file and have the garbage collector recycle large objects:

Git filter-branch-index-filter 'git rm-cached-ignore-unmatch filename' HEAD

Option 2: delete the branch directly

If all your large files are on a separate branch, you can delete the branch directly. Deleting this branch automatically deletes all references.

1. Delete branches.

$git branch-D PROJ567bugfix

2. Remove all reflog references from subsequent branches.

Garbage collection of unused data

1. Delete all reflog references from now to later (unless you explicitly operate on only one branch).

$git reflog expire-expire=now-all

2. Repackage the warehouse by running the garbage collector and deleting old objects.

$git gc-prune=now

3. Push all your changes back to the warehouse.

$git push-all-force

4. Make sure all your tags are up-to-date:

$git push-- tags-- force Thank you for reading! This is the end of this article on "what are the Git maintenance tips?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report