git
强烈推荐一本很不错的git书籍:https://git-scm.com/book/zh-tw/v2
常用git命令
1.git config一些配置
git config --global user.name "Zhongshan.Zhou"
git config --global user.email Zhongshan.Zhou@xxx.com
git config --global credential.helper store
2.git init
git init
git add .
git commit -m "Initial commit"
3.git log 查看某个文件的修改记录
git log --pretty=oneline forward.c
某时间之后:git log --since="2020-06-08" ./ > ./my_git_log
4.git show 查看某个commit id的具体信息
git show 671ff15f2d8cb78133c4fea6b0f5270adb992c66
git show --name-only commit_id :只列出改动的文件,不显示具体的修改
5.git reset
5.1 回退到某个指定的commit 点
git reset --hard commit_id
Ie: git reset --hard c137080c6c3276de8c4a7f6190bca805bdafca1e
5.2 以当前head为相对起始点,回退修改
git reset --soft HEAD^
HEAD^ 表示上一个版本,即上一次的commit,也可以写成HEAD~1
如果进行两次的commit,想要都撤回,可以使用HEAD~2
5.3 备注:hard, soft, mixed的区别
--hard
删除工作空间改动代码,撤销commit,撤销git add .
--soft
不删除工作空间改动代码,撤销commit,不撤销git add .
--mixed
意思是:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作
这个为默认参数,git reset --mixed HEAD^ 和 git reset HEAD^ 效果是一样的。
连接:https://www.cnblogs.com/lfxiao/p/9378763.html
https://cloud.tencent.com/developer/article/1582800
6.git stash
6.1 git stash save "message":
对工作区(没有add的)和暂存区(add之后的)的状态进行保存
save可以添加保存时的备注。示例:git stash save "完成user接口"
6.2 git stash list:显示备份列表。
6.3 git stash show stash@{0}
显示某个指定stash的内容,包括更改的文件列表。
git stash show -p stash@{0}:加了-p option后,就是具体的每一处改动。

6.4 组合git stash apply + git stash drop
不太建议使用git stash pop,因为git stash apply并不会讲stash list里的那个stash删除掉,等确认代码stash恢复没有问题后,再进行删除git stash里面的内容
git stash apply stash@{1}:将某个stash号恢复出来,并且不删除掉这个stash点
git stash pop:不使用任何参数,会恢复最近一次的备份文件(也就是stash list中的stash@{0}),并将恢复的工作进度从存储的工作进度列表中清除。
git stash drop stash@{0}:删除掉某一个stash点
6.5 git stash clear
删除所有存储的进度从git stash中恢复 指定的文件
6.6 git checkout --patch stash@{0} path/to/file
连接:https://stackoverflow.com/questions/1105253/how-would-i-extract-a-single-file-or-changes-to-a-file-from-a-git-stash
7.git clean -xfd ./
-x:By default, the git clean command will only remove untracked files that are not ignored. Any file that matches a pattern in your .gitignore or other ignore files will not be removed. If you want to remove those files too, such as to remove all .o files generated from a build so you can do a fully clean build, you can add a -x to the clean command.
把.gitignore里的文件也删除。
-f:The -f means force or "really do this".
真的做这件事情,而不是像-n那样只是告诉你将要删除的文件。
-d:removes any files and also any subdirectories that become empty as a result.
把untrack的文件夹也删掉。
-n:If you ever want to see what it would do, you can run the command with the -n option, which means “do a dry run and tell me what you would have removed”.
-e: git clean -xfd -e "build" 在git clean时忽略某个文件夹
连接:https://git-scm.com/book/id/v2/Git-Tools-Stashing-and-Cleaning#_git_clean
8.git blame file_name
这样,我们就可以知道commit ID了,然后使用命令:git show commitID来看
9.git status -uno
基本用法-u[<mode>],no表示 不显示untracked files,对untracked的file采取不显示的行为。
连接:https://git-scm.com/docs/git-status
10.git cherry-pick
git cherry-pick 7105a91383d: cherry pick 某个commit hash(11位):
git cherry-pick --abort:在上步骤cherry pick之后,如果出现了merge confict,想取消此次cherry-pick动作。

11.git format-patch
使用举例:
生成patch文件:git format-patch -1 6c8694b
应用patch文件:git am <patch_file>
其中-1 表示1个commit点,6c8694b 是具体的commit号。
如果我们想要多个commit,可以将-1改成-n,n表示你想要的这个commit(包含)+ 此commit之前的(n-1)个commit。
比如如下这个使用实例:git format-patch -11 58e62caacbaaa2990c6e22e9ce8eaa89084a52b3 会生成11个patch。

12.git apply && 更加通用的patch命令
12.1 使用git apply命令
git apply -p1 --directory=boot/uboot ./uboot_boot_time_optimize.patch
其中-p1里面的1的目的是为了去除.patch文件中的patch路径的第一个字符串,虽然是a/ b/,但我们也认为是路径的一部分。想要去除几个,就-pN
如果是-p1,对于git apply是不需要的。
--directory=boot/uboot 由于别人提供的patch的路径是a/u-boot-2022.10/configs/,而我们本地的仓库路径是boot/uboot/u-boot-2022.10/configs/xxx-nand_defconfig,所以加上前缀boot/uboot

所以整个命令的完整语义是 去除patch中的路径的第一个字符串,并添加相对路径boot/uboot以适配我们本地的代码路径。
12.2 使用patch命令
cd path/to/your/repo
patch -p1 -d boot/uboot < ./uboot_boot_time_optimize.patch
含义和上面一样,唯一的区别就是-p1 即使是1,也不能省去
13.git clone
git clone --recurse-submodules -b release/CNCA_S311 ssh://git@sourcecode.com:7999/g3n/fvg3_lfs.git ./
git submodule update --init --recursive
git lfs update
git lfs pull
14.git difftool 弹出图形化界面
13.1 首先进行设定git config --global diff.tool kdiff3
13.2 使用方法:git difftool CMakeLists.txt
13.3 如果想设定成beyond compare, 按如下操作
# Set Beyond Compare as the difftool
git config --global diff.tool bc
git config --global difftool.bc.path /usr/bin/bcompare
git config --global difftool.bc.trustExitCode true
# Set Beyond Compare as the mergetool
git config --global merge.tool bc
git config --global mergetool.bc.path /usr/bin/bcompare
git config --global mergetool.bc.trustExitCode true
15.git commit --amend 修改最后一次提交的commit log
14.1 git commit --amend
14.2 按住ctrl+x: ^X Exit
14.3 输入Y

浙公网安备 33010602011771号