[ Back to main ]
[ Back to dev page ]


Git Recipes                                                     


Rename Branch Locally and Remotely                              

Example renames `master` branch to `main` for codeberg.         

1. `git branch -m master main`                                  
2. `git push origin -u main`                                    
3. Ensure origin's default branch is not `master`.              
4. `git remote set-head origin main`                            
5. `git branch --delete --remotes origin/master`                
6. `git branch -r`                                              

Command for remote branch deletion:                             
`git branch origin -d master`                                   


Extract a Directory from Repo                                   

Clone the original repository.                                  
                                                                
1. `git clone https://.../REPO.git`                             
2. `cd REPO`                                                    
                                                                
Extract the directory and put it in a new branch.               
                                                                
3. `git subtree split --prefix=path/to/dir/ --branch=newbranch` 
                                                                
Create a new git repository from the extracted directory.       
                                                                
4. `cd ..`                                                      
5. `mkdir NEWREPO`                                              
6. `cd NEWREPO`                                                 
7. `git init`                                                   
8. `git pull ../REPO newbranch`                                 


Delete a Directory in a Repo                                    

Clone the original repository.                                  
                                                                
1. `git clone https://.../REPO.git`                             
2. `cd REPO`                                                    

Make sure you can run the `git-filter-repo` Python script.      

3. `git-filter-repo --path path/to/dir/ --invert-paths`         

Enforce the deletion remotely.                                  

4. git remote add origin https://.../REPO.git                   
5. git push origin master --force