Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git实践篇 #171

Open
soapgu opened this issue Oct 12, 2022 · 0 comments
Open

Git实践篇 #171

soapgu opened this issue Oct 12, 2022 · 0 comments
Labels

Comments

@soapgu
Copy link
Owner

soapgu commented Oct 12, 2022

  • 前言

虽然Git经过系统学习,真的到具体的实例有时候经常手忙脚乱,这里再进行一下查漏补缺

  • Key Point

持续更新中

  • git同步远程已删除的分支和删除本地多余的分支

这是我们推行git新流程带来的“新问题”,由于feature分支在合并以后。服务端会自动删除。所以本地会多很多“僵尸”分支
可以通过

git remote show origin

* 远程 origin
  获取地址:https://git.shgbit.xyz:9443/team1/gitflowtest.git
  推送地址:https://git.shgbit.xyz:9443/team1/gitflowtest.git
  HEAD 分支:main
  远程分支:
    dev                           已跟踪
    feature2-1                    已跟踪
    main                          已跟踪
    nicecommit                    已跟踪
    refs/remotes/origin/feat_test 过时(使用 'git remote prune' 来移除)
    release-1.0                   已跟踪
    release-2.0                   已跟踪
    v1.1.1-hotfix                 已跟踪

查看远端的分支情况,这里可以看到feat_test已经被删了
注:删除远端分支,使用git fetch没有任何反应

使用下面命令清理过期远端分支

git remote prune origin

修剪 origin
URL:https://git.shgbit.xyz:9443/team1/gitflowtest.git
 * [已删除] origin/feat_test

最后是干掉本地分支

git branch -D feat_test

已删除分支 feat_test(曾为 0db7be1)

注意:

  • git branch -d 会在删除前检查merge状态(其与上游分支或者与head

  • git branch -D 是git branch --delete --force的简写,它会直接删除,大D还是猛啊
    图片

  • 初始化一个新仓库到远端

  1. git init
  2. 本地commit
  3. 在远端创建仓库
  4. 添加本地仓库和远端仓库关联
git remote add origin [email protected]:soapgu/S3Demo.git

注:origin这个远端别名命名是常规操作,使用上希望大家墨守成规。

  1. “第一次”推送远端
git push -u origin master

这里-u 是推送分支的同时设置和远端分支的关联
和下面代码等价

git push origin master
git branch --set-upstream master origin/master

When the command line does not specify where to push with the argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin

设置完成后后面的pull/push都不用指定repository了

  • git本地仓库迁移指南

gitlab可以使用git远程地址和用户密码来完成远端到远端到迁移,这个比较简单就不讲了。

有一种场景也是很有用,本地有原来git的代码和仓库,但是现在各种原因,现在的远程仓库不能用了,需要迁移到新仓库中去。

先重命名origin,原有远程仓库备份(以后可以删除)
git remote rename origin old-origin

git remote add origin <新地址>

git push -u origin --all

git push -u origin --tags
@soapgu soapgu added the Git label Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant