本地文件上傳到gitee倉庫的詳細步驟
🔰 一、前期準備
- 注冊 Gitee 賬號
訪問 Gitee 官網完成注冊并登錄。
網址:https://gitee.com/
- 安裝 Git
下載 Git 官方客戶端并完成安裝。
下載網址:https://git-scm.com/downloads
- 配置 Git 全局信息(首次使用需設置)
git config --global user.name "你的用戶名"
git config --global user.email "你的郵箱"
🛠? 二、操作步驟
1. 在 Gitee 創建倉庫
-
登錄 Gitee → 點擊右上角
+
→ 選擇 新建倉庫。 -
填寫倉庫名稱(如
my-project
)、描述,選擇公開/私有。 -
關鍵選項:
- 初始化倉庫:不勾選
使用README文件初始化倉庫
(避免后續沖突)。 - 忽略文件:按需添加
.gitignore
模板(如 Java/Python)。 - 點擊 創建,生成空倉庫
- 初始化倉庫:不勾選
2. 初始化本地倉庫
# 進入項目根目錄
cd /path/to/your/project# 初始化 Git 倉庫
git init# 添加所有文件到暫存區
git add .# 提交到本地倉庫
git commit -m "Initial commit"
3. 關聯遠程倉庫
# 復制 Gitee 倉庫的 HTTPS 地址(格式:https://gitee.com/用戶名/倉庫名.git)
git remote add origin https://gitee.com/your_username/your_repo.git
4. 推送到 Gitee
# 首次推送(注意分支名)
git push -u origin master # Git < 2.28 版本
git push -u origin main # Git ≥ 2.28 版本
- 輸入 Gitee 賬號密碼 或 個人訪問令牌(若啟用兩步驗證需用令牌替代密碼)
- 注意:如果報錯類似如下錯
To https://gitee.com/miraclechq/food-app.git! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/miraclechq/food-app.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you want to integrate the remote changes, use 'git pull'
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
-
這個錯誤表明你的本地master分支落后于遠程倉庫的master分支,導致推送被拒絕。以下是解決方案:
-
同步遠程更改
git pull origin master
-
這會拉取遠程倉庫的最新更改并與本地合并
- 解決沖突后執行:
git add . git commit -m "解決沖突"
- 重新推送?:
git push origin master
-
?特殊情況處理?:
- 若遠程有初始化文件(如README)而本地沒有 ,若確定要覆蓋遠程更改(謹慎使用):
<!--先執行下面命令--> git pull origin master --allow-unrelated-histories <!--再推送--> git push -f origin master
5. 驗證結果
- 刷新 Gitee 倉庫頁面,確認文件已成功同步
?? 三、關鍵注意事項
- 分支名稱問題
Git 2.28+ 默認分支為 main
,低版本為 master
。若推送失敗,檢查遠程倉庫默認分支名并調整命令
-
認證方式選擇
-
HTTPS:需每次輸入密碼(或配置憑證緩存)。
-
SSH(推薦):通過公鑰免密推送(需提前 配置 SSH 密鑰)。
- 忽略文件配置
在項目根目錄創建
.gitignore
文件,列出需忽略的文件(如node_modules/
,.log
),避免提交無效文件。4.沖突解決
若遠程有初始化文件(如 README),需先拉取合并:
git pull origin master --allow-unrelated-histories # 強制合并不相關歷史 # 解決沖突后重新提交推送
-
? 四、常見問題解決
-
fatal: refusing to merge unrelated histories
**添加
--allow-unrelated-histories
參數合并。 -
錯誤:
remote: Incorrect username or password
-
檢查密碼或令牌是否正確
git credential-manager reject # Windows git credential-osxkeychain erase # macOS rm ~/.git-credentials # Linux
-
-
大文件上傳失敗
增大 Git 緩沖區:
git config --global http.postBuffer 524288000 # 500MB
💡 總結
完成上述步驟后,本地文件即成功同步至 Gitee。后續更新代碼只需:
git add .
git commit -m "更新描述"
git push
保持提交信息清晰,定期拉取遠程更新,可高效管理代碼版本