將本地專案推送到遠端git新倉庫

NO IMAGE

使用命令列的方式,首先在本地新建立一個git倉庫(如果當前專案還沒有倉庫的話):

在git bash下

1. mkdir project
2. cd project
3. git init (初始化倉庫)
4.複製專案檔案到剛建立的新倉庫目錄

準備工作做好後,接著就是把本地倉庫專案推送到遠端倉庫:

1.git add . (將當前目錄下的檔案加入到倉庫)
2.git commit -m "the first commit project" (第一次提交專案檔案)
3.git remote add origin https://git.coding.net/solent/xingzuo.git
4.git push -u origin master (推送遠端倉庫)

執行第四步的時候,報錯如下:

To https://git.coding.net/solent/xingzuo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://git.coding.net/solent/xingzuo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

報錯因為遠端倉庫與本地倉庫檔案不一致,使用git pull拉下遠端程式碼到本地

5.git pull

接著又報如下錯誤:

warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://git.coding.net/solent/xingzuo
* [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master

報錯原因是沒有指定本地master和遠端origin/master的連線,執行git branch –set-upstream master origin/master,設定連結

6.git branch --set-upstream master origin/master
The --set-upstream flag is deprecated and will be removed. Consider using --trac k or --set-upstream-to

Branch master set up to track remote branch master from origin.

然後再執行下git pull

7.git pull
fatal: refusing to merge unrelated histories

這回報更嚴重的致命錯誤,根據提示然後如下執行:

8.git pull origin master --allow-unrelated-histories 
From https://git.coding.net/solent/xingzuo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | 46                
LICENSE | 191     
README.md | 1  
3 files changed, 238 insertions( )
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md

最後再執行推送到遠端倉庫的命令,就不會再報錯了

9.git push -u origin master 
Counting objects: 2279, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2227/2227), done.
Writing objects: 100% (2279/2279), 46.29 MiB | 543.00 KiB/s, done.
Total 2279 (delta 349), reused 0 (delta 0)
To https://git.coding.net/solent/xingzuo.git
29b55c0..7c92f17 master -> master
Branch master set up to track remote branch master from origin.