git 第一次 push 遇到问题

开始用 git 的时候我只会

git clone git pull git push

这三个命令满足了我的基本需求,到自己创建仓库的时候遇到了问题,

git remote add origin https://github.com/logig/wechat-shake.git
git push -u/--set-upstream origin master

上面是官方提示的方法,只有仓库是空的时候才可以这样用。
如果在创建仓库时选择了添加 license 文件等操作,按上面来操作就会报错。

以下是我的解决方法,

git remote add origin https://github.com/logig/wechat-shake.git
git pull
git branch -u/--set-upstream-to origin/master # 下一次 push 就不用带 -u 了
git pull
git push

问题解决。


执行每一步命令后观察 .git/config 文件的变化,下面是我对这个过程的理解,

[remote "origin"]
	url = https://github.com/logig/wechat-shake.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

git remote add origin 增加了一个 remote 项。Git 是分布式的,可以有多个 remote,

比如,我可以再添加一个码云的 remote

git remote add oschina https://git.oschina.net/logig/wechat-shake.git
[remote "origin"]
	url = https://github.com/logig/wechat-shake.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[remote "oschina"]
	url = https://git.oschina.net/logig/wechat-shake.git
	fetch = +refs/heads/*:refs/remotes/oschina/*

git push不带参数的时候 push 到一个默认的 remote,git branch -u就是起到设置默认 remote 的作用。

也可以 git push oschina 推到任意 remote。


git 的命令比较多,但是每个命令都可以方便的查看帮助 git help remote/push/branch/...

原文地址:https://www.cnblogs.com/liaozt/p/5852755.html