標籤: remote repo

  • 將現成的 git repo 添加現成的專案資料夾

    將現成的 git repo 添加現成的專案資料夾

    這是一個 git 小技巧,其實滿常用的,但是每次要用的時候都要查一次有點小麻煩,紀錄一下。

    主要的應用場景是在我們在伺服器/本地已經有一個現成的資料夾,要把他添加至一個遠端新創的 git repo中,但是因為環境設定原因,我不想要移動資料夾的路徑。

    要注意的是:

    1. 如果是用 Github 創建,預設都不要選擇 .gitignore 等檔案,會出現一個教學可添加,照著作即可。讚!
    2. 確保目前伺服器/本地可以 push/clone/pull git 伺服器(廢話),推薦使用 ssh 連線,參考這裡
    3. 這是使用的流程以空的 git repo 為主,雖然不影響,但是還是建議拿空的 repo 比較不容易有衝突。
    4. 資料夾中會生成一的 .git 資料夾,有些資料夾是從別人那拉下來的。請先把這個資料夾刪除。也要記得先檢查 .gitignore 是不是你要的。

    流程上:

    1. 記得先安裝好 git 套件。進入到目標資料夾,確保沒有存在其他 .git/ ,以及 .gitignore 設定正確。
    2. 建立一個新的 git
      git init

    3. 添加檔案至 git,並且 commit
      git add .; git commit -m "initial commit"
    4. 注意遠端的 branch 是什麼,在 Github 上通常是 master
      git branch -M master
    5. 添加到遠端 git。 <remote-repo-url> 要替換成 git repo
      git remote add origin <remote-repo-url>

    6. 檢查網址
      git remote -v

    7. git push 資料到遠端 repo
      git push -f origin master

    提醒一下,現在有些 Git 平台使用 main 作為預設的主要 branch。

    以上。

    參考: