Zhao Dongyu's Blog

A life which is unexamined is not worth living.

0%

git

日常的一些git操作其实已经非常熟悉了,比较欠缺的是团队协作遇到的一些问题。

在此记录一下需要经常查阅的情景。

Git:将当前修改的内容提交到新的分支上

前言:

有时候在参加一个项目时,执行clone得到master分支, 一开始只是想看看源码或者忘记了自己没有新建分支,结果后面自己根据需求添加了代码添加后没有执行commit, 但是此时的修改都在master分支, 提交必然是不可以的,还是要新建分支所有修改都要在新建分支上进行,最后在分支执行通过后,才能合并到master分支。 那么,这时候如何力挽狂澜,如何在保存这些修改的前提下,新建分支并提交呢?

操作思路:

1
2
3
4
5
6
7
8
9
//步骤1:在当前的master分支上的修改暂存起来
git stash
//步骤2:暂存修改后,在本地新建分支(new_branch为新分支的名字)
git checkout -b new_branch
//步骤3:将暂存的修改放到新建分支中
git stash pop
//步骤4:使用TortoiseGit进行commit,比如add、modify、delete...
//步骤5:将提交的内容push到远程服务器
git push

————————————————

版权声明:本文为CSDN博主「Oruizn」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Oruizn/article/details/111294375

git创建本地分支并关联远程分支

1. 创建本地分支

git branch 分支名

例如:git branch dev,这条命令是基于当前分支创建的本地分支,假设当前分支是master(远程分支),则是基于master分支创建的本地分支dev。

2. 切换到本地分支

git checkout 分支名

例如:git checkout dev,这条命令表示从当前master分支切换到dev分支。 ## 3. 创建本地分支并切换

git checkout -b 分支名

例如:git checkout -b dev,这条命令把创建本地分支和切换到该分支的功能结合起来了,即基于当前分支master创建本地分支dev并切换到该分支下。

4. 提交本地分支到远程仓库

git push origin 本地分支名

例如:git push origin dev,这条命令表示把本地dev分支提交到远程仓库,即创建了远程分支dev。

注:要想和其他人分享某个本地分支,你需要把它推送到一个你拥有写权限的远程仓库。你创建的本地分支不会因为你的写入操作而被自动同步到你引入的远程服务器上,你需要明确地执行推送分支的操作。换句话说,对于无意分享的分支,你尽管保留为私人分支好了,而只推送那些协同工作要用到的特性分支。

5.新建本地分支与远程分支关联

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

或者

git branch –set-upstream-to=origin/远程分支名

例如:git branch –set-upstream-to=origin/dev,把本地dev分支和远程dev分支相关联。

注:本地新建分支, push到远程服务器上之后,使用git pull或者git pull 拉取或提交数据时会报错,必须使用命令:git pull origin dev(指定远程分支);如果想直接使用git pullgit push拉去提交数据就必须创建本地分支与远程分支的关联。

————————————————

版权声明:本文为CSDN博主「EntyIU」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_59244784/article/details/129170582


推git遇到了一个错误:

remote: fatal: pack exceeds maximum allowed size

估计以后也会再次遇到,记录一下解决步骤。

解决方案

第一反应当然是google一下,顺利找到解决方案

First, you can use git-sizer to get an idea of what is taking too much space in your current local repository (that you fail to push)

  • if it is because of a commit too big, you can:

    • git reset @~ to cancel that commit
    • remake several smaller commits
    • try and push again
  • if it is because of a file too big, you can try and activate Git LFS, but that is limited by quota and going above might include a non-free service.

More generally, a "large project" might need to be split up into several Git repositories.

实践操作

1、安装git-sizer

https://formulae.brew.sh/formula/git-sizer

brew install git-sizer

2、分析文件大小

在git项目内执行git-sizer

Processing blobs: 6673                        
Processing trees: 1053                        
Processing commits: 2                        
Matching commits to trees: 2                        
Processing annotated tags: 0                        
Processing references: 3                        
| Name                         | Value     | Level of concern               |
| ---------------------------- | --------- | ------------------------------ |
| Biggest objects              |           |                                |
| * Blobs                      |           |                                |
|   * Maximum size         [1] |  60.9 MiB | ******                         |
|                              |           |                                |
| Biggest checkouts            |           |                                |
| * Maximum path depth     [2] |    10     | *                              |
| * Maximum path length    [2] |   146 B   | *                              |
| * Total size of files    [2] |  1.40 GiB | *                              |

[1]  ***********(delete)*********** (refs/heads/main:toolchain/***********(delete)***********)
[2]  ***********(delete)*********** (refs/heads/main^{tree})

好像也没有太大的文件,最大的也就是64MB。那应该是整个commit太大了。

于是我拆开多个分别push,解决了问题。

LFS

下次遇到不得不使用LFS的时候再来补充后续的博客

Git LFS的使用

brew install git-lfs

git lfs install

Thanks for your support.