ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 从git仓库中移除文件 参考 git pro pages34 我们想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中。换句话说,你想让文件保留在磁盘,但是并不想让 Git 继续跟踪。当你忘记添加 .gitignore 文件,不小心把一个很大的日志文件或一堆 .a 这样的编译生成文件添加到暂存区时,这一做法尤其有用。为达到这一目的,使用 --cached 选项: ```plain $ git rm --cached README ``` # 对上次提交进行重新提交 有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。此时,可以运行带有 --amend 选项的提交命令尝试重新提交: ```plain $ git commit --amend ``` # 查看已跟踪文件 ```plain git ls-files ``` # 重命名本地分支 重命名分支 A(oldName) 为分支 B(newName) ```plain git branch -m <oldName> <newName> ``` # 重命名当前分支 ```plain git branch -m <newName> ``` # 修改远程分支 ```plain // 直接修改 git remote set-url origin [url] // 删除再添加 git remote rm origin git remote add origin [url] ``` # 查看远程仓库地址 ```plain git remote -v ``` # 修改远程仓库地址 ```plain git remote set-url origin [url] ``` # 储藏变更 Git 工具 - 储藏(Stashing) ## 储藏 ```plain git stash ``` ### 查看储藏列表 ```plain git stash list ``` ### 查看储藏内容 ```plain git stash show -p stash@{0} ``` ## 恢复储藏 ```plain git stash apply --index ``` ## 恢复储藏并从储藏列表删除 ```plain git stash pop --index ``` ## 清空储藏列表 ```plain git stash clear ``` # 打标签 ## 查看已有标签 ```plain git tag ``` 筛选特定标签 ```plain git tag -l 'v1.4.4.*' ``` ## 打标签 1、轻量级标签 ```plain git tag v1.4 ``` 2、含附注的标签 ```plain git tag -a v1.4 -m 'my version v1.4' ``` ## 查看标签 ```plain git show v1.4 ``` # 推送标签 git push 时并不会推送标签,需要显示命令来推送: ```plain git push origin v1.4 // 或者一次推送所有本地新增标签 git push origin --tags ``` # 删除某个 commit 例如提交历史如下: ```plain commit 58211e7a5da5e74171e90d8b90b2f00881a48d3a Author: test <test@36nu.com> Date: Fri Sep 22 20:55:38 2017 +0800 add d.txt commit 0fb295fe0e0276f0c81df61c4fd853b7a000bb5c Author: test <test@36nu.com> Date: Fri Sep 22 20:32:45 2017 +0800 add c.txt commit 7753f40d892a8e0d14176a42f6e12ae0179a3210 Author: test <test@36nu.com> Date: Fri Sep 22 20:31:39 2017 +0800 init ``` 假如要删除备注为 `add c.txt`,commit 为`0fb295fe0e0276f0c81df61c4fd853b7a000bb5c`的这次提交 1、首先找到此次提交之前的一次提交的commit`7753f40d892a8e0d14176a42f6e12ae0179a3210` 2、执行 `git rebase -i 7753f40` 3、将`0fb295f`这一行前面的 pick 改为 drop,然后按照提示保存退出 # 清理无效的远程追踪分支 ```plain git remote prune origin ``` # 仅合并某些提交 cherry-pick cherry-pick 和它的名称一样,精心挑选,挑选一个我们需要的 commit 进行操作。它可以用于将在其他分支上的 commit 修改,移植到当前的分支。、 简单操作: ```plain git cherry-pick <commit-id> ``` 批量操作 ```plain git cherry_pick <start-commit-id>…<end-commit-id> ``` 它的范围就是 start-commit-id 到 end-commit-id 之间所有的 commit,但是它这是一个 (左开,右闭\] 的区间,也就是说,它将不会包含 start-commit-id 的 commit。 而如果想要包含 start-commit-id 的话,就需要使用 ^ 标记一下,就会变成一个 \[左闭,右闭\] 的区间,具体命令如下: ```plain git cherry-pick <start-commit-id>^...<end-commit-id> ``` # 子模块 ## 添加子模块 ```plain git submodule add "子模块仓库地址" ``` ## 克隆含有子模块的仓库 ### 方式一 ```plain git clone "主项目远程仓库地址" ``` 此时子模块目录为空的,运行: ```plain git submodule init git submodule update ``` ### 方式二 ```plain git clone --recursive "主项目远程仓库地址" ``` ## 拉取子模块最新修改 ### 方式一 拉取: ```plain git fetch [远程分支名称] ``` 合并: ```plain git merge [分支名称] ``` ### 方式二 ```plain git submodule update --remote ``` 此命令默认会假定你想要更新并检出子模块仓库的 master 分支,可以通过以下命令设置想要跟踪的子模块分支: ```plain git config -f .gitmodules submodule."子模块名称".branch "要跟踪的分支名称" ``` ## 在子模上工作 ### 推送时检查所有子模块是否已推送 ```plain git push origin dev --recurse-submodules=check ``` ## A git directory for '...' is found locally with remote(s) 问题解决 详细问题: ```plain git submodule add https://github.com/iissnan/hexo-theme-next.git themes/next --branch master A git directory for 'themes/next' is found locally with remote(s): origin https://github.com/XXX/hexo-theme-next.git If you want to reuse this local git directory instead of cloning again from https://github.com/iissnan/hexo-theme-next.git use the '--force' option. If the local git directory is not the correct repo or you are unsure what this means choose another name with the '--name' option. ``` ### 解决方案 1、git rm –-cached themes/next 2、删除 .gitmodules 文件中如下内容: ```plain [submodule "themes/next"] path = themes/next url = https://github.com/XXX/hexo-theme-next.git ``` 3、删除 .git/config 文件中如下内容 ```plain [submodule "path_to_submodule"] url = https://github.com/XXX/hexo-theme-next.git ``` 4、rm -rf .git/modules/themes/next 5、重新添加子模块 ## 参考连接 [Git-document:Git 工具 - 子模块](https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97) [GIT使用问题记录](http://blog.bflyer.com/2017/07/23/GIT%E4%BD%BF%E7%94%A8%E9%97%AE%E9%A2%98%E8%AE%B0%E5%BD%95-%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0/)