最近有和別人進行unity項目協作的需求,需要把自己的本地代碼上傳到github已有的一個倉庫里。記錄一下上傳過程,防止后續還需要用。
文章目錄
- 一、把自己的本地代碼上傳到github已有的一個倉庫中
- 二、常用功能
一、把自己的本地代碼上傳到github已有的一個倉庫中
首先可以借鑒這個博主的做法,生成ssh密鑰、添加ssh key到github項目。https://blog.csdn.net/Natsuago/article/details/145646982
這樣做的目的是為了安全地驗證你的身份,讓你可以在不輸入用戶名和密碼的情況下,通過加密通道與 GitHub 倉庫進行交互。
然后,按照如下步驟操作:
(1)克隆github項目到本地
git clone git@github.com:用戶名/項目名
(2)把本地 Unity 項目的內容復制到這個文件夾里
把已有的 Unity 項目的 Assets/, Packages/, ProjectSettings/ 等拷貝進克隆下來的文件夾中。
(3)進入項目文件夾,并添加變動
cd 項目文件夾名稱
git add .
git commit -m "Add local Unity project code"
(4)推送到github
git push origin main # 如果主分支是 main
# 或者
git push origin master # 如果主分支是 master
注意備份!這個操作可能會覆蓋遠程已有的內容。
二、常用功能
1. 更新遠程倉庫URL(從HTTPS改成SSH)
檢查當前遠程配置:
git remote -v # 查看現有遠程倉庫
若顯示 HTTPS 格式(如 https://github.com/…),更新為 SSH:
git remote set-url origin git@github.com:用戶名/倉庫名稱
2.查看當前所在的分支是什么
git branch
3.如何放棄當前.git歷史,重新初始化一個干凈的git倉庫上傳
# 1. 先備份當前代碼(可選)
cd ..
cp -r Interactive-Physics-Based-Gaussian-Splatting-Real-Time-Visualization-Editing backup_project# 2. 刪除 Git 跟蹤信息(包括所有歷史記錄和大文件)
cd Interactive-Physics-Based-Gaussian-Splatting-Real-Time-Visualization-Editing
rm -rf .git# 3. 重新初始化倉庫
git init
git remote add origin https://github.com/yourname/yourrepo.git # ← 換成你的倉庫地址
echo "cuda_11.8.0_520.61.05_linux.run" >> .gitignore
git add .
git commit -m "Initial clean commit"
git push -f origin master # 或 main,看你用哪個分支
如果出現下面這個情況:
(base) 用戶名@dbcloud:~/projects/github/Interactive-Physics-Based-Gaussian-Splatting-Real-Time-Visualization-Editing$ git add .
warning: adding embedded git repository: gaussian-splatting/SIBR_viewers
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> gaussian-splatting/SIBR_viewers
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached gaussian-splatting/SIBR_viewers
hint:
hint: See "git help submodule" for more information.
warning: adding embedded git repository: gaussian-splatting/submodules/diff-gaussian-rasterization
warning: adding embedded git repository: gaussian-splatting/submodules/fused-ssim
warning: adding embedded git repository: gaussian-splatting/submodules/simple-knn
說明你在一個 Git 倉庫(我們稱為 父倉庫)中添加了 其他 Git 倉庫(子倉庫),Git 把這些子倉庫當成是 嵌套倉庫 或 子模塊,而不是普通目錄。
如果你是 有意 把這些作為子模塊(submodule),推薦使用:
git submodule add <URL> gaussian-splatting/SIBR_viewers
否則,推薦 不要直接把它們 add 進來,可以用以下命令移除:
git rm --cached gaussian-splatting/SIBR_viewers
git rm --cached gaussian-splatting/submodules/diff-gaussian-rasterization
git rm --cached gaussian-splatting/submodules/fused-ssim
git rm --cached gaussian-splatting/submodules/simple-knn
之后可以在 .gitignore 文件中添加它們:
echo "gaussian-splatting/SIBR_viewers/" >> .gitignore
echo "gaussian-splatting/submodules/" >> .gitignore
如果你忽略這些 warning 把它們提交了,其他人克隆你的倉庫時,這些子模塊內容不會被包含進來,他們會看到的是一個空目錄,除非手動初始化子模塊,這會帶來很多困擾。