git
DESCRIPTION
The git is source code version control software.
install git, example Ubuntu/Debian.
$ sudo apt install gitConfigure personal information
$ git config --global user.name "Your Name Comes Here" $ git config --global user.email you@yourdomain.example.comCheck personal information
$ git config -l
IMPORTING A NEW PROJECT
init git repositories
$ git init .add files that want to trace
$ git add <files>commit files to git repositories for tracing
$ git commit -m 'VIEWING PROJECT HISTORY
$ git log [--reverse] [file]
$ git show <commit>MANAGING BRANCHES
Review branch
$ git brachCreate new branch from the current tag/commit
$ git branch <new_branch> [tag_or_commint]
or
$ git checkout -b <new_branch> [tag_or_commint]Create new branch from the previous commit of a commit
$ git branch <new_branch> <commint>~1
## such as:
$ git branch slub 81819f0fc828~1Checkout branch
$ git checkout <branch>Delete branch
$ git branch -d <branch>Modify branch name
$ git branch -M <branch>REMOTE
The remote server has github, gitlab, gitee and private git server.
Clone remote server git repositories
$ git clone <remote_url>Push code/tags to remote server
$ git push [--tags] <remote_url> <local_branch>[:<remote_branch>]Pull code/tags from remote server
$ git pull [--tags] <remote_url> <remote_branch>[:<local_branch>]
or
$ git fetch <remote_url> <remote_branch>:<local_branch>PATCH
output <commit> patch only
$ git format-patch -1 <commit>Output the patch of a certain range, such as the patch of commit1~commit3, excluding commit1 patch
$ git format-patch <commit1>..<commit3>output all patches of a file
## such as: mm/slub.c
$ i=0; for line in `git log --reverse --pretty=format:"%h" mm/slub.c`; do i=$[$i+1]; git format-patch -1 --start-number $i $line -o patches/; donesee if the patch is applicable to the current working tree and/or the index file and detects errors. Turns off apply.
$ git apply --check <patch>apply patch to git repositories
$ git am <patch>SUB-MODULES
Add sub-modules into a repository
$ git submodule add <repository URL> [path]
$ git commit -m 'Cloning a repository that contains submodules
## The first method
$ git clone <repository URL>
$ git submodule update --init [--recursive]
## The second method
$ git clone --recurse-submodules <repository URL>Update sub-modules
## The first method
$ cd <sub-modules>
$ git pull
$ cd <main-repository>
$ git add .
$ git commit -m '
## The second method
$ cd <main-repository>
$ git submodule update --remote [sub-modules]
$ git add .
$ git commit -m 'Hooks
git hooks 是 git 的一个重要特性,它让你可以在Git仓库中定义一些自动化的脚本,这些 脚本可以在特定的Git事件(如提交代码、接收代码等)发生时被触发执行。 它们是在 Git仓库目录中的 .git/hooks/ 下的一组可执行文件。
pre-commit触发时机: 在用户输入提交信息之前,执行
git commit命令时触发。功能: 检查即将提交的内容。
使用场景:
运行代码风格检查(如
eslint,pylint,rubocop)。检查是否有调试语句(如
console.log,print)被意外提交。检查是否包含敏感信息(如密码、密钥)。
检查文件命名规范、编码规范等。
prepare-commit-msg触发时机: 在默认提交信息被创建之后,但在编辑器启动让用户编辑信息之前触发。
功能: 修改提交信息。
使用场景:
自动将当前分支名、工单号(JIRA)等信息注入到提交信息模板中。
commit-msg触发时机: 在用户编辑完提交信息之后触发。
功能: 验证提交信息的格式和内容。
使用场景:
强制要求提交信息遵循特定格式。
检查提交信息是否包含必要的工单号或关键字。
确保提交信息描述达到最小长度要求。
post-commit触发时机: 在整个提交过程完成之后触发。
功能: 主要用于通知或其他不影响提交本身的操作。
使用场景:
触发通知(如发送邮件、Slack 消息告知有新提交)。
更新项目文档或生成变更日志草稿。
EMAIL
Shell 终端能够访问 gmail 服务器
安装
git send-email功能,执行$ apt install git-email配置
git send-email功能,参考$ git help send-email的EXAMPLES小节,完成后,就能够使用
$ git send-email进行发送邮件
如果想要给 Linux Kernel 提交 patch,需要按照以下步骤来执行:
修改源码,编译运行,测试,确保能够正常运行 patch 对应的功能
提交源码,如下:
$ git add xxx $ git commit -s -m ' module name: fix some bugs why and how '生成 patch
$ git format-patch -M origin/master -o patch/将与 origin/master 不同的 commit 都生成 patch,存储在 patch/ 目录下
可选:
--cover-letter:生成以0000-为前缀的邮件封面,需要指定标题、简介。-v <version>:指定新版本 注意:第二版本后的邮件封面,除了需要指定标题、简介外, 还需要写 版本改变日志、之前的版本在https://lore.kernel.org的链接--base=auto:显示是基于哪一个 commit 进行修改--subject-prefix="RFC PATCH": 生成RFC PATCH前缀,默认是PTACH前缀
通过
./scripts/checkpatch.pl xxx.patch检查 patch 格式是否符合要求通过
./scripts/get_maintainer.pl xxx.patch获得 maintainer 邮箱地址通过
git send-email --to xxx --cc xxx xxx.patch来发送 patch 给 maintainer。 如果需要发送给多个邮箱地址,用,作为分隔符。如果是 patchset,发送时选择 a[ll]
TIPS
tag
## view
$ git tag
## create
$ git tag <version>look for string
$ git grep "string"compare differ branch
$ git diff <branch_1> <branch_2> [files_path_or_dir]let's some commit of other branch to current branch
$ git cherry-pick <commit>get files of some commit of other branch
$ git checkout <commit> <file name>When you develop a feature, commit multiple changes through git commit. If you want to merge this change into a single commit, you may do so through git rebase
$ git log
commit 20b6f52f5d70a07117f6d11b2902d72f69ff1ae5 (HEAD -> test)
add: 1
commit f845618b13247321e7b3fcd27fd94d0392dbe6f9
3
commit 373f063d4f68a1cdec3b6b29676d4203a86b4387
2
commit b2dba726e7606b9f5374218c236c4e2d5efb7877
Init
# 1. Select the range that you want to merge, b2dba72~20b6f52(exception b2dba72)
$ git rebase -i b2dba72 20b6f52
or
$ git rebase -i b2dba72
or
$ git rebase -i HEAD~3
pick 373f063 2
pick f845618 3
pick 20b6f52 add: 1
## 2. Modify the above information, rearrange, and merge commit, such as rearranging 20B6f52 after 373f063 and squash to 373f063
pick 373f063 2
squash 20b6f52 add: 1
pick f845618 3
## 3. Then save and exit, change the commit information, and save the exit.
$ git log
commit 4612f3b2bf08ef705b2b70dc8f2e1d8f14023a9b (HEAD)
3
commit c02e774166119ff4830b5a2f4db48b97b74dd290
2
test: add: 1
commit b2dba726e7606b9f5374218c236c4e2d5efb7877
InitThe remote repository has new commits, but the local repository also has new commits. How to synchronize the new commits of the remote repository to the local repository?
$ git rebase <branch>When we need to actively merge new feature branches
$ git merge <branch>Displays the commit of each line of a file
$ git blame <file>
or
$ tig blame <file>当我们想要知道 commit A 是在哪一个新merge feature合入时, 如何找到对应的merge commit ?
https://blog.csdn.net/qq_39734650/article/details/116658540
## ~/.gitconfig
[alias]
find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'"
show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"
$ git find-merge <commit A>当我们想要研究某一个新merge feature时,merge commit 会显示feature commit范围, 比如 (A,F],其中不包括A,包括F。如何显示此feature所有commit ?
$ git log --online --graph <merge commit>
or
$ tig <merge commit>显示 commit A 是在哪一个版本被引入?
$ git tag --sort taggerdate --contains <commit A>将一个已经 commit 的 patch 拆分成多个 patch,如下:
$ git rebase -i <prev_commit>
<pick to edit> <commit>
$ git reset HEAD^
$ git add --patch <file>
$ git commit -s -m '
$ git rebase --continue查看某个版本的文件内容,如下:
$ git show <hash>:<file>查看 [HEAD, v6.9) 范围之间的所有合并提交,如下:
$ git log --oneline --merges HEAD...v6.9查看 [HEAD, v6.9) 范围之间的所有真正提交,如下:
$ git log --oneline --no-merges HEAD...v6.9查看某个 commit xxx 的上一个提交,如下:
$ git show xxx^
## 查看 HEAD 的上一个提交
$ git show HEAD^查看文件 mm/vmscan.c 第 1169 行的提交历史,如下:
$ git blame -L 1169,1169 mm/vmscan.c
2 years ago Matthew Wilcox d92013d1e5e47│1169│ references = folio_check_references(folio, sc);
$ git blame -L 1674,1674 mm/vmscan.c d92013d1e5e47^
11 years ago Minchan Kim 02c6de8d757cb│1674│ references = page_check_references(page, sc);查看文件 mm/vmscan.c 第 1169~1190 行,在 v6.6~v6.12 的提交历史,如下:
$ git log v6.6..v6.12 -L1169,1190:mm/vmscan.cLast updated
Was this helpful?