Wednesday, January 22, 2025

GIT useful

Common Git Commands:-


//screw all the local commits and set to the origin branch

How do I reset my local branch to be just like the branch on the remote repository?

git fetch origin
git reset --hard origin/master

https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head

 

git branch -vv | sls "\[.*: gone\]" | % { git branch -D ($_.Line.Trim() -split '\s+')[0] } 



🛠️ The Full PowerShell Command List

1. Update Remote List

This tells your local machine which branches have been deleted on the server.

PowerShell
git fetch --prune

2. See "Ghost" Branches (The Preview)

Run this to see which branches are marked as : gone] before you delete them.

PowerShell
git branch -vv | Select-String ": gone]"

3. Clean All Ghost Branches (Safe Delete)

This is the full "one-liner." It finds the branches marked "gone" and deletes them only if they have been merged.

PowerShell
git branch -vv | Select-String ": gone]" | ForEach-Object { git branch -d ($_.ToString().Trim().Split(" ")[0]) }

4. Force Clean (Unmerged)

Only use this if the command above fails with an error saying a branch "is not fully merged" and you are 100% sure you don't need that code.

PowerShell
git branch -vv | Select-String ": gone]" | ForEach-Object { git branch -D ($_.ToString().Trim().Split(" ")[0]) }

⚠️ Why the full command matters

If you only run git branch -vv, Git just prints a list of branches to your screen. The Select-String and ForEach-Object parts act like a "filter and destroy" sequence—without them, no cleaning actually happens.


FOr Linux

git branch -vv | Receive-String -Filter "origin/.*: gone" | ForEach-Object { git branch -D $_.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)[0] }

No comments:

Post a Comment