「Git」- 提示和技巧

  CREATED BY JENKINSBOT

在命令行中,无密码检出

How to give username/password to git clone in a script, but not store credentials in .git/config – Server Fault

# 方法一、直接指定密码
git clone https://user:password@github.com/name/repo.git

# 方法二、自定义密码程序
cat > /tmp/git-ask-pass.sh <<EOF
#!/bin/sh
exec echo "$GIT_PASSWORD"
EOF
GIT_ASKPASS=/tmp/git-askpass-helper.sh GIT_PASSWORD=password git clone "https://username@hostname/repo"

常用文件操作

查看在某次提交中特定文件内容:

# git show REVISION:/path/to/file

// 将内容写入文件

# git show REVISION:/path/to/file > /path/to/file.copy

创建空分支

Git create a empty branch from existing repository – Stack Overflow

git checkout --orphan "<your branch name>"

# 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 <k4nzdroid@163.com>"

在提交时,关闭钩子

githooks – Skip Git commit hooks – Stack Overflow

git commit --no-verify

将文件添加到当前提交

How to add a file to the last commit in git? – Stack Overflow

git add "/path/to/file"
git commit --amend --no-edit

# git tag

How To Delete Local and Remote Tags on Git – devconnected

# 增加
git tag <tagname>
git tag <tagname> --message ".." # 添加注解
git tag <tagname> -a # 打开编辑器,以添加注解

# 删除
git tag --delete v1.0
git push --delete origin v1.0

CMD DESC
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"

替换 git:// 为 https:// 协议

npm git protocol dependencies – Stack Overflow

git config --global url."https://github.com/".insteadOf git@github.com:
git config --global url."https://".insteadOf git://

参考文献

Create a tag in a GitHub repository
How to view a file at a specific commit in git? – SysTutorials