The Issue of the Day Before

清除 git 所有遠端舊的紀錄 重新來過

-

記錄目前 git 遠端倉儲的路徑 → 刪除工作目錄中的 .git 目錄 → 重新初始化本地的 git 倉儲 → 設定遠端 git 倉儲路徑 → 將檔案加入本地倉儲 → 強迫推送到遠端倉儲

記錄設定

切換到工作目錄,並記錄下遠端倉儲的路徑。 遠端倉儲的路徑可以看目錄下的 .git/config 檔案內容或使用 git config --get remote.origin.url 命令查閱。

.git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@domain:user/repo.git   // (1)
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
  1. 記錄下遠端倉儲的路徑,後面重設時需要用到

刪除 .git 並重新推送

切換到工作目錄,且以下命令都必須在工作目錄下執行。

> rm -rf .git      // (1)
> git init         // (2)
> git remote add origin git@domain:user/repo.git // (3)
> git add --all    // (4)
> git commit -a -m "clean"          // (5)
> git push -u origin master --force // (6)
  1. 清除本地端所有紀錄

  2. 重新初始化本地 git

  3. 重新加入遠端

  4. 重新加入本地檔案

  5. 並交付所有本地檔案

  6. 強迫更新所有本地檔案到遠端

簡易 script

cleanGit.sh
#!/bin/bash
if test -z "$1" ; then
  echo "error: must has path param"
  exit 1
fi

cd $1 || (echo "Cannot change directory!" 1>&2 && exit 1)

URL=$(git config remote.origin.url)
if test -z "$URL" ; then
    echo "Not found git!" 1>&2
    exit 1
fi

echo "remote.origin.url: $URL"

echo "clean git"
rm -rf .git

echo "reset git"
git init
git remote add origin "$URL"
git add --all
git commit -a -m "new"              // (1)
git push -u origin master --force
  1. "new" 可以換成其他合適的說明

閱讀在雲端