Git资源

gitlog

Git原理详解及实用指南

git操作区

  • 工作区(coding)
  • 暂存区(after Add)
  • 本地仓库(after Commit)
  • 远程仓库(after Push)

新建代码库

1
2
3
4
# 在此目录新建一个git代码库
git init
# 新建一个目录,将其初始化未git代码库
git init project-name

配置

1
2
3
4
5
6
7
# 显示当前的Git配置
git config --list
# 编辑Git配置文件
git config -e [--global]
# 设置提交代码时的用户信息
git config [--global] user.name "name"
git config [--global] user.email "email address"

增加/删除文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 添加指定文件到暂存区
git add [file1] [file2] ...
# 添加指定目录到暂存区,包括子目录
git add [dir]
# 添加当前目录的所有文件到暂存区
git add .
# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
git add -p
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...
# 停止追踪指定文件,但该文件会保留在工作区
git rm --cached [file]
# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

代码提交

1
2
3
4
5
6
7
8
9
10
11
12
13
# 提交暂存区到仓库区
git commit -m [message]
# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]
# 提交工作区自上次commit之后的变化,直接到仓库区
git commit -a
# 提交时显示所有diff信息
git commit -v
# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
git commit --amend -m [message]
# 重做上一次commit,并包括指定文件的新变化
git commit --amend [file1] [file2] ...

查看信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 显示有变更的文件
git status
# 显示当前分支的版本历史
git log
# 显示commit历史,以及每次commit发生变更的文件
git log --stat
# 搜索提交历史,根据关键词
git log -S [keyword]
# 显示某个commit之后的所有变动,每个commit占据一行
git log [tag] HEAD --pretty=format:%s
# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
git log [tag] HEAD --grep feature
# 显示某个文件的版本历史,包括文件改名
git log --follow [file]
git whatchanged [file]
# 显示指定文件相关的每一次diff
git log -p [file]
# 显示过去5次提交
git log -5 --pretty --oneline
# 显示所有提交过的用户,按提交次数排序
git shortlog -sn
# 显示指定文件是什么人在什么时间修改过
git blame [file]
# 显示暂存区和工作区的差异
git diff
# 显示暂存区和上一个commit的差异
git diff --cached [file]
# 显示工作区与当前分支最新commit之间的差异
git diff HEAD
# 显示两次提交之间的差异
git diff [first-branch]...[second-branch]
# 显示今天你写了多少行代码
git diff --shortstat "@{0 day ago}"
# 显示某次提交的元数据和内容变化
git show [commit]
# 显示某次提交发生变化的文件
git show --name-only [commit]
# 显示某次提交时,某个文件的内容
git show [commit]:[filename]
# 显示当前分支的最近几次提交
git reflog

暂存操作

操作 bash
创建 git stash(git stash save “日志信息”)
查看 git stash list
应用 git stash apply stash@{num}
删除 git stash drop stash@{num}
还原上一个暂存并删除暂存(如无conflict) git stash pop

撤销修改

未提交到暂存区(未add)

1
2
## 如果没有 -- 的话就变成切换分支了
git checkout -- filename.txt

未提交到仓库(未commit)

1
2
3
# 首先用reset来把修改撤回到工作区,再使用checkout命令撤回工作区的修改。这里的reset相当于add的反操作。
git reset HEAD filename.txt
git checkout -- filename.txt

已经提交到仓库

1
2
# --hard表示强制回退,丢弃本地的修改,回退后,该版本号之后的提交都将不可见
git reset --hard xxxxxx

撤销之前某一个提交

1
2
# 撤销的同时,会创建一个新的提交,这是一个安全的方法,因为它不会重写提交历史。
git revert xxxxx

合并commit

1
2
# 如果要撤回目前的commit,可以把它合并到上一个commit中,在出现的两个提交信息的pick改为fixup
git rebase -i HEAD~

分支操作

操作 bash
查看分支 git branch
查看所有分支 git branch -a
在targe分支上创建分支,没有则从当前分支 git branch branch-name target-branch
创建并切换分支 git checkout -b branch-name
合并某分支到当前分支 git merge branch-name
删除分支,只能删参与了合并的 git branch -d branch-name
强行删除 git branch -D branch-name
删除远程分支 git push origin : remote-branch-name

远程仓库操作

操作 bash
克隆 git clone url
添加远程仓库 git remote add name url
删除远程仓库 git remote rm name
拉取 git pull remote-branch-name local-branch-name
推送本地所有分支到远程 git push –all origin
推送到远程同名分支 git push origin local-branch-name
推送本地分支到远程master git push origin local-branch-name:master
把当前本地分支推送并创建到远程 git push origin
检出远程分支 git checkout -b new-local-branch-name origin/remote-branch-name

其他

1
2
# 生成一个可供发布的压缩包
git archive

提交格式

  • feat: 新特性,添加功能

  • fix: 修改bug

  • refactor: 代码重构

  • docs: 文档修改

  • style: 代码格式修改, 注意不是 css 修改

  • test: 测试用例修改

  • chore: 其他修改, 比如构建流程, 依赖管理.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## 跳到之前的分支
git checkout -

# 查看历史
## 每个提交在一行内显示
git log --oneline
## 在所有提交日志中搜索包含[homepage]的提交
git log --all --grep='homepage'
## 获取某人的提交日志
git log --author="yaogerjiang"

## 获取所有操作历史
git reflog
## 重置到相应提交
git reset HEAD@{4}
## ...或者...
git reset --hard <提交的哈希值>

## 本次仓库混乱
git fetch origin
git checkout master
git reset --hard origin/master

## 查看我的分支和master的不同
git diff master..my-branch

## 编辑上次提交
git commit --amend -m "更好的提交日志"

## 在上次提交中附加一些内容,保持提交日志不变git add . && git commit --amend --no-edit

## 空提交 -- 可以用来重新触发CI构建
git commit --allow-empty -m "chore: re-trigger build"

# 清理
git fetch -p

Author

jiangyao

Posted on

2021-06-12

Updated on

2023-02-27

Licensed under