Git常用命令 1

核心概念

版本控制基础

  • 工作区 (Working Directory)执行 git init 初始化的文件夹,包含所有项目文件

  • 版本库 (Repository)工作区中的 .git 目录,存储所有版本控制数据

  • 暂存区 (Stage/Index).git/index 文件,临时存储工作区变更

.git 目录结构

.git/
├── index            # 暂存区
├── HEAD             # 指向当前分支的最新提交
├── refs/            # 分支和标签的引用
├── info/            # 仓库级忽略规则
├── logs/            # 操作日志
└── objects/         # Git对象数据库

基础配置

用户身份设置

# 系统级配置(所有用户)
git config --system user.name "Your Name"
git config --system user.email "email@example.com"

# 用户级配置(推荐)
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# 项目级配置
git config --local user.name "Your Name"
git config --local user.email "email@example.com"

查看配置

git config --list  # 查看所有配置
git config user.name  # 查看用户名
git config user.email # 查看邮箱

SSH 密钥生成

ssh-keygen -t rsa -C "your_email@example.com"

仓库操作

克隆仓库

# HTTP协议
git clone http://example.com/repo.git
git clone -b branch_name http://example.com/repo.git

# SSH协议
git clone git@example.com:repo.git
git clone ssh://git@example.com:port/repo.git

远程仓库管理

git remote -v  # 查看远程仓库
git remote add origin git@example.com:repo.git  # 添加远程仓库
git remote show origin  # 查看远程仓库详情
git remote rename origin new_name  # 重命名
git remote rm origin  # 删除

基础工作流

代码提交

git add .  # 添加所有变更到暂存区
git add -u  # 添加已跟踪文件的变更
git add -A  # 添加所有变更(包括未跟踪文件)
git commit -m "commit message"  # 提交到本地仓库

撤销操作

git reset HEAD file  # 从暂存区撤销文件
git checkout -- file  # 丢弃工作区修改
git commit --amend  # 修改最后一次提交

分支管理

git branch  # 查看本地分支
git branch -r  # 查看远程分支
git branch -a  # 查看所有分支
git branch new_branch  # 创建分支
git checkout -b new_branch  # 创建并切换分支
git branch -d branch_name  # 删除分支
git branch -m new_name  # 重命名分支

高级操作

合并与变基

git merge branch_name  # 合并分支
git merge --no-ff branch_name  # 非快进合并
git rebase branch_name  # 变基操作
git rebase --abort  # 终止变基
git rebase --continue  # 继续变基

代码同步

git fetch --all --prune  # 获取所有远程更新并清理已删除分支
git pull origin branch_name  # 拉取并合并
git push origin branch_name  # 推送分支
git push origin :branch_name  # 删除远程分支

标签管理

git tag  # 查看标签
git tag v1.0  # 创建轻量标签
git tag -a v1.0 -m "Release 1.0"  # 创建附注标签
git push origin v1.0  # 推送标签
git push origin --tags  # 推送所有标签
git tag -d v1.0  # 删除本地标签
git push origin :refs/tags/v1.0  # 删除远程标签

问题排查

查看状态与历史

git status  # 查看工作区状态
git log --graph --oneline  # 图形化查看提交历史
git reflog  # 查看所有操作记录
git diff  # 比较工作区与暂存区
git diff --cached  # 比较暂存区与仓库

代码回退

git reset --soft commit_id  # 仅回退提交记录
git reset --mixed commit_id  # 回退提交和暂存区(默认)
git reset --hard commit_id  # 彻底回退(慎用)
git revert commit_id  # 创建反向提交

暂存工作现场

git stash  # 暂存当前工作
git stash list  # 查看暂存列表
git stash apply  # 恢复最近暂存
git stash pop  # 恢复并删除暂存记录
git stash drop  # 删除暂存记录

最佳实践

  1. 分支策略

    1. main/master: 生产环境代码

    2. develop: 集成测试分支

    3. feature/xxx: 功能开发分支

    4. hotfix/xxx: 紧急修复分支

  1. 提交规范

    1. 使用语义化提交信息

    2. 单次提交只做一件事

    3. 提交前检查差异 (git diff --cached)

  1. 协作流程

    1. # 日常开发流程 git fetch --all --prune git checkout -b feature/xxx origin/develop # ...开发代码... git add . git commit -m "feat: add new feature" git push origin feature/xxx # 创建Pull Request合并到develop

  1. 版本发布

    1. git checkout develop git pull git checkout -b release/v1.0 # ...版本准备工作... git tag -a v1.0 -m "Release v1.0" git push origin v1.0 git checkout main git merge --no-ff release/v1.0 git push origin main

Git常用命令 2

一、创建于初始化
git init: 在当前目录初始化一个新的 Git 仓库。
git clone [url]: 克隆远程仓库到本地。

二、基本操作
git add . : 将文件添加到暂存区。
git commit -m “message”: 提交暂存区的文件到本地仓库。
git status: 显示工作区和暂存区的状态。
git diff: 显示文件修改的差异。

三、分支与合并
git branch: 列出本地分支,创建或删除分支。
git checkout [branch]: 切换到指定分支。
git merge [branch]: 合并指定分支到当前分支。
git rebase [branch]: 将当前分支变基到指定分支。

四、远程操作
git remote -v: 显示远程仓库的详细信息。
git fetch [remote]: 从远程仓库拉取最新变更。
git pull [remote] [branch]: 拉取远程分支并合并到本地分支。
git push [remote] [branch]: 将本地分支推送到远程仓库。

五、撤销与重置
git reset [file]: 从暂存区撤销文件的更改。
git revert [commit]: 撤销指定提交的更改。
git checkout – [file]: 恢复文件到最近一次提交的状态。

六、查看历史与日志
git log: 显示提交日志。
git show [commit]: 显示某次提交的详细内容。

七、标签与版本
git tag: 列出标签。
git tag [name]: 创建标签。
git tag -d [name]: 删除标签。

Git网络配置

Git 提交推送时> git pull --tags origin main fatal: unable to access Failed to connect to github.com port ...

git pull --tags origin main fatal: unable to access 'https://github.com/xxx/xxx.git/': Failed to connect to github.com port 443 after 21108 ms: Could not connect to server

错误原因:

可能是由于你使用了 VPN 改变了系统端口。

查看系统网络设置-代理-复制这个端口号

然后到在git中

git config --global http.proxy 127.0.0.1:<复制的端口号>
git config --global https.proxy 127.0.0.1:<复制的端口号>

git config --global http.proxy 127.0.0.1:<复制的端口号>

git config --global https.proxy 127.0.0.1:<复制的端口号>