git笔记

发布于 2024-12-03  336 次阅读



建立仓库


设置用户和邮箱

# 仅设置当前仓库的用户和邮箱
git config --local user.name <name-string>
git config --lcoal user.email <email-string>

# 设置全局仓库的用户和邮箱
git config --global user.name <name-string>
git config --global user.email <email-string>

工作区、暂存区、本地仓库

# 查看工作区和暂存区的状态
git status

# 提交所有工作区到暂存区
git add .

# 提交工作区指定内容到暂存区
git add <file or directory>

# 移除暂存区文件但不删除原文件
git rm --cached <file>

# 递归移除暂存区目录但不删除原目录
git rm --cached -r <directory>

# 移除暂存区文件并删除原文件、目录同理
git rm -f <file>

# 移除工作区和暂存区内容,即回撤内容到本地仓库某个版本,不加id则默认重置到当前版本
git reset --hard <commit id>

# 提交暂存区到本地仓库, 不加 -m 则在打开的新页面添加提交说明
git commit -m <message string>

版本和分支管理

# 查看当前版本的变动
git show

# 查看版本提交记录
git log

# 检出到某个指定版本
git checkout <commit id>

# 查看版本的变更记录
git reflog

# 查看本地分支
git branch -v

# 查看远程关联分支
git branch -a

# 创建变切换到新分支, 推荐使用switch, switch专用于管理分支的切换
git switch -c <branch-name>
git checkout -b <branch-name>

# 创建新分支但不切换到新分支
git branch <branch-name>

# 切换到指定分支
git switch <branch-name>
git checkout <branch-name>

# 删除指定分支
git branch -d <branch-name>

# 合并指定分支到当前分支, 被合并到分支不会改变, 仅影响当前分支
git merge <branch-name>

本地仓库和远程仓库之间操作

# 查看当前远程仓库url
git remote -v

# 拉取远程仓库内容
git pull

# 推送本地仓库到远程仓库
git push

# 设置远程仓库url
git remote set-url <remote-name> <remote-url>

# 删除和远程仓库的关联
git remote remove <remote-name>

其他

# 查看标签
git tag

# 添加标签, 可以用"-m"添加标签的注释
git tag <tag-name>
git tag <tag-name> -m <tag-info>

# 查看指定标签的信息
git show <tag-name>

# 删除标签
git tag -d <tag-name>

# 推送标签到远程仓库
git push origin --tags
届ける言葉を今は育ててる
最后更新于 2024-12-03