学习Flask过程中,在Github创建新版本库页面创建了新的版本库,然后,在本地:
$git init
Initialized empty Git repository in D:/watchlist/.git/
(venv) PS D:\watchlist> git remote add origin git@github.com:myusernameGithub/watchlist.git
(venv) PS D:\watchlist> git add .
$git commit -m "Add minimal home page"
以上一切正常
在用“push -u origin master"到远程时,出现错误:Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing.
git push -u origin master
To github.com:myusernameGithub/watchlist.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:myusernameGithub/watchlist.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.
经搜索,有两种解决办法:
- 1.使用强制push的方法:
$ git push -u origin master -f
这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。
- 2.push前先将远程repository修改pull下来
$ git pull origin master
测试第二种办法:
$git pull
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:myusernameGithub/watchlist
* [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
看似成功。好,用git status看状态,正常,再push,又出错。
$git status
On branch master
nothing to commit, working tree clean
$ git push -u origin master
To github.com:myusernameGithub/watchlist.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:myusernameGithub/watchlist.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.
再查询解决办法,应该是通过github网页创建repository时,创建了一个readme.txt,而这个txt没有同步到本地。
解决办法:pull的时候带参数 --allow-unrelated-histories
$git pull origin master --allow-unrelated-histories
From github.com:myusernameGithub/watchlist
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git push -u origin master
Enumerating objects: 671, done.
Counting objects: 100% (671/671), done.
Delta compression using up to 4 threads
Compressing objects: 100% (648/648), done.
Writing objects: 100% (670/670), 2.38 MiB | 1.06 MiB/s, done.
Total 670 (delta 54), reused 0 (delta 0)
remote: Resolving deltas: 100% (54/54), done.
To github.com:myusernameGithub/watchlist.git
4b6e101..51be486 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
搞定!