赞
踩
$ git push origin master
To github.com:meteorsh/demo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:meteorsh/demo.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.
使用git pull --rebase origin master
来合并远程GitHub库和本地库的文件,本地会生成之前你没有进行同步的文件
举个栗子:A和B一起开发和维护一个GitHub仓库,周二是A工作,周三是B工作。周一的时候A和B的从远端clone了一份仓库到各自的本地库,但是当A完成周二的工作时,远端GitHub仓库的内容已经有了更新或者说添加了commit,那么到了周三B必须先把自己的本地仓库更新成远端最新的GitHub仓库,然后再开始这一天的工作,这是显然是非常合理的工作流程。
但如果两个人工作时间很近(如只间隔1小时),那B以为远端GitHub没更新过,其实A已经更新了远端仓库,这时不可避免会出现git push冲突的问题
所以这时要用到git pull --rebase origin master
命令
这时再git push origin master就不会有冲突了
从远端拉取分支,其实有下面两种方式(两者具体区别我还没有深入研究)
git pull = git fetch && git merge
git pull --rebase = git fetch && git rebase
这里我再用git pull origin master来拉取分支
有时候,使用git pull命令会产生冲突(因为这里远端和本地库都修改了local-test1.txt文件)
我们需要手动fix冲突,然后添加一个新的commit,最后git push到远端GitHub库
然后git push origin master推到远端GitHub库即可
PS:这时在终端用glog命令,让我明白了HEAD -> master, origin/master, origin/HEAD
这几句话的含义
git pull是建立在你的修改已经加到git的历史世界线上的(commit形成一个结点)
每次使用git pull命令前,需要将本地工作区编辑过的文件添加到暂存区(git add .),或提交到本地仓库中(git commit),才可以使用该命令拉取指定分支的代码合并到当前分支中
每次在操作完git commit命令后,必须拉取一下master分支代码,保持本地正在开发功能逻辑的代码分支代码是最新的,避免后续在提交时冲突过多或覆盖掉其他人的代码的问题出现
我在本地修改了local-test1.txt文件,但是还没有git commit,同时远端GitHub我也修改了local-test1.txt文件,此时git会报错
解决方法:把本地的所有修改用git commit添加到暂存区,然后再用git pull命令
未合并冲突前,git记录可视化是这样的
此时直接git push会报错:Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes
正确做法当然是先解决冲突(手动修复冲突文件),然后git commit,最后再git push
git clone 仓库地址
这时你进入clone的文件夹用git add, git commit, git push等命令就不会出现报错
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。