iOS开发-知识合集iOS学习Android开发经验谈

git使用常见问题

2017-07-12  本文已影响843人  Xcoder_
1、问题描述:

出现 Your branch and 'origin/master' have diverged,
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean

解决办法:

如果不需要保留本地的修改,只要执行下面两步:

$ git fetch origin
$ git reset --hard origin/master

KUNdeiMac:test kun$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 3 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
KUNdeiMac:test kun$ git branch -a
* master
  remotes/origin/master
KUNdeiMac:test kun$ git fetch origin 
KUNdeiMac:test kun$ git reset --hard origin/master
HEAD is now at a79252b 删除文件.gitignore
KUNdeiMac:test kun$ git status
2、问题描述:

当 git push 时出现Updates were rejected because the tip of your current branch is behind错误。

【分析】:因为远程repository和我本地的repository冲突导致的,而我在创建版本库后,在github的版本库页面点击了创建README.md文件的按钮创建了说明文档,但是却没有pull到本地。这样就产生了版本冲突的问题


image.png
解决办法:

参考:http://www.cnblogs.com/daemon369/p/3204646.html

三种解决办法:

1.使用强制push的方法:

$ git push -u origin master -f

这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。

2.push前先将远程repository修改pull下来

$ git pull origin master

$ git push -u origin master

3.若不想merge远程和本地修改,可以先创建新的分支:

$ git branch [name]

然后push

$ git push -u origin [name]
3、问题描述:

The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to

解决办法:

正确的本地分支关联远程分支命令 是

$ git branch --set-upstream-to <remote>/<branch> <localbranch>

KUNdeiMac:test kun$ git status
On branch master
Your branch is up-to-date with 'myorigin/master'.
nothing to commit, working tree clean
KUNdeiMac:test kun$ git branch -a
* master
  remotes/myorigin/master
  remotes/origin/master
KUNdeiMac:test kun$ git branch --set-upstream-to origin/master
Branch master set up to track remote branch master from origin.
KUNdeiMac:test kun$ 
4、问题描述:

合并pull两个不同的项目失败 ,提示:fatal: refusing to merge unrelated histories]

解决办法:

$ git pull origin master --allow-unrelated-histories

5、问题描述:

关于origin/HEAD,git可以默认选择哪个分支(即克隆时), 默认情况下origin / HEAD指向的即为默认分支,比如origin/HEAD -> origin/master ,指向的origin/master即为默认分支。

解决办法:

参考:https://stackoverflow.com/questions/354312/why-is-origin-head-shown-when-running-git-branch-r

① 您可以在GitHub repo的管理设置中更改此设置。 您也可以从命令行设置

$ git remote set-head origin trunk

② 或者删除它

$ git remote set-head origin -d

KUNdeiMac:SKCamera kun$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

KUNdeiMac:SKCamera kun$ git remote set-head origin -d
KUNdeiMac:SKCamera kun$ git branch -a
* master
  remotes/origin/master
KUNdeiMac:SKCamera kun$ 
上一篇下一篇

猜你喜欢

热点阅读