「Git」- 提示和技巧 @20210123

# git help -a

git list all available commands

显示全部可用的GIT命令,或者执行 ls -l /usr/lib/git-core/ 命令。

# git commit

修改在提交信息中的邮箱

How can I change the author (name / email) of a commit? | Learn Version Control with Git

git commit --amend --author="k4nz <[email protected]>"

# git tag

CMD DESC
git tag <tagname> 创建标签
git tag <tagname> -a 打开编辑器,添加详细的注解
git push origin --tags 全部TAG推送到远程的仓库中
git push origin <tagname> 将特定TAG推送到远程分支
git diff branchA...branchB 对比分支间的差异

获取 TAG 所指向提交的哈希值(SHA1)

git - Get the commit hash for a tag - Stack Overflow

git rev-parse "v1.25.0^{}" # git rev-parse "tag^{}"

git rev-parse "v1.25" # 用于获取 TAG 自身的 SHA1

# git checkout

复制其他分支的文件

How do I copy a version of a single file from one git branch to another?
Hard reset of a single file
git: checkout files from another branch into current branch (don't switch HEAD to the other branch) - Stack Overflow

git checkout master "path/to/file.txt"
git checkout origin/master "path/to/file.txt"
git checkout "commit-id" "path/to/file.txt"

使用 branchSrc 覆盖 branchDst(复制分支的全部文件)

git checkout "other-branch-name" -- .

# git push

同时推送到多个远程仓库

bjmiller121/multiple-push-urls.md

#(1)添加额外的远程仓库:
git remote set-url --add --push origin https://example.com/demo/foo.git

#(2)然后推送到远程仓库(此时可以推送到两个远程仓库):
git push origin master

#(3)查看已有的远程仓库:
git remote -v

#(4)删除某个推送地址(有时候还是编辑配置文件更快)
git remote set-url --delete --push origin https://example.com/demo/foo.git

# git stash

只暂存部分文件

Stash only one file out of multiple files that have changed with Git? - Stack Overflow

# 1) 将无需暂存的文件保存到索引中
git add 'file1'

# 2)执行 stash 命令,并保存索引
git stash --keep-index

# git submodule

添加子模块

git - How do I add a submodule to a sub-directory? - Stack Overflow
How can I specify a branch/tag when adding a Git submodule? - Stack Overflow

git submodule add "<repo>" "path/to/folder"

git submodule add -b "<branch>" "<repo>" "path/to/folder" 

# 注意事项,-b 选项不支持指定 Tag,否则会提示:
# git submodule fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it

#######################################
## 如果需要添加特定分支,可以使用如下命令:
#######################################
git submodule add "<your repo>" path/to/folder
cd path/to/folder
git checkout v1.0.2
cd -
git add path/to/folder
git commit -m "moved submodule to v1.0.2"
git push

删除子模块

How effectively delete a git submodule.

有很多不同的删除方法,似乎没有统一的做法:

mv path/to/module path/to/module.backup

git submoduel deinit --force -- path/to/module
rm -rf .git/modules/path/to/module
git rm --force path/to/module
# Note: path/to/module (no trailing slash)

检出子模块仓库

在 git clone 后,执行如下命令:

git submodule update --init --recursive

合并:两个无关分支的合并

Git refusing to merge unrelated histories on rebase - Stack Overflow

使用 --allow-unrelated-histories 选项,可以合并两个无关分支。

# git rebase

很并多个提交为单个提交(以简化变更历史)

rebase - Squash my last X commits together using Git - Stack Overflow

git rebase -i '<after-this-commit>'
# <after-this-commit> is commit X+1 i.e. parent of the oldest commit you want to squash

# 根据提示修改前缀为 fixup 或 sqaush 以进行“合并”

# git cherry-pick

从其他分支复制提交,到当前分支

git - How to copy commits from one branch to another? - Stack Overflow

git checkout "target-branch"
git cherry-pick '<commit id from other branch>'
git push origin "target-branch"

参考文献

WikiNotes/提示和技巧
Create a tag in a GitHub repository

猜你喜欢

转载自blog.csdn.net/u013670453/article/details/113059203
今日推荐