github提交可能遇到的问题

来源:互联网 发布:大数据培训好就业吗 编辑:程序博客网 时间:2024/05/18 02:13
Git Push(向远程Github提交本地代码)
$ git push git@github.com:hepusheng/linux-2.6.11-study-note.git master // 把本地仓库提交到远程仓库的master分支中


$ git remote add origin git@github.com:hepusheng/linux-2.6.11-study-note.git
$ git push -u origin master


这两个操作是等价的,第二个操作的第一行的意思是添加一个标记,让origin指向git@github.com:hepusheng/linux-2.6.11-study-note.git,也就是说你操作origin的时候,实际上就是在操作git@github.com:hepusheng/linux-2.6.11-study-note.git。
    默认情况下这条语句等价于提交本地的master仓库到远程仓库,并作为远程的master分支。


在向远程提交代码时出现了错误:
1.error:failed to push some refs to ...
当要push代码到git时,出现提示:
error:failed to push some refs to ...
Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:
$ git push origin master 
To ../remote/ 
 ! [rejected]        master -> master (non-fast forward) 
error: failed to push some refs to '../remote/' 
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
 
问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去,特别对于在Github上新建版本仓库时,因为它为了增加其它人对用户提交的代码的可读性时,默认是要求新建一个README.md文件,和系统会生成.gitignore 文件。所以在push化码到远程仓库时,因为我们本地仓库中没有类似于README.md或.gitignore 等文件,这样就是提示(Non-fast-forward). 有2个方式可以解决这个问题:
 
强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容,这种方式不建议使用。
git push -f
 
2. 先把git的东西fetch到你本地然后merge后再push
$ git fetch
$ git merge
这2句命令等价于
$ git pull 
可是,这时候又出现了如下的问题:
上面出现的 [branch "master"]是需要明确(.git/config)如下的内容
[branch "master"]
    remote = origin
    merge = refs/heads/master
这等于告诉git2件事:
1,当你处于master branch, 默认的remote就是origin。
2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变
如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:
$ git config branch.master.remote origin 
$ git config branch.master.merge refs/heads/master 
之后再重新git pull下。最后git push你的代码,到此步顺利完成时,则可以在Github上看到你新建的仓库以及你提交到仓库中文件了,OK。
0 0
原创粉丝点击