lxq.link
postscategoriestoolsabout

Git的使用

Git官网

Documentation

配置Git

git config --global user.name "Your Name"  ##设置用户名
git config --global user.email "youremail@domain.com"  ##设置email

ssh-keygen -t rsa -C "youremail@domain.com"  ##创建SSH key

#查看SSH key
cat ~/.ssh/id_rsa.pub

Clone 代码

# 基本使用
git clone <remote_repo>

# 增加选项
# 详细文档: https://git-scm.com/docs/git-clone
# 示例:
git -C <path> clone -b <branch> <remote_repo> --depth=1
# 选项说明:
# -C: specify the root directory
# -b: specify branch
# --depth: Create a shallow clone with a history truncated to the specified number of commits

Git从远程的分支获取最新的版本到本地

git fetch + git log + git merge

git fetch origin master

git log -p master..origin/master

git merge origin/master

git fetch + git diff + git merge

git fetch origin master:tmp

git diff tmp

git merge tmp

git pull

git pull origin master
# 相当于git fetch + git merge

删除远程分支

git push origin --delete <BranchName>

拉取远程分支并创建本地分支

git checkout -b <BranchName> origin/<BranchName>

强制推送本地分支到远程分支(覆盖远程分支)

git push -f origin <BranchName>

Merge unrelated histories

Using --allow-unrelated-histories flag:

git pull origin branchname --allow-unrelated-histories

Tag

# add tag
git tag -a v1.0 -m "description"
# check tag list
git tag
# check tag detail
git show v1.0
# push tag to remote
git push origin v1.0
# fetch remote tag
git fetch origin tag v1.0
# checkout tag
git checkout tags/v1.0

Apply a commit from one branch

git cherry-pick <commit-hash>

Conventional commit messages (git cz)

https://github.com/commitizen/cz-cli

npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

Remove all git branches except master

git branch | grep -v "master" | xargs git branch -D

上传文件区分大小写

git config core.ignorecase false

获取远程所有分支

git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

git fetch --all
git pull --all

Change the url of an existing remote repository

git remote rename origin old-origin
git remote add origin https://github.com/user/repo.git

查看远程信息

git remote -v

推送所有分支和tag到远程仓库

git push -u origin --all
git push -u origin --tags
2020-08-01