準備將本地代碼上傳到gitlab上,使用svn或者idea上傳有錯誤時,直接terminal。
首先進入本地項目目錄,e.g. 我要將icon文件夾下的代碼上傳到gitlab,則
cd workspace-sts/icon/
需要創建README.md文件
echo "# 上傳代碼 測試環境使用" >> README.md
執行git命令,在當前目錄下創建.git文件夾
git init
返回
Initialized empty Git repository in /Users/stefan/Documents/workspace-sts/icon/.git/
使用ls -a
可以查看新建的.git
文件
將項目中的所有文件添加到本地代碼倉庫中
git add .
此命令會把當前路徑下的全部文件,添加到待上傳的文件列表中。
如果要添加某個特定的文件,只需把.
換成特定的文件名即可。
將add的文件commit到倉庫,
git commit -m "註釋語句" # 提交代碼的註釋
之後就是代碼創建上傳路徑。
去gitlab上創建自己的項目new project clone下來你的路徑。
將本地的倉庫關聯到gitlab上
git remote add origin http://gitlab.com/icon.git
上傳代碼到gitlab遠程倉庫
git push -u origin master
輸入Username和Password,即輸入gitlab的賬號和密碼就行了。如果沒有異常,等待執行完,就上傳成功了!
git遠程地址配錯了,再次配置提示錯誤:遠程 origin 已經存在。
此時只需要將遠程配置刪除,重新添加即可;
git remote rm origin
git remote add origin http://gitlab.com/icon.git
git push -u origin master
再次提交文件即可!!!
2019-12-23 修改 上傳時報錯信息error: failed to push some refs to
error: failed to push some refs to ‘http://git/work/document-backup.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.
解決辦法:
1、在使用git 對源代碼(文件)進行push時可能會出錯
2、出現錯誤的主要原因是git中的README.md文件不在本地代碼目錄中
3、可以通過如下命令進行代碼合併【注:pull=fetch+merge
]
git pull --rebase origin master
4、執行上面代碼後可以看到本地代碼庫中多了README.md文件
5、此時再執行語句 git push 即可完成代碼上傳到git
歡迎在點贊和評論區留言,期待與大家一起進步!