最近用到的git命令

本文整理了近期常用的 Git 命令,涵盖分支重命名、修改默认分支、更新本地引用、更改远程仓库 URL、多仓库同步推送(单 remote 多 URL 及多 remote)、添加/删除远程仓库、推送失败处理、认证配置、本地与空远程仓库关联,以及自动筛选 50M+ 文件并通过 Git LFS 追踪的脚本。

作者:zhuge···预计阅读 11 分钟·237 阅读·0 评论
最近用到的git命令

最近 git 仓库经常在改变,于是我就整理了一下现在阶段常用到的命令

分支重命名

# 1. 切换到 master 分支(如果不在的话)
git checkout master

# 2. 重命名分支为 main
git branch -m master main

# 3. 推送到远程(需要强制推送)
git push -u origin main

# 4. 删除远程的 master 分支
git push origin --delete master

修改默认分支

# 设置默认分支为 main
git config --global init.defaultBranch main

更新本地引用

# 获取远程所有分支
git fetch --all --prune

# 更新本地跟踪分支
git branch -u origin/main main

更改remote url

git remote -v
# 更改 origin 的 URL
git remote set-url origin https://github.com/用户名/新仓库名.git

一个仓库拉取但同步推送到多个仓库

查看/验证
git remote -v
#添加第一个 URL(若未配置)
git remote add origin https://github.com/你的用户名/仓库名.git

#追加第二个 URL 到 origin
git remote set-url --add origin https://gitee.com/你的用户名/仓库名.git

#推送代码
git push origin main  # main 为分支名,根据实际分支调整

#移除多余的 URL(如需)
git remote set-url --delete origin https://gitee.com/你的用户名/仓库名.git

添加多个不同名称的 remote

#添加第一个 remote(origin,指向 GitHub)
git remote add origin https://github.com/你的用户名/仓库名.git

#添加第二个 remote(如 gitee,指向 Gitee)
git remote add gitee https://gitee.com/你的用户名/仓库名.git

#分别推送代码
#推送到 GitHub:
git push origin main
#推送到 Gitee:
git push gitee main

#拉取指定仓库的代码:
#从 GitHub 拉取:
git pull origin main
#从 Gitee 拉取:
git pull gitee main

添加多个远程仓库

# 添加第二个远程仓库(通常命名为 upstream)
git remote add upstream https://github.com/原仓库所有者/仓库名.git

删除 upstream

git remote remove upstream

other

# 如果 push 失败,尝试:
git push -u origin main  # 首次推送需要 -u 参数

# 如果认证失败,重新配置凭据
git config --global credential.helper cache  # 临时缓存
git config --global credential.helper store  # 永久存储

将本地添加到空的远程

git remote add origin https://github.com/用户名/新仓库名.git
git branch -M main
git push -u origin main

自动查询50M以上的文件,并git lfs

#!/bin/bash

# 查找大于50MB的文件并生成git LFS track命令
echo "正在查找大于50MB的文件..."
echo "=============================="

# 使用find命令查找大于50MB的文件,排除.git目录
find . -type f -size +50M ! -path "*/.git/*" | while read file; do
    # 获取相对路径
    rel_path="${file#./}"
    
    # 显示文件信息
    file_size=$(du -h "$file" | cut -f1)
    echo "发现大文件: $rel_path (大小: $file_size)"
    
    # 生成git LFS track命令
    echo "git lfs track \"$rel_path\""
done | tee /tmp/lfs_track_commands.txt

echo ""
echo "=============================="
echo "所有git LFS track命令已保存到 /tmp/lfs_track_commands.txt"

# 可选:询问是否要自动运行这些命令
echo ""
read -p "是否要自动运行所有git LFS track命令?(y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "正在运行git LFS track命令..."
    echo ""
    # 运行保存的命令
    while read line; do
        if [[ "$line" == git\ lfs\ track* ]]; then
            echo "执行: $line"
            eval "$line"
        fi
    done < /tmp/lfs_track_commands.txt
    echo ""
    echo "所有git LFS track命令已执行完成!"
    echo ""
    echo "请确保执行以下操作:"
    echo "1. 检查.gitattributes文件:cat .gitattributes"
    echo "2. 添加.gitattributes文件:git add .gitattributes"
    echo "3. 提交更改:git commit -m '添加git LFS跟踪大文件'"
    echo "4. 如果文件已经在git中,需要重新添加:"
    echo "   git rm --cached <file>"
    echo "   git add <file>"
fi

相关文章

评论

加载中...