Shallow Git Repository

Create Shallow Repository

Just run shallow fetch command:

git fetch --depth 10

Unshallow

git fetch --unshallow

Checkout official doc for more info.

Remove Unused Remote Branch

Update Fetch Config

We can edit the git config file to determine which branches should be fetched automatically.

edit .git/config
[remote "origin"]
	url = ...
	fetch = +refs/heads/main:refs/remotes/origin/main
	fetch = +refs/heads/thesis:refs/remotes/origin/thesis

As you see, we could change the fetch config in the remote section to tell Git which branch in the remote repository should be fetched and what name should be used for the corresponding remote tracking branch in the local machine.

Note that updating fetch config and using git fetch --prune will NOT help remove the unused remote branch that is no longer in the fetch refspecs, the content below introduces how to manually remove remove-tracking branch on local machine.

Remote-Tracking Branch

First of all, we need to know the concept of remote-tracking branch.

flowchart LR
  subgraph "Remote Repo (Origin)"
    RMain[refs/heads/main]
    RThesis[refs/heads/thesis]
  end

  subgraph Local Repo
    LThesis[refs/heads/thesis]
    LMain[refs/heads/main]
    OMain[refs/remotes/origin/main]
    OThesis[refs/remotes/origin/thesis]
    HEAD[HEAD]
  end

  HEAD --> LMain
  RMain -->|git fetch| OMain
  RThesis -->|git fetch| OThesis
  OMain -->|merge/rebase/cherry-pick| LMain

As the diagram above depicts, the refs/remotes/origin/main branches are actually live in our local machine. And git allow us to somehow “sync” it with the actual content in the remote repository using the command git fetch.

Remove Branches

To remove a remote-tracking branch, we will use the git branch command.

git branch -d -r origin/thesis

This command will remove the remote-tracking branch in the local machine with the name origin/thesis.

  • -d means delete a branch with protection
  • -r means the operation should be done to the remote-tracking branch.

Links

Published by Oyasumi

Just a normal person in the world

Leave a Reply

Index