一、命令
1. 修改提交的标签或添加修改
xxxxxx@srv-pad-compile5:$ git commit –amend
注:
(1)只能修改最近的一个提交。编辑标签(i),保存退出(Esc+wq)。
(2)该命令也可以将暂存区里所有的修改(绿色)提交到最近那个版本库中。
如果上一个提交到服务器的修改没有被review,则直接git push origin 即可,使用的是同一个commit-ID。
2. 版本回退指令
xxxxxx@srv-pad-compile5:$ git reset HEAD^
3. 代码审查工具(个别项目有)
xxxxxx@srv-pad-compile5:$ git diff | ./scripts/checkpatch.pl --no-signoff
//脚本自动检查编码规范
--------------------------------------------------------------------------------------------------------------------------------
-:300: WARNING:LONG_LINE: line over 80 characters
#300: FILE: subsys/usb/class/cdc_acm.c:374:
+ usb_device_register_descriptors(cdc_acm_usb_fs_descriptor, cdc_acm_usb_hs_descriptor);
-:352: WARNING:LINE_SPACING: Missing a blank line after declarations
#352: FILE: subsys/usb/class/cdc_acm.c:870:
+ char *tmp;
+ lenofstr = strlen(str);
-:445: WARNING:LONG_LINE: line over 80 characters
#445: FILE: subsys/usb/class/cdc_acm.c:963:
+ ret = usb_write(CONFIG_CDC_ACM_BULK_IN_EP_ADDR, fmt_str, strlen(fmt_str), &wrote);
-:447: WARNING:LONG_LINE: line over 80 characters
#447: FILE: subsys/usb/class/cdc_acm.c:965:
+ SYS_LOG_ERR("Failed to write EP 0x%x", CONFIG_CDC_ACM_BULK_IN_EP_ADDR);
- total: 0 errors, 4 warnings, 623 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
Your patch has style problems, please review.
NOTE: Ignored message types: AVOID_EXTERNS BRACES CONFIG_EXPERIMENTAL CONST_STRUCT DATE_TIME FILE_PATH_CHANGES MINMAX NETWORKING_BLOCK_COMMENT_STYLE PRINTK_WITHOUT_KERN_LEVEL SPLIT_STRING VOLATILE
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
--------------------------------------------------------------------------------------------------------------------------------
4. 代码分支查看指令
xxxxxx@srv-pad-compile5:~/prj/$ git branch –a //查看所有分支
//查看指定分支的提交记录:只显示该分支最近的两条提交记录
xxxxxx@srv-pad-compile5:~/prj/$ git log -2 remotes/origin/tb_gl5120_dev
//更新服务器代码到本地版本库
xxxxxx@srv-pad-compile5:~/prj/$ git fetch origin
//查看本地分支
xxxxxx@srv-pad-compile5:~/prj/$ git branch
//显示工作目录和暂存区的状态
xxxxxx@srv-pad-compile5:~/prj/$ git status
On branch tb_dev_v1.9_ble Your branch is behind 'origin/tb_dev_v1.9_ble' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: drivers/usb/device/aotg_udc.c modified: samples/actions_sdk_demo/usb_audio_dongle/prj.conf modified: samples/actions_sdk_demo/usb_audio_dongle/src/Makefile modified: samples/actions_sdk_demo/usb_audio_dongle/src/main.c modified: samples/hello_world/Makefile modified: subsys/usb/class/audio/Makefile modified: subsys/usb/class/hid/core.c modified: subsys/usb/usb_descriptor.c modified: subsys/usb/usb_device.c Untracked files: (use "git add <file>..." to include in what will be committed) .zephyrrc no changes added to commit (use "git add" and/or "git commit -a") |
5. 创建本地代码分支
//取远程分支并创建一个本地分支
//格式:git checkout –b 本地分支名 origin/远程分支名
例:
xxxxxx@srv-pad-compile5:~/prj/$
git checkout -b tb_dev remotes/origin/tb_dev
Checking out files: 100% (3256/3256), done. Branch tb_dev set up to track remote branch tb_dev from origin. Switched to a new branch 'tb_dev' |
//查看本地分支
xxxxxx@srv-pad-compile5:~/prj/$ git branch
tb_dev_v1.9_ble * tb_dev //多出了刚创建的本地分支 |
6. 切换本地分支
xxxxxx@srv-pad-compile5:~/prj/ATS350B$ git checkout tb_dev_v1.9_ble
//切换到tb_dev_v1.9_ble分支
//语法:git checkout 分支名
查看当前位于哪个分支:git branch
分支前会有星号! |
切换到刚刚自己创建的本地分支:git checkout xxx
7. 跟踪文件的改动
使用git status可以知道当前工作目录下,哪些文件有改动、哪些文件被删除。
xxxxxx@srv-pad-compile5:~/prj/samples/recorder_app$ git status |
控制台打印信息:
On branch dev_v1.9
Your branch is up-to-date with 'origin/dev_v1.9'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
Modified(有改动的): ../../arch/Makefile
modified: ../../drivers/audio/Kconfig
modified: ../../drivers/audio/andes/audio_in_acts_andes.c
modified: ../../drivers/audio/andes/audio_inner.h
………………………………………………………………………………
Untracked(无法跟踪的、或者是已被删除、或者是新创建的文件或者目录) files:
(use "git add <file>..." to include in what will be committed)
../../arch/Makefile.bak
../../arch/csky/soc/actions/andes/soc_memctrl.h.bak
../../drivers/audio/Kconfig.bak
../../drivers/audio/andes/audio_in_acts_andes.c.bak
所以,对于新创建的文件或者目录,要先执行指令git add。
查看文件具体改动的内容:git diff + 文件名
二、问题集
1. windows平台下Git 使用reset命令出现More?
E:\zsdk\sdk-acts>git reset HEAD^ More? More? fatal: ambiguous argument 'HEAD ': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' E:\zsdk\sdk> E:\zsdk\sdk>git branch * tb_dev_sdk |
解决办法:因为cmd控制台中换行符默认是^,而不是\ ,所以它的more?的意思是问你下一行是否需要再输入,而^ 符号就被当做换行符而被git命令忽略掉了。 加一个^即可解决:git reset --hard HEAD^^。如果不是在CMD命令行中使用git则不会有这个问题(git bash/powershell)。
三、提交代码被驳回之后
1. Abandoned操作
是否需要abandoned?如果这个提交确认是不需要的,那就彻底abandoned掉。
代码没有被review(审查)和merge(合并)之前,在网页上直接进行Abandoned(抛弃)
如果只是需要修改提交的内容,或者新增修改到这一个提交(当前也只有这一个提交),那么可以不用abandoned掉,这就要用到命令:git commit --amend
2. 将新的修改直接添加到最近一次的本地版本库
命令:
git add xxx
git commit --amend
这样的话,Change-ID都不用修改。
3. 提交代码到远程服务器
xxxxxx@srv-pad-compile5:~/stub/$ git branch -a * dev_v1.9 remotes/origin/BF1_TAG_1900_190717 remotes/origin/dev_v1.9 remotes/origin/dev_v1.9_new_dsp remotes/origin/dev_v1.9 ……………………………………………………………………………………………………… //本地分支名可以是任意的,前面的参数是本地分支名,后面的参数是远程分支名 xxxxxx@srv-pad-compile5:~/stub/$ git push origin dev_v1.9:refs/for/dev_v1.9 Counting objects: 7, done. Delta compression using up to 24 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (7/7), 626 bytes | 0 bytes/s, done. Total 7 (delta 6), reused 0 (delta 0) remote: Resolving deltas: 100% (6/6) remote: True remote: Processing changes: new: 1, refs: 1, done remote: To ssh://192.168.4.4:29418/ZH/a/AT * [new branch] dev_v1.9 -> refs/for/dev_v1.9 |
4. 如何确认代码已成功提交
方式1:通过代码管理平台
网页上有merge信息,All----->Merge
方式2:查看远程分支的提交日志
//先更新本地代码(同步) xxxxxx@srv-pad-compile5:~/stub/$ git fetch origin //有打印,有最新的提交信息 remote: Counting objects: 71, done remote: Finding sources: 100% (28/28) remote: Total 28 (delta 22), reused 28 (delta 22) Unpacking objects: 100% (28/28), done. From ssh://192.168.4.4:29418/ZH/a/AT //这里是有最新提交的分支 9f6acd33d3..0210820518 dev_v1.9 -> origin/dev_v1.9 6266921565..32775467cd tb_dev_libratone -> origin/tb_dev_libratone xxxxxx@srv-pad-compile5:~/stub/$ xxxxxx@srv-pad-compile5:~/stub/$ xxxxxx@srv-pad-compile5:~/stub/$ git branch -a * dev_v1.9 remotes/origin/BF1_TAG_1900_190717 remotes/origin/BF1_TAG_1900_180727 remotes/origin/BF1_TAG_1900_181026 remotes/origin/BF_TAG_1900_190329_V1.0.0 remotes/origin/BF_TAG_1900_190923_V2.0.0 remotes/origin/dev_v1.9 remotes/origin/dev_v1.9_new_dsp remotes/origin/dev_v1.9 //同步之后,就可以查看某个分支提交的日志了 xxxxxx@srv-pad-compile5:~/stub$ git log -2 remotes/origin/dev_v1.9
|
5. 另一种方式
另一种方式是在网页上abandoned掉,然后添加修改到本地版本库,再修改Change-ID,再上传。这种方式工作量多,重复,且Change-ID发生了改变。当然,如果有多个提交,同时都需要修改掉,因为各个提交之间又有依赖,所以不得不全部abandoned掉的情况下,还是得采取这种方式,重新修改并提交代码。
四、补丁文件的提取和使用
如何在本地代码库打上补丁。
1. 拿到补丁文件,拷贝到源码根目录
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$ ls
0001-drivers-usb-aotg-fixing-race-conditions.patch Kconfig Makefile.inc boards ext mips_env.sh subsys
0002-usb-cdc_acm-initialize-and-fixing-for-state-check.patch Kconfig.zephyr Makefile.test build.sh genpatch.sh misc tests
CODEOWNERS LICENSE README.rst doc include release.sh zephyr-env.sh
CONTRIBUTING.rst MAINTAINERS arch drivers kernel samples
Kbuild Makefile auto_build.sh dts lib scripts
2. 打补丁
命令格式:git am + 补丁文件名 |
//打第一个补丁
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
git am 0001-drivers-usb-aotg-fixing-race-conditions.patch
Applying: drivers: usb: aotg: fixing race conditions
//打第二个补丁
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
git am 0002-usb-cdc_acm-initialize-and-fixing-for-state-check.patch
Applying: usb: cdc_acm: initialize and fixing for state check
注意:要按照顺序打补丁,否则可能会有冲突。
成功打完补丁,查看新增的commit标签
@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
i@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$ git log
/*****************原作者的修改*****************************************/
commit 702b9127cc6e552a4c9dfb0ce4308b697db6dfb0
Author: xxxxxx<xxxxxx@xxxxxx-semi.com>
Date: Wed Jan 13 20:25:21 2021 +0800
usb: cdc_acm: initialize and fixing for state check
Change-Id: I61ebe5bda2b9e192d48ea5aace114fdf93a545f2
commit 60354ff56c33f04367cb3b15275097e324a0b3e3
Author: xxxxxx <xxxxxx@xxxxxx-semi.com>
Date: Wed Jan 13 20:18:59 2021 +0800
drivers: usb: aotg: fixing race conditions
1. using irq_lock to protect potential race conditions
case 1: dc read/write and deteach.
case 2: hc submit and disconnect.
2. check address 32-bit alignment for DMA.
3. set busy for short packet when epin DMA complete.
Change-Id: Ifd301afc32744589044d5f32249fed977194a48e
/****************************************************************************/
commit b5a4ab1d17867bb12984e73de0697e693bdda688
Author: xxxxxx <xxxxxx@xxxxxx-semi.com>
Date: Wed Jan 13 20:02:08 2021 +0800
samples: usb_cdc_acm: change the DMA source buffer to 4-byte aligned.
Change-Id: I60ed1509cb595ec62c4769d65e14826663420ed7
3. 打补丁后提交修改
按常规操作即可:git push origin xxxxxx,原作者的修改信息会被提交到服务器。
4. 如何生成自己的补丁文件
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$ git log
commit 702b9127cc6e552a4c9dfb0ce4308b697db6dfb0
Author: xxxxxx<xxxxxx@xxxxxx-semi.com>
Date: Wed Jan 13 20:25:21 2021 +0800
usb: cdc_acm: initialize and fixing for state check
Change-Id: I61ebe5bda2b9e192d48ea5aace114fdf93a545f2
commit 60354ff56c33f04367cb3b15275097e324a0b3e3
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
//生成补丁文件命令格式:git format-patch -1 + commit-id
//参数-1生成第一个标签文件
xxxxxx@srv-pad-compile5:~/dev_v1.9_woodpecker/commit_new$
git format-patch -1 702b9127cc6e552a4c9dfb0ce4308b697db6dfb0
//对应的是补丁文件的前缀
0001-usb-cdc_acm-initialize-and-fixing-for-state-check.patch
该补丁文件可以提供给别人使用,或者自己可以使用同样的命令(git am xxx),在同一个项目上打上补丁。