GIt使用笔记大全

Git 使用笔记大全

1. 安装 Git

在终端或命令提示符中,输入以下命令检查是否已安装 Git:

复制代码
git --version

如果未安装,可以从 Git 官方网站 下载并安装适合你操作系统的版本。

2. 配置 Git

首次使用 Git 时,需要配置用户名和邮箱:

复制代码
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
  1. 创建仓库

在本地创建一个新的 Git 仓库:

复制代码
git init

或者克隆一个远程仓库:

复制代码
git clone https://github.com/user/repo.git
  1. 添加文件

添加所有文件:

复制代码
git add .

git add -A
5. 提交更改

提交暂存区的更改到本地仓库:

复制代码
git commit -m "commit message"
  1. 查看状态

查看工作目录和暂存区的状态:

复制代码
git status
  1. 查看提交历史

查看提交历史记录:

复制代码
git log --oneline
  1. 分支管理

创建新分支:

复制代码
git branch <branch-name>

切换到指定分支:

复制代码
git checkout <branch-name>

创建并切换到新分支:

复制代码
git checkout -b <branch-name>

合并分支:

复制代码
git merge <branch-name>
  1. 远程仓库

查看远程仓库信息:

复制代码
git remote -v

添加远程仓库:

复制代码
git remote add origin https://github.com/user/repo.git

推送本地仓库到远程仓库:

复制代码
git push origin <branch-name>

从远程仓库拉取最新更改:

复制代码
git pull origin <branch-name>
  1. 标签

创建标签:

复制代码
git tag <tag-name>

推送标签到远程仓库:

复制代码
git push origin <tag-name>
  1. 解决冲突

当合并分支时发生冲突,需要手动编辑冲突文件,解决冲突后添加到暂存区并提交。

12. 撤销操作

放弃工作目录中的所有修改:

复制代码
git checkout -- <file>

放弃暂存区的修改:

复制代码
git reset HEAD
  1. 查看差异

查看工作目录与暂存区的差异:

复制代码
git diff

查看暂存区与最新提交的差异:

复制代码
git diff --cached
  1. 使用 Git 忽略文件

创建 .gitignore 文件,定义要忽略的文件和目录模式。

15. 删除文件

从仓库中删除文件:

复制代码
git rm <file>
  1. 恢复旧版本

使用 git checkout 切换到之前的提交:

复制代码
git checkout <commit-hash>
  1. 远程分支

查看远程分支:

复制代码
git branch -r

创建远程分支:

复制代码
git push origin <local-branch>:<remote-branch>

删除远程分支:

复制代码
git push origin --delete <remote-branch>
  1. 别名

为常用的 Git 命令设置别名:

复制代码
git config --global alias.[alias-name] [command]

例如:

复制代码
git config --global alias.co checkout
  1. 检查忽略的文件

查看被 .gitignore 忽略的文件:

复制代码
git check-ignore -v <file>
  1. 跟踪文件

开始跟踪新文件:

复制代码
git add <file>

停止跟踪文件但保留文件:

复制代码
git rm --cached <file>
  1. 重置提交

重置到某个提交:

复制代码
git reset <commit-hash>
  1. 恢复已删除的分支

查看已删除的分支:

复制代码
git reflog

恢复已删除的分支:

复制代码
git branch <new-branch-name> <commit-hash>
  1. 交互式重新构建

交互式地修改提交历史:

复制代码
git rebase -i <commit-hash>
  1. 使用子模块

添加子模块:

复制代码
git submodule add <repository-url> <path>

初始化子模块:

复制代码
git submodule init

更新子模块:

复制代码
git submodule update
  1. 推送标签

推送标签到远程仓库:

复制代码
git push origin <tag-name>

推送所有标签:

复制代码
git push origin --tags
  1. 查看分支日志

查看特定分支的提交历史:

复制代码
git log <branch-name>
27. 使用 Git stash

暂存当前工作区的更改:

复制代码
git stash

应用暂存的更改:

复制代码
git stash apply
  1. 查看远程 URL

查看远程仓库的 URL:

复制代码
git remote get-url origin

修改远程仓库的 URL:如果是从https拉取,但是推送想用ssh,则需要改变url

复制代码
git remote set-url origin <new-url>
  1. 创建空仓库

在本地创建一个空的 Git 仓库:

复制代码
git init --bare
  1. 查看合并图

查看分支合并图:

复制代码
git log --graph --oneline --all
相关推荐
春日见6 小时前
拉取与合并:如何让个人分支既包含你昨天的修改,也包含 develop 最新更新
大数据·人工智能·深度学习·elasticsearch·搜索引擎
时代的凡人6 小时前
0208晨间笔记
笔记
今天只学一颗糖6 小时前
1、《深入理解计算机系统》--计算机系统介绍
linux·笔记·学习·系统架构
Elastic 中国社区官方博客8 小时前
如何防御你的 RAG 系统免受上下文投毒攻击
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
Elastic 中国社区官方博客10 小时前
Elasticsearch:交易搜索 - AI Agent builder
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
饭碗、碗碗香11 小时前
【Python学习笔记】:Python的hashlib算法简明指南:选型、场景与示例
笔记·python·学习
艺杯羹11 小时前
Git版本控制深度复盘:从入门到精通的完整指南
git·wpf·版本控制·git学习·git复盘
Wils0nEdwards11 小时前
初中化学1
笔记
摇滚侠11 小时前
IDEA invalidate caches 中每个勾选项是什么
java·elasticsearch·intellij-idea
历程里程碑12 小时前
矩阵----=矩阵置零
大数据·线性代数·算法·elasticsearch·搜索引擎·矩阵·散列表