Featured image of post GIT笔记

GIT笔记

命令

从 git 服务器拉取完整仓库代码

1
git clone xxx.git

配置开发者用户和邮箱,代码每次提交包含配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
git config user.name xxx
git config user.email xx@xx.com

cat .git/config # 查看配置

# 配置代理
git config --global http.proxy 127.0.0.1:7890
git config --global https.proxy 127.0.0.1:7890

# 查看代理
git config --global --get http.proxy
git config --global --get https.proxy

# 取消
git config --global --unset http.proxy
git config --global --unset https.proxy

查看文件变动状态

1
git status

添加文件变动到暂存区

1
2
3
4
5
6
7
git add xxfile
git add . # 添加所有文件

git add -i # 进入交互式
# patch: 显示文件当前内容与本地版本库差异,决定是否添加修改到暂存区
# update: 列出工作区 修改 或 删除 的文件列表
# diff: 比较暂存区文件和本地版本库的差异

此时.git/objects中加入xx/yyyyyyyyy,可通过git cat-file -p xxyyyyyyyyy查看添加的文件内容

列出将要被提交的文件

1
git commit -n

提交文件变动到版本库,若为其他分支会获取一个url用于pull request

1
2
3
git commit -m "description" # 会创建新的tree、blob、commit object

# --amend 只添加 修改 和 删除 的文件到本地版本库,修改最近一次commit信息,修改后commit id变化

查看commit添加的内容

1
git ls-files -s

删除暂存区的内容

1
git rm --cached -r file

修改最近3个commit

1
git rebase -i HEAD~3

查看悬空的commit

1
git fsck --lost-found

删除不需要的object,且打包压缩object减少仓库体积

1
2
3
# 先设置reflog防止无操作后数据丢失
git reflog expire --expire=now --all
git gc prune=now # 修剪多久前对象,默认2周前

重命名文件并添加变动到暂存区

1
git mv src.md dest.md -f # -f 强制

从工作区和暂存区移除文件并添加变动到暂存区

1
git rm xx.md

将本地的代码改动推送到服务器

1
git push origin branch_name # orgin: git 服务器地址

服务器最新代码拉取到本地并与本地代码合并,等价于git fetch + git merge

1
2
git pull origin brach_name # main
git pull --rebase # 等价于 git fetch + git rebase

版本提交记录

1
git log # J下翻, K上翻, Q退出

标记里程碑

1
2
3
git tag publish/0.0.1
git tag -a # 创建附注标签
git push origin publish/0.0.1

分支命令

1
2
3
4
5
6
git branch xxx # 创建
git branch -m old_name new_name # 重命名
git branch -a # 查看本地版本库和远程版本库上的分支列表
git branch -r # 远程版本库的分支列表
git branch -d xxx # 删除
git branch -vv # 查看带有最后提交id、最近提交原因等信息的本地版本库分支列表

切换分支

1
2
3
4
5
6
# 切换到某分支 -b 创建并切换
git checkout xxx 
# 创建一个全新的,完全没有历史记录的新分支,必须commit操作后才真正成为一个分支
git checkout --orphan new_branch 
# 比较两个分支间的差异内容,并提供交互式的界面来选择进一步的操作
git checkout -p other_branch

合并其它分支到当前分支

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 默认合并,分支中的各个节点都加入到master中
git merge
# 将待合并分支上的 commit 合并成一个新的 commit 放入当前分支,适用于待合并分支的提交记录不需要保留的情况
git merge --squash
# 执行正常合并,在 Master分支上生成一个新节点
git merge --no-ff
# 没有冲突的情况下合并,git自动生成提交原因
git merge --no-edit

# fast-forward:线性不创建新merge结点,命令`--ff-only`
# three-way merge:三方合并产生新merge结点,命令`--no-ff`

栈:保存当前修改或删除的工作进度

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
git stash # 将未提交文件保存到Git栈中
git stash list # 查看栈中保存的列表
git stash show stash@{0} # 显示一条记录
git stash drop stash@{0} # 移除一条记录
git stash pop # 检出最新一条记录移除
git stash apply stash@{0} # 检出一条记录
git stash brach new # 检出最近一次记录并创建一个新分支
git stash clear # 清空栈里所有记录

git stash create # 为当前修改或删除文件创建自定义栈,返回一个ID
git stash store ID # 栈真正创建一个记录,文件并未从工作区移除

比较工作区、暂存区、本地版本库间文件差异

image-20250122215003787
1
git diff --stat

远程仓库

1
2
git remote -v # 列出已存在的远程分支
git remote add origin https://github.com/xx/xx.github.com.git # 添加一个新远程仓库

将远程版本库的最新更新取回到本地版本库,不会执行merge操作,会修改refs/remote内分支信息

1
git fetch origin xx/0.0.1

ssh免密配置访问

1
2
ssh-keygen -t ed25519 -C "email@example.com"
# 秘钥公钥在~/.ssh/id_ed25519及id_ed25519.pub中

.gitignore中存储需要被忽略而不推送到服务器的文件

image-20250115214715037

object

  • commit:存储提交信息,一个 commit 对应唯一版本的代码,可找到 tree ID
  • tree:存储文件目录信息,可获取不同 blob ID
  • blob:存储文件内容
  • tag

Refs

Refs中存储对应的commit ID

refs/heads 前缀表示分支,refs/tags 前缀表示标签

研发

github

  • 一个主干分支,基于Pull Request往主干分支提交代码
  • owner外用户Fork创建自己仓库进行开发
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计
Caret Up