---
type: collection
section: snippets
generated_at: 2026-04-11T07:11:37+02:00
total_entries: 5
tag_filter: Git
---

# Snippets

## Delete Local Git Branches Whose Remote Was Deleted

---
type: snippet
id: le540vfe3hsou4yo
title: >
  Delete Local Git Branches Whose Remote
  Was Deleted
url: >
  https://www.jakubpelak.com/snippets/git-branches-remote-deleted
section: snippets
tags:
  - Git
  - Cleanup
  - Automation
  - CLI
published_at: 2026-01-31
---

When working with feature branches, it’s easy for your local repository to accumulate branches that were already deleted on the remote. This snippet safely removes all local branches whose remote tracking branch no longer exists.

```sh
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs -r git branch -d
````

If you want to remove them regardless of merge status, replace `-d` with `-D`.

## Find Dirty Git Repositories

---
type: snippet
id: yqeihixirgfk24gh
title: Find Dirty Git Repositories
url: >
  https://www.jakubpelak.com/snippets/find-dirty-git-repositories
section: snippets
tags:
  - Git
  - Automation
  - CLI
  - Script
published_at: 2025-08-08
---

A tiny shell script that recursively scans a directory for Git repositories with uncommitted changes, nicknamed `dirgit`—a play on words combining "directory", "dirty", and "git".

```bash
#!/bin/bash

# Use current directory or first argument
ROOT="${1:-$(pwd)}"

# Find all Git repositories and check for dirty state
find "$ROOT" -type d -name ".git" 2>/dev/null | while read -r gitdir; do
    repo="$(dirname "$gitdir")"
    cd "$repo" || continue
    if [[ -n $(git status --porcelain) ]]; then
        echo "$repo"
    fi
done
````

Save it as `dirgit` (or any name you prefer), make it executable (`chmod +x dirgit`), and place it in `/usr/local/bin` or `~/bin` to use it globally.

### Usage

```bash
dirgit         # scan current directory
dirgit ~/code  # scan a specific directory
```

## How to Reset a Single File in Git

---
type: snippet
id: g4togielbs9vkxus
title: How to Reset a Single File in Git
url: >
  https://www.jakubpelak.com/snippets/how-to-reset-single-file-in-git
section: snippets
tags:
  - Git
  - CLI
published_at: 2025-07-15
---

If you've made changes to a file and want to discard them, you can use `git checkout` to restore it to the version from the last commit. This is useful when you want to undo changes that haven't been staged yet.

```sh
git checkout -- <file>
```

To restore a file from a different branch, you can use:

```sh
git checkout <branch> -- <file>
```

## Delete All Local Branches Except main

---
type: snippet
id: RYVuj6j1rJ49DePN
title: Delete All Local Branches Except main
url: >
  https://www.jakubpelak.com/snippets/delete-all-local-branches-except-main
section: snippets
tags:
  - Git
  - Automation
  - CLI
published_at: 2025-06-26
---

A handy one-liner to clean up all your local Git branches except `main`. It uses `-D` to **force-delete** each branch, even if it hasn’t been merged—so be careful. If you want a safer version, replace `-D` with `-d` to only delete branches that have been merged.

```sh
git branch | grep -v "main" | xargs git branch -D
```

Credit: (link: https://coderwall.com/p/x3jmig/remove-all-your-local-git-branches-but-keep-master)

## Switch remote HTTPS to SSH in Git

---
type: snippet
id: f4NDATop1pkniG9P
title: Switch remote HTTPS to SSH in Git
url: >
  https://www.jakubpelak.com/snippets/switch-remote-https-to-ssh-in-git
section: snippets
tags:
  - Git
  - SSH
  - HTTPS
  - CLI
published_at: 2025-05-25
---

Quick snippet to switch a Git remote URL from HTTPS to SSH for easier authentication with SSH keys.

```sh
git remote -v
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
```

Credit: (link: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories)

