Git 使用 介绍 分布式版本控制系统。
1 2 git blame [options] <file>
相关概念 Git 中的 4 个位置:工作目录 Working Directory、暂存区 Staging Area、本地存储库 Local Repository、远程存储库 Remote Repository
暂存区(stage):已经修改、等待后续提交的文件 文件三个类别:未跟踪(Untracked)、已追踪(Tracked)、被忽略(Ignored) HEAD:当前工作区在提交历史中的指针 detached HEAD:HEAD 指向某个历史提交,而不是某个 “分支”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 git init git init folder git check-ignore -v file git rm git rm --cached git mv
.git 结构 WIP…
参考资料
使用 工具
1 2 3 4 5 6 7 8 9 10 11 12 brew install lazygit go install github.com/jesseduffield/lazygit@latest cargo install onefetch pip install -U gita brew install coreutils export PATH="$HOMEBREW_PREFIX /opt/coreutils/libexec/gnubin:$PATH " brew install git-quick-stats brew install git-delta
delta :主要用于 Git 相关命令(diff、blame、show 等)的语法突出显示分页器
安装包为 git-delta,可执行命令为 delta
使用:在 ~/.gitconfig
中添加以下设置
1 2 3 4 5 6 7 8 9 10 11 [core] pager = delta [interactive] diffFilter = delta --color-only [delta] features = side-by-side line-numbers decorations navigate = true [merge] conflictstyle = diff3 [diff] colorMoved = default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 brew install git-extras git clone [https://github.com/tj/git-extras.git](https://github.com/tj/git-extras.git) cd git-extrasgit checkout $(git describe --tags $(git rev-list --tags --max-count=1)) make install PREFIX=$HOME /local/git-extras export PATH=$HOME /local/git-extras/bin:$PATH source $HOME /local/git-extras/git-extras-completion.zshgit setup git summary git changelog git commits-since git count git count --all git undo N
基本使用
注册 GitHub 或 Gitee 账户
配置 GitHub 或 Gitee 的 SSH
配置 Git
1 2 3 4 5 6 7 8 9 10 11 12 13 git init git add . git commit -m "first commit" git remote add origin [email protected] :user/repo.git git remote add origin [email protected] :user/repo.git git push -u origin main
开发使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 git clone url git checkout -b feature git add . git commit -m "message" git push origin feature git checkout main git pull origin main git checkout feature git rebase main git pull origin main:feature git push origin feature -f git pull origin feature:feature git merge feature git rebase feature
特殊文件 .gitconfig
git 配置文件
路径:Linux ~/.gitconfig
;Windows git\etc\gitconfig
内容示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [user] name = XXX email = [email protected] [init] defaultBranch = main [credential] helper = cache --timeout 300000 [core] quotepath = false [help ] autocorrect = 1 [push] autosetupremote = true
.gitignore
忽略文件:写入在 .gitignore
文件中的文件/目录会被忽略掉
可在 repo 根目录及其子目录分别创建
常用 .gitignore
模板:
.gitattributes
用于配置 Git 在处理不同类型文件时的行为:定义行结束符(Line Endings)、指定语言统计等(linguist-language)等。
示例 1:.gitattributes- simpy 示例 2:md - .gitattributes - note
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 * text=auto *.txt eol=lf *.jpg binary *.pdf binary *.png binary *.doc diff=astextplain *.DOC diff=astextplain *.docx diff=astextplain *.DOCX diff=astextplain *.dot diff=astextplain *.DOT diff=astextplain *.pdf diff=astextplain *.PDF diff=astextplain *.rtf diff=astextplain *.RTF diff=astextplain *.md linguist-documentation=false linguist-detectable=true *.md linguist-language=Markdown *.html linguist-detectable=false *.js linguist-detectable=false *.css linguist-detectable=false linguist-documentation=false linguist-detectable=true linguist-language=... linguist-vendored
.gitmodules
Git 子模块(submodule):允许将一个 Git repo 嵌套在另一个 Git repo 中,以便在一个项目中使用其他项目的代码。
.gitmodules
写法及示例:.gitmodules - ZJU-UGCourse
1 2 3 4 5 6 7 [submodule "submodule" ] path = submodule url = [email protected] :user/submodule.git branch = main shallow = true depth = 1
添加子模块
1 2 3 4 git submodule add [email protected] :user/repo.git git submodule update --init --recursive
克隆含子模块的 GitHub repo
1 2 3 4 5 6 7 8 9 10 11 git clone --recurse-submodules [email protected] :user/repo.git git clone [email protected] :user/repo.git cd repogit submodule init git submodule update git submodule update --init --recursive
.git-credential
Git - 凭证存储
凭证存储;非标准 git 配置文件。
默认所有都不缓存。 每一次连接都会询问用户名和密码
“cache” 模式会将凭证存放在内存中一段时间。 密码不会被存储在磁盘中,且 15 分钟后从内存中清除
“store” 模式可接受 --file <path>
参数,自定义存放密码的文件路径(默认 ~/.git-credentials
)
1 2 3 git config --global credential.helper cache git config --global credential.helper 'store --file ~/.my-credentials'
多账号 ssh 配置
配置同时使用 Gitlab、Github、Gitee(码云) 共存的开发环境 - 简书
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Host github.com Port 22 HostName github.com User git IdentityFile ~/.ssh/id_rsa.github Host gitee.com Port 22 HostName gitee.com User git IdentityFile ~/.ssh/id_rsa.gitee Host gitlab.com Port 22 HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa.gitlab
1 2 3 4 5 Hi XXX! You've successfully authenticated, but GitHub does not provide shell access. Hi XXX! You've successfully authenticated, but GITEE.COM does not provide shell access. Welcome to GitLab, XXX!
注意事项 :
~/.ssh/config
文件出现 Bad owner or permissions
错误的解决办法:文件权限问题,设置 config 文件权限为 600
超算平台中的登陆节点禁止对外的 ssh,无法使用 git 交互环境,建议在本地或者实验室工作站(Manager 和 Master)使用;超算平台进行以上设置会出现以下报错:
1 ssh: connect to host github.com port 22: Network is unreachable
将 repo 的 remote origin 由 https 改为 ssh 或 token 形式
GitHub Token
Gitee 与 GitHub、GitLab 之间互相同步
Git LFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 sudo apt-get install git-lfs brew install git-lfs git lfs install git lfs track "*.pdf" git add .gitattributes git commit -m "add .gitattributes" git lfs track git lfs pull git lfs clone repo_url git lfs ls-files git lfs untrack "*.pdf" git rm --cached "*.pdf" git lfs migrate import --include="*.dmg" --everything git push --force
git-filter-repo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 brew install git-filter-repo pip install -U git-filter-repo --dry-run git filter-repo --path-glob '*.jpg' --invert-paths git filter-repo --path old_path --to-path new_path git filter-repo --name-callback 'return name.replace(b"Old Name", b"New Name")' git filter-repo --strip-blobs-bigger-than 10M git filter-repo --subdirectory-filter path git push -f origin main
常用命令 clone 1 2 3 4 5 git clone <url> git clone --depth 1 <url> git fetch --unshallow git clone -b <branch1> -b <branch2> <url>
config 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 git config --list git config --global --list git config --list --show-origin git config --global user.name "username" git config --global user.email "[email protected] " color.ui 1 core.editor "vim" core.fileMode false core.quotepath false help.autocorrect 1
add 1 2 3 4 5 git add file git add --patch git add -i
commit
1 2 3 4 5 6 7 8 9 10 11 12 git commit -m "$(date '+%Y-%m-%d %H:%M:%S') " git commit --amend --no-edit git push --force git commit --amend --no-edit -m 'xxx' git commit -m 'empty' --allow-empty
push 1 2 3 4 5 6 7 8 git push git push origin :<remote-branch> git push origin --delete <remote-branch> git remote prune origin
pull 1 2 3 git pull git pull --rebase
branch 1 2 3 4 5 6 7 8 9 10 11 12 13 git branch git show-branch git branch -r git branch -a git branch -u origin/main git branch -m <NewBranch> git branch -d <Branch> git branch -D <Branch> git branch -vv * master 8b700ba [origin/master] vault backup: 2024-04-01 19:11:49
checkout 1 2 3 4 5 git checkout <Branch> git checkout -b <Branch> git checkout - git checkout -- file git checkout -t <remote>/<Branch>
remote 1 2 3 4 5 6 7 8 git remote show origin git ls-remote origin git remote prune origin git remote prune origin --dry-run git remote rm origin
status 1 2 3 4 5 6 git status git status --short --branch gsb | grep -E '^\s*[?][?]' | awk '{print $2}' | xargs rm -rf
reset 1 2 3 4 5 git reset git reset HEAD~ git reset --hard HEAD~ git reset commit_hash git reset file
tag
版本号命名一般规范:v 主版本号.次版本号.修订号[- 预发布版本号]
主版本号:不兼容的 API 修改;为 0 时表示还在开发阶段,不保证稳定性
次版本号:添加新功能,但是保持兼容
修订号:兼容修改,修正不正确的行为
示例:v1.0.0、v1.0.0-beta
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 git pull --all git fetch --tags git tag -ln git ls-remote --tags origin git show v1.0.0 git tag -a v1.0.0 -m 'comment' git push origin v1.0.0 git push origin --tags git tag -d v1.0.0 git push origin :refs/tags/v1.0.0
log 查看 commit 记录/日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 git log git reflog git shortlog -s -n git log --pretty=format:'%an <%ae>' | sort | uniq -c | sort -nr git rev-list --count --all git rev-list --count [branch] git log --oneline --graph --all git log --oneline --graph --stat git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" --stat git log -1 --pretty="%ci" file git log -1 --diff-filter=A --follow --pretty="%ci" file -p --graph --stat --oneline --pretty=oneline =%B =%s =%H =%h =%ci =%ad =%ar =%an =%ae =%d -n N / HEAD~N --grep=pattern --date =short --since="midnight" --since="2024-09-17 00:00" --until ="2024-09-18 00:00" --since="1 day ago"
diff 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 git diff git diff <Branch> git diff <Branch1> <Branch2> git diff HEAD~N -- file git diff --stat git diff --stat file git diff <commit_id_1> <commit_id_2> --stat git show <commit_id> --stat git diff-tree <commit_id> --stat git diff --staged --stat git diff --cached --stat --name-only --diff-filter=M
stash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 git stash git stash -u git stash push -- file git stash save 'message' git stash list git stash pop git stash apply git stash show -p git stash show --name-only git stash apply stash@{n} git stash drop stash@{n} git stash clear git stash branch <branch>
rm
从工作区批量去除已删除文件
1 2 3 git rm $(git ls-files -d) git rm --cached file git rm -r --cached folder
git 命令自定义别名
1 2 3 4 5 6 7 8 9 git config --global alias.p 'push' [alias ] p = push alias | grep 'git subcommand'
其他用法
1 2 3 4 5 6 7 8 9 git remote add origin url git remote set-url --add origin url git remote set-url --delete origin url url = XXX
浅克隆时,push 到多个远程 repo 会出现如下报错:
1 2 3 4 5 remote: fatal: did not receive expected object XXX error: remote unpack failed: index-pack failed To github.com:user/repo.git ! [remote rejected] main -> main (failed) error: failed to push some refs to 'github.com:user/repo.git'
git clone 部分内容:如何使用 Git 只克隆部分文件 | 猎人杂货铺
git sparse-checkout
- 可实现只克隆或检出指定文件夹,不下载所有内容
--filter=blob:none
- 只获取元数据,不下载原始数据部分
1 2 3 4 5 6 7 git clone --filter=blob:none --sparse <repo> git sparse-checkout set <file> <folder> git clone --filter=blob:none --no-checkout <repo> git checkout origin/main -- <file> <folder>
下载单个文件:打开文件,点击 “Raw”,用 wget
下载,示例:
1 2 3 4 5 6 7 8 wget https://gitee.com/Devkings/oh_my_zsh_install/raw/master/install.sh -O install.sh wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O install.sh wget https://gist.githubusercontent.com/user/GIST_ID/raw/filename -O filename
gitmoji-cli:git commit 时使用 emoji
GitHub - carloscuesta/gitmoji-cli: A gitmoji interactive command line tool for using emojis on commits. 💻
gitmoji 速查表 - Git 重学指南
创建 Releases:Create a new release - Choose a tag,之后填写相关信息,必要时上传附件
1 2 3 git switch --orphan <new branch> git commit --allow-empty -m "init" git push -u origin <new branch>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 git whatchanged --since='2 weeks ago' git checkout --orphan <NewBranch> <commit-hash> git commit -m 'init' git branch -D main git branch -m main git push origin main --force git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}') " git ls-tree --full-tree -r --name-only HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 git clone --no-local git commit -am 'message' git commit --amend --no-edit git pull --prune git merge --allow-unrelated-history git reflog expire --expire=now --all git filter-repo -f --commit-callback 'commit.committer_date = commit.author_date'
相关问题
1 2 3 4 5 6 7 8 9 10 Host github.com Hostname ssh.github.com Port 443 git config --local -e
1 2 3 4 5 6 7 error: RPC failed; Failed to connect to github.com port 443: Couldn't connect to server # 解决方法 # 若有 VPN 代理,设置代理 git config --global http.proxy 127.0.0.1:7890 git config --global https.proxy 127.0.0.1:7890
1 2 3 4 5 6 error: cannot pull with rebase: You have unstaged changes. error: please commit or stash them. git config pull.rebase false