今天使用
git push
提交代码的时候出现了一些问题,于是把相关的内容整理在这里,以备后用,希望对您也有所帮助,谢谢。
强制pull
git pull -f | --force <remote-repo>[:local-repo] <remote-branch>[:<local-branch>]
git checkout ./ #恢复所有本地更改
git pull --force origin master:master # 强制拉取远程
问题:
- 当我使用传统的方式在远程建立一个仓库,然后通过把仓库的代码clone到本地,再把本地内容拷贝到带有
.git
的文件夹中,接着git add ./
,再git commit -m ""
,再git push
,报错如下:{ ~~~ } master » git push No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. Everything up-to-date
- 翻译过来就是:这个仓库没有公用的远程引用,也没有专门指定,什么也做不了,你应该手动添加这个本地仓库要push到远程仓库的master分支上
解决问题
- 执行以下命令:
{ ~~~ } master » git push origin master
新问题来了:这是在干啥?
- git push origin master
- 翻译:把本地的master分支提交到origin仓库的master分支上
- 注意:
- origin:指的是本地的仓库对应的远程仓库的地址是什么,即我们在clone的时候的那个地址,亦即哪个远程仓库,如果报错说origin没有,则可以使用下面的语句指定:
git remote add origin <git-repo-addr>
- master:远程仓库的哪个分支名称
- origin:指的是本地的仓库对应的远程仓库的地址是什么,即我们在clone的时候的那个地址,亦即哪个远程仓库,如果报错说origin没有,则可以使用下面的语句指定:
- 如果我要把本地指定的分支提交到远程的master分支上呢?
- 把本地的
v_1.0
分支提交到远程的master
分支上git push origin v_1.0:master
- 把本地的
v_1.0
分支提交到远程的v_1.0
分支上git push origin v_1.0:v_1.0
-
记住这个公式即可:
git push <remote-repo-name> [<local-baranch-name>:]<remote-branch-name>
- 如果[]的内容不写,默认是将本地的master分支提交到远程指定的分支上
- 把本地的
- 关于git push的另类用法: 删除远程分支
git push origin :v_1.0
- 注意:执行上面的语句后,远程上的分支
v_1.0
就会被干掉了,但是本地的分支v_1.0
不会受影响;
- 注意:执行上面的语句后,远程上的分支
git push 官方文档
语法结构
git push
[--all | --mirror | --tags]
[--follow-tags]
[-n | --dry-run]
[--receive-pack=<git-receive-pack>]
[--repo=<repository>]
[-f | --force]
[--prune]
[-v | --verbose]
[-u | --set-upstream]
[--force-with-lease[=<refname>[:<expect>]]]
[--no-verify]
[<repository> [<refspec>...]]
- 参数说明:
- --all:Push all branches (i.e. refs under refs/heads/); cannot be used with other <refspec>.
- --follow-tags:Push all the refs that would be pushed without this option, and also push annotated tags in refs/tags that are missing from the remote but are pointing at commit-ish that are reachable from the refs being pushed.
- -n, --dry-run:Do everything except actually send the updates.
- --receive-pack=<git-receive-pack>, --exec=<git-receive-pack>:Path to the git-receive-pack program on the remote end. Sometimes useful when pushing to a remote repository over ssh, and you do not have the program in a directory on the default $PATH.
- --repo=<repository>:This option is only relevant if no <repository> argument is passed in the invocation. In this case, git push derives the remote name from the current branch: If it tracks a remote branch, then that remote repository is pushed to. Otherwise, the name "origin" is used. For this latter case, this option can be used to override the name "origin". In other words, the difference between these two commands
git push public #1
git push --repo=public #2
is that #1 always pushes to "public" whereas #2 pushes to "public" only if the current branch does not track a remote branch. This is useful if you write an alias or script around git push. - -f, --force:Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, when --force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected.
This flag disables these checks, and can cause the remote repository to lose commits; use it with care.
Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push
may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front
of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details. - -u, --set-upstream:For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).
- 其他参看:git push --help