ECWU Homepage

To Dark Mode
Featured Images
Photo by Yancy Min on Unsplash

Cleaning Up Git Repo For My Blog

Zhenghao Wu

Status: Finished Confidence: likely Importance: 7

Post Details

This post is part 3 of 5 in the Website Note series.

Goto: First Post | Prev: (Switched!) Switching to a new theme ... I guess | Next:自建 GitLab 踩坑实录

Table of Contents

What I have done

When creating the theme announced in the post Switching to a new theme … I guess. I set up a new branch in the original source repository and uses Cloudflare pages to build the previews for the site. This can make sure all the original Github actions for the main site working normally.

Since the new branch new-theme was created using attribute --orphan, it will be impossible to do pull requests to the original default branch master. So I did a workaround.

  1. Change branch name from new-theme to main locally
  2. Push the new branch to the remote
  3. Change the default to main via GitHub Web UI
  4. Rename master branch to legacy via GitHub Web UI
  5. Remove new-theme branch via GitHub Web UI

(Noted that I probably did something wrong in step 2 with the following commands)

# Commands in Step 2
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
git remote prune origin
git push -u

Everything seems to work after all these steps. And the site and GitHub actions are working perfectly.

The problem surfaced

The theme is integrated into the repository using git submodule, but every time I do some changes to the theme, I have to commit twice: for both the theme and the blog source. I found a bash script from GitHub Gist that can commit both the parent repo and the submodule changes.

I download the script, and it seems can be run without a problem and successfully push the changes in the submodule. But when the script is trying to push the parent repo, the script prompted a message, and the push action is not conducted.

Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.

I searched for the web and run command git remote show origin

warning: more than one branch.main.remote
* remote origin
  Fetch URL: [email protected]:ecwu/ecwu.github.io.source.git
  Push  URL: [email protected]:ecwu/ecwu.github.io.source.git
  HEAD branch: main
  Remote branches:
    legacy                        tracked
    main                          tracked
    refs/remotes/origin/master    stale (use 'git remote prune' to remove)
    refs/remotes/origin/new-theme stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    legacy merges with remote master
    main   merges with remote master
              and with remote new-theme
  Local ref configured for 'git push':
    HEAD pushes to main (up to date)

The messages show that there are more than one branch.main.remote, and two local branches legacy and main are set to merge with master and new-theme branches which are no longer exist.

So I believe the solution is to re-config the default merge branch for the local branches and remove the stale remote branches.

Re-config the branch

The file that stores the configuration is located in .git/config, after opening the file, I can spot that there are two [branch "main"], and its merge field is filled incorrectly. I remove one extra one and change the field to ref/heads/main.

# Final result in .git/config
[branch "main"]
    remote = origin
    merge = refs/heads/main
[branch "legacy"]
    remote = origin
    merge = refs/heads/main

Then, I run the command git fetch origin --prune, it removes those outdated branches.

$ git fetch origin --prune
From github.com:ecwu/ecwu.github.io.source
 - [deleted]         (none)     -> origin/master
   (refs/remotes/origin/HEAD has become dangling)
 - [deleted]         (none)     -> origin/new-theme

Now, my remote seems normal, and rebase pull is working fine!

* remote origin
  Fetch URL: [email protected]:ecwu/ecwu.github.io.source.git
  Push  URL: [email protected]:ecwu/ecwu.github.io.source.git
  HEAD branch: main
  Remote branches:
    legacy tracked
    main   tracked
  Local branches configured for 'git pull':
    legacy merges with remote main
    main   merges with remote main
  Local ref configured for 'git push':
    HEAD pushes to main (up to date)

Extra words

When I encounter the problem, I was also confused by the multiply remote branches (refs/remotes/origin/master, refs/heads/master, …) that were seen to point to the same branch. If this also confuse you, you can check the post What are the differences between git remote prune, git prune, git fetch –prune, etc on StackOverflow.

Article Card

For "Cleaning Up Git Repo For My Blog"

Author Zhenghao Wu
Publish & Update Date 2022-01-10
Tags github git repository git branch git rebase
Extra Materials
  • SO: How does origin/HEAD get set?
  • SO: What are the differences between git remote prune, git prune, git fetch --prune, etc
  • SO: What is a "stale" git branch?

Related Posts