git 强制推送到master

强制推送到master

git checkout master 
切换到旧的分支

git reset --hard develop  
 将本地的旧分支 master 重置成 develop

git push origin master --force 
 再推送到远程仓库
 

如果出错:

Total 0 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To http://xxx/xxx.git
 ! [remote rejected]   master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://xxx/xxx.git'

先把master取消protect.
创建本地分支stable  并且 关联到远程的 origin/stable分支 
git checkout -b stable origin/stable
只合并1dc642a5d8 commit的提交
git cherry-pick 1dc642a5d8

切换分支
git checkout branch_name

git切换到指定远程分支
我们在使用git进行开发的时候经常会遇到需要切换远程分支并且提交到远程指定分支的情况,现在记录下操作步骤。

查看远程所有分支

$ git branch -a

git branch不带参数,列出本地已经存在的分支,并且在当前分支的前面用*标记,加上-a参数可以查看所有分支列表,包括本地和远程,远程分支一般会用红色字体标记出来

* dev
 master
 remotes/origin/HEAD -> origin/master
 remotes/origin/master
 remotes/origin/release/caigou_v1.0

新建分支并切换到指定分支

git checkout -b dev origin/release/caigou_v1.0

git checkout -b 本地分支名 origin/远程分支名

该命令可以将远程git仓库里的指定分支拉取到本地,这样就在本地新建了一个dev分支,并和指定的远程分支release/caigou_v1.0关联了起来。

Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'release/caigou_v1.0' from 'origin'.

查看本地分支及追踪的分支

$ git branch -vv

git branch -vv命令可以显示本地所有分支,执行结果如下:

* dev    e67e3ef [origin/release/caigou_v1.0] 供应商详情页面
 master 11160c2 [origin/master: behind 5] Merge branch 'master' of https://git.jointforce.com/scm/join/yj-stat

*表示当前所在分支,[远程分支]表示当前本地分支追踪的远程分支,最后一个是最近一次提交的注释。

将本地分支推送到远程

$ git push -u origin dev:release/caigou_v1.0

git push <远程主机名> <本地分支名>:<远程分支名>

成功后显示如下:

Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 754 bytes | 754.00 KiB/s, done.
Total 8 (delta 6), reused 0 (delta 0)
remote:
remote: Create pull request for release/caigou_v1.0:
remote:   https://git.jointforce.com/projects/JOIN/repos/yj-stat/compare/commits?sourceBranch=refs/heads/release/caigou_v1.0
remote:
To https://git.jointforce.com/scm/join/yj-stat.git
   a22ed65..e8782b2  dev -> release/caigou_v1.0
Branch 'dev' set up to track remote branch 'release/caigou_v1.0' from 'origin'.

上述命令表示将本地dev分支推送到远程release/caigou_v1.0分支。

你可能感兴趣的:(git 强制推送到master)