fetch 必须配合着merge 使用,光fetch,只是改变了 cat .git/FETCH_HEAD,
git branch并没有改变,还需要做merge操作
git merge --allow-unrelated-histories origin/xxx
=========
pull时有冲突,如果想全用theirs的
# fetch from the default remote, origin
git fetch
# reset your current branch (master) to origin's master
git reset --hard origin/master
===========
http://www.cnblogs.com/lbsx/archive/2010/10/16/1853193.html
https://ruby-china.org/topics/15729
git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。
$ git pull <远程主机名> <远程分支名>:<本地分支名>
比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。
$ git pull origin next:master
如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
$ git pull origin next
上面命令表示,取回origin/next分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。
$ git fetch origin
$ git merge origin/next
在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。
Git也允许手动建立追踪关系。
git branch --set-upstream master origin/next
上面命令指定master分支追踪origin/next分支。
如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名。
$ git pull origin
上面命令表示,本地的当前分支自动与对应的origin主机”追踪分支”(remote-tracking branch)进行合并。
如果当前分支只有一个追踪分支,连远程主机名都可以省略。
推荐使用git pull --rebase
Runs git fetch with the given parameters, and calls git merge to merge the retrieved head(s) into the current branch. With --rebase, calls git rebase instead of git merge. 因为merge会产生难看的冲突。
如果你这个特定的分之只是一个临时的,做一些功能开发,最后要把这个分支的变动合并回 master,那么就需要在特定分支上 git pull --rebase origin master
一般我们在使用 Git 进行代码更新时,在本地修改完成之后,进行 commit,然后 push,如果此时远程库有人提交了一次 commit,与我本地的修改是同一个文件,那么肯定会提示冲突导致 push 被 rejected。
这时候,我们一般的习惯应该是 git pull --rebase
(记住: 不是 git pull)。
pull = fetch + merge
每一个操作都是要指明【来源】和【目标】的,而对于 pull 来说,【目标】就是当前分支
git fetch
拿到了远程所有分支的更新,用 cat .git/FETCH_HEAD
可以看到其状态
不要用git pull,用git fetch和git merge代替它。
git fetch
取回所有分支(branch)的更新。
所以要比较本地分支与远程分支的不同时,先
- git fetch //fetch后,并不会把远程的变更应用到本地
- git diff master origin/master
- git merge origin/master
git log --remotes=origin
当git clone之后,直接git pull它会自动匹配一个正确的remote url
是因为在config文件中配置了以下内容:
2 remote = origin
3 merge = refs/heads/master
表明:
1.git处于master这个branch下时,默认的remote就是origin;
2.当在master这个brach下使用指定remote和merge的git pull时,使用默认的remote和merge。
但是对于自己建的项目,并用push到远程服务器上,并没有这块内容,需要自己配置。
如果直接运行git pull,会得到如此结果:
2 Password:
3 You asked me to pull without telling me which branch you
4 want to merge with, and 'branch.master.merge' in
5 your configuration file does not tell me, either. Please
6 specify which branch you want to use on the command line and
7 try again (e.g. 'git pull <repository> <refspec>').
8 See git-pull(1) for details.
9
10 If you often merge with the same branch, you may want to
11 use something like the following in your configuration file:
12
13 [branch "master"]
14 remote = <nickname>
15 merge = <remote-ref>
16
17 [remote "<nickname>"]
18 url = <url>
19 fetch = <refspec>
20
21 See git-config(1) for details.
在参考[2]中,有这样一段:
Note: at this point your repository is not setup to merge _from_ the remote branch when you type 'git pull'. You can either freshly 'clone' the repository (see "Developer checkout" below), or configure your current repository this way:
2 git config branch.master.remote origin
3 git config branch.master.merge refs/heads/master
因此通过git config进行如下配置:
2 $ git config branch.master.merge refs/heads/master
或者加上--global选项,对于全部项目都使用该配置。
参考:
1.How do you get git to always pull from a specific branch? - Stack Overflow
一般我们在使用 Git 进行代码更新时,在本地修改完成之后,进行 commit,然后 push,如果此时远程库有人提交了一次 commit,与我本地的修改是同一个文件,那么肯定会提示冲突导致 push 被 rejected。
这时候,我们一般的习惯应该是 git pull --rebase
(记住: 不是 git pull)。
或者 git fetch && git rebase
提示:推荐为 pull --rebase
设置 pl
作为 alias:
git config --global alias.pl pull --rebase
这样,执行 git pl
之后,Git 会把远程库拉下来之后会自动 merge 有修改的文件。
因为只要一执行 git pull
它就会自动进行 merge,产生那个不易看懂的 merge 之后的文件。
=======================
在使用Git Push代码到数据仓库时,提示如下错误:
[remote rejected] master -> master (branch is currently checked out)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@192.168.1.X:/var/git.server/.../web
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@192.168.1.X:/var/git.server/.../web'
这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如下代码:
[receive]
denyCurrentBranch = ignore
在初始化远程仓库时最好使用 git --bare init 而不要使用:git init
如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时, 如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上, 也即在远程仓库的目录下对应的文件还是之前的内容,必须得使用git reset --hard才能看到push后的内容.
=================
git push origin master 将本地master分支推送到远端origin 的master分支
git push origin master:develop 将本地master分支推送到远端origin 的develop分支
git remote show origin
git remote -v
https://www.cnblogs.com/my_life/articles/4134356.html
$git diff origin/master remotes/origin/master
git push origin master
origin指定了你要push到哪个remote
master其实是一个“refspec”,正常的“refspec”的形 式为”+<src>:<dst>”,冒号前表示local branch的名字,冒号后表示remote repository下 branch的名字。
注意,如果你省略了<dst>,git就认为你想push到remote repository下和local branch相同名字的branch。听起来有点拗口,再解释下,push是怎么个push法,就是把本地branch指向的commit push到remote repository下的branch,比如
$git push origin master:master (在local repository中找到名字为master的branch,使用它去更新remote repository下名字为master的branch,如果remote repository下不存在名字是master的branch,那么新建一个)
$git push origin master (省略了<dst>,等价于“git push origin master:master”)
$git push origin master:refs/for/mybranch (在local repository中找到名字为master的branch,用他去更新remote repository下面名字为mybranch的branch) $git push origin master:refs_for_mybranch
$git push origin HEAD:refs/for/mybranch (HEAD指向当前工作的branch,master不一定指向当前工作的branch,所以我觉得用HEAD还比master好些)
$git push origin :mybranch (再origin repository里面查找mybranch,删除它。用一个空的去更新它,就相当于删除了)