[git use] clear the local warehouse and remote warehouse

Series Article Directory

The first chapter uses git to clear the local warehouse and remote warehouse



foreword

Git is currently one of the most popular version control tools, which can help us better manage the code of the project. In the process of using Git, sometimes we need to clear the branches of the local warehouse and the remote warehouse, and re-upload the code or file. This article will introduce how to clear the local warehouse and remote warehouse branches (by default, the remote master branch is write-protected and cannot be overwritten and deleted).


1. Specific steps

First of all, you need to enter the local warehouse address to synchronize, so I won’t go into details about this step.

1. Delete all local branches

The following command will delete all local branches except master

git branch | grep -v 'master$' | xargs -I {
    
    } git branch -D {
    
    } && echo "Deleted local branch: {}"
  • git branch --------------------------- List all local branches.
  • grep -v 'master$' ----------------- Filter out branches whose name ends with master
  • xargs -I {} git branch -D {} -------------- Execute the git branch -D command for each branch, thereby deleting the branch
  • echo "Deleted local branch: {} -------- print the deleted branch name

2. Delete all remote branches

After executing the following command, all remote branches except the master branch will be deleted, and the name of each deleted branch will be printed.

git branch -r | grep -v 'master$' | awk -F/ '{print $2}' | xargs -I {
    
    } git push origin --delete {
    
    }
  • git branch -r -------------------------- list all remote branches
  • grep -v 'master$' -------------------- Filter out branches whose name ends with master
  • awk -F/ '{print $2}' ------------------ Extract the name of each branch
  • xargs -I {} git push origin --delete {} -------- Execute the git push origin --delete command for each branch to delete the branch

3. Delete all files in the local warehouse

git rm -r --force .

This command will completely delete all files and directories from the local repository and file system, but will not delete the .git directory itself.

4. Submit the changes to the local master branch

This command will delete all files written in history

git commit -m "Remove all files from repository"

5. Push to the remote master branch

git push origin master --force

Summarize

In this article, we covered how to empty both local and remote branches. Before performing these operations, please make sure to back up important codes or files, and read the meaning of the commands carefully to avoid misuse.

Guess you like

Origin blog.csdn.net/PellyKoo/article/details/129585102