GIT使用笔记

Posted by wsxq2 on 2022-03-16
TAGS:  FROM_DOCXGIT

本文最后一次编辑时间:2022-03-16 15:25:18 +0800

安装

简单安装

1
2
yum install git -y # for centos
apt install git -y # for ubuntu

安装最新版本

参考Install Latest Git ( Git 2.x ) on CentOS 7 | ComputingForGeeks

常用高级用法

本部分内容使用的版本:1.8.3.1

push

1
2
3
4
git push --set-upstream origin CSos-V3.2-fix14755
git push origin --delete abc
git push origin --tags
git push -f

cherry-pick

1
2
3
4
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a..adsfsas
git cherry-pick eb83bd6151e836a388a0232a5f520ff61fe4149a^..adsfasdf
git cherry-pick -m 1 <commit> # -m 的使用说明请参见 git cherry-pick --help

show

1
2
git show efe5822
git show 07bd0aa:isos/platform/mcp/fpdetect.c >fpdetect_07bd0aa.c

log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git log /home/wangzhiqiang/SecOS-main/isos/platform/scripts/Makefile.common
git log CSos-V3.1
git log c626201..2458bf0
git log c626201^..2458bf0
git log --graph c626201
git log --author=wangzhiqiang
git log --format='xxx'
git log --minimal
git log --name-only
git log --name-status c626201
git log --oneline
git log --stat
git log --summary
git log --pretty=xxx
git log --patch # -p/-u
git log --decorate # show tags
git log --decorate --all --oneline --graph # from Newbie: git log to show tags as well?

branch

1
2
git branch --contains efe5822
git branch -D abc

clean

1
2
sudo git clean -nd .
sudo git clean -fd .

fetch

1
git fetch origin CSos-V3.1

rev-parse

1
git rev-parse --abbrev-ref HEAD

diff

1
2
git diff --name-status c626201 2458bf0
git log --name-only

add

1
2
3
4
git add a.c
git add .
git add -A .
git add -u .

commit

1
2
3
git commit a.c
git commit .
git commit --amend --author="Author Name <email@address.com>" --no-edit # 修正上一次的commit message

reset

1
2
3
git reset HEAD .
git reset <commit>
git reset --hard origin/master # 重置到远程分支最新版本

blame

1
git blame ./ipv4/appidentify/dpi/dpi_tls.c

whatchanged

git whatchanged -p v2.6.12.. include/scsi drivers/scsi

Show as patches the commits since version v2.6.12 that changed any file in the include/scsi or drivers/scsi subdirectories

git whatchanged –since=”2 weeks ago” – gitk

Show the changes during the last two weeks to the file gitk. The “–” is necessary to avoid confusion with the branch named gitk

——引用自man git-whatchanged

rebase

1
2
3
4
$ git checkout experiment
$ git rebase master
$ git checkout master
$ git merge experiment

详情参见以下链接:

tag

1
git tag <tag name> <tag name>^{} -f -a # 修改某个tag的message

describe

1
2
git describe --tags --abbrev=0 #获取latest tag
git describe --abbrev=0 # To get the most recent annotated tag

archive

1
git archive --format zip --output /full/path/to/zipfile.zip master

最佳实践

这里的“最佳实践”是指一些比较好的使用习惯

Commit message 和 Change log

参见以下链接:

遇到过的问题

git show

查看指定提交的指定文件的内容?

1
git show REVISION:/path/to/file

详情参见How to view a file at a specific commit in git? - SysTutorials

git stash

How can I git stash a specific file?

参见How can I git stash a specific file? - Stack Overflow

git log

sort by date?

参见Can you order git log by commit timestamp? - Stack Overflow

How to configure ‘git log’ to show ‘commit date’?

git log - How to configure ‘git log’ to show ‘commit date’ - Stack Overflow

Why git AuthorDate is different from CommitDate?

Why git AuthorDate is different from CommitDate? - Stack Overflow

删除远程分支?

git push origin –delete abc

git format-patch

How can I generate a Git patch for a specific commit? - Stack Overflow

git commit

git commit - Removing multiple files from a Git repo that have already been deleted from disk - Stack Overflow

修订记录

修订时间 修订人 版本 说明
2021-02-03 wsxq2 1.0 初稿