git 分支本地创建、推送与删除远程分支指定文件

本文详细介绍如何使用Git进行分支管理,包括创建本地与远程分支、关联本地与远程分支、删除远程分支及文件等操作。通过具体步骤与命令示例,帮助读者掌握Git分支的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

git创建分支并推送到远程分支

将本地分支与远程分支关联

删除指定远程分支

删除远程分支的某个指定文件



git创建分支并推送到远程分支

新建一个本地分支.

git checkout -b show_branch_local

查看当前分支状态

git branch

结果:

  master
  on_cluster_snapshot_etl_v2
* show_branch_local
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

星号(*)表示当前所在分支。没有前面的字母的是本地分支。现在的状态是成功创建的新的分支并且已经切换到新分支上。

把新建的本地分支push到远程服务器,远程分支与本地分支同名(当然可以随意起名):

git push origin show_branch_local:show_branch_remotes

结果提示:

Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for show_branch_remotes, visit:
remote:   https://git.xxxx.com.cn/snapshot-fe/snapshot_etl_v2/merge_requests/new?merge_request%5Bsource_branch%5D=show_branch_remotes
remote:
To https://git.xxxx.com.cn/snapshot-fe/snapshot_etl_v2.git
 * [new branch]      show_branch_local -> show_branch_remotes
zwdeMacBook-Pro:snapshot_etl_v2 zhaowang$

使用查看分支命令查看,remotes后面是否有自己刚推到远程的分支命令

git branch -a

结果: 

 master
  on_cluster_snapshot_etl_v2
* show_branch_local
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/on_cluster_snapshot_etl_v2
  remotes/origin/show_branch_remotes

将本地分支与远程分支关联

git branch --set-upstream 本地分支名 origin/远程分支名

注意上面set 前有两个连续的-  且和up之间的-没有空格。如

$ git branch --set-upstream nspagev_rank_v2_with_data_test1 origin/nspagev_rank_v2_with_data_test1 

删除指定远程分支

git push origin –delete show_branch_remotes

或者推送一个空分支到远程分支(就相当于删除了远程分支)

git push origin :show_branch_remotes

结果提示:

To https://git.xxx.com.cn/snapshot-fe/snapshot_etl_v2.git

 - [deleted]         show_branch_remotes

查看分支命令,已经看不到show_branch_remotes了,即已经删除。

删除远程分支的某个指定文件

1)预览将要删除的文件(当前分支下)

git rm -r -n –cached 目标文件/文件夹名称

加上 -n 这个参数,执行命令时,是不会删除任何文件,而是展示此命令要删除的文件列表预览。

上面的目标文件可以不写路径,结果会提示到带着具体的路径。

2) 确定无误后删除文件

git rm -r --cached 文件/文件夹名称
如$ git rm -r --cached get_days_data_auto_0803_31.sh get_days_data_auto_0903_31.sh 
结果提示 
rm 'script/get_days_data_auto_0803_31.sh'
rm 'script/get_days_data_auto_0903_31.sh'

3 提交说明与推送。

git commit -m '删除之前写死的脚本

git push  本地分支名:远程分支名

(注意此要加origin,然后空格如)

git push origin on_cluster_snapshot:on_cluster_snapshot

参考: git创建分支并推送到远程分支_git新建分支并推送到远程_赖进杰的博客-优快云博客

https://www.jianshu.com/p/de75a9e3d1e1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值