git push和pull时遇到的两个问题及解决办法

学习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没有同步到本地。
readme

解决办法: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'.

搞定!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

steventian72

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值