[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/)
- 導讀
- Java知識
- Java基本程序設計結構
- 【基礎知識】Java基礎
- 【源碼分析】Okio
- 【源碼分析】深入理解i++和++i
- 【專題分析】JVM與GC
- 【面試清單】Java基本程序設計結構
- 對象與類
- 【基礎知識】對象與類
- 【專題分析】Java類加載過程
- 【面試清單】對象與類
- 泛型
- 【基礎知識】泛型
- 【面試清單】泛型
- 集合
- 【基礎知識】集合
- 【源碼分析】SparseArray
- 【面試清單】集合
- 多線程
- 【基礎知識】多線程
- 【源碼分析】ThreadPoolExecutor源碼分析
- 【專題分析】volatile關鍵字
- 【面試清單】多線程
- Java新特性
- 【專題分析】Lambda表達式
- 【專題分析】注解
- 【面試清單】Java新特性
- Effective Java筆記
- Android知識
- Activity
- 【基礎知識】Activity
- 【專題分析】運行時權限
- 【專題分析】使用Intent打開三方應用
- 【源碼分析】Activity的工作過程
- 【面試清單】Activity
- 架構組件
- 【專題分析】MVC、MVP與MVVM
- 【專題分析】數據綁定
- 【面試清單】架構組件
- 界面
- 【專題分析】自定義View
- 【專題分析】ImageView的ScaleType屬性
- 【專題分析】ConstraintLayout 使用
- 【專題分析】搞懂點九圖
- 【專題分析】Adapter
- 【源碼分析】LayoutInflater
- 【源碼分析】ViewStub
- 【源碼分析】View三大流程
- 【源碼分析】觸摸事件分發機制
- 【源碼分析】按鍵事件分發機制
- 【源碼分析】Android窗口機制
- 【面試清單】界面
- 動畫和過渡
- 【基礎知識】動畫和過渡
- 【面試清單】動畫和過渡
- 圖片和圖形
- 【專題分析】圖片加載
- 【面試清單】圖片和圖形
- 后臺任務
- 應用數據和文件
- 基于網絡的內容
- 多線程與多進程
- 【基礎知識】多線程與多進程
- 【源碼分析】Handler
- 【源碼分析】AsyncTask
- 【專題分析】Service
- 【源碼分析】Parcelable
- 【專題分析】Binder
- 【源碼分析】Messenger
- 【面試清單】多線程與多進程
- 應用優化
- 【專題分析】布局優化
- 【專題分析】繪制優化
- 【專題分析】內存優化
- 【專題分析】啟動優化
- 【專題分析】電池優化
- 【專題分析】包大小優化
- 【面試清單】應用優化
- Android新特性
- 【專題分析】狀態欄、ActionBar和導航欄
- 【專題分析】應用圖標、通知欄適配
- 【專題分析】Android新版本重要變更
- 【專題分析】唯一標識符的最佳做法
- 開源庫源碼分析
- 【源碼分析】BaseRecyclerViewAdapterHelper
- 【源碼分析】ButterKnife
- 【源碼分析】Dagger2
- 【源碼分析】EventBus3(一)
- 【源碼分析】EventBus3(二)
- 【源碼分析】Glide
- 【源碼分析】OkHttp
- 【源碼分析】Retrofit
- 其他知識
- Flutter
- 原生開發與跨平臺開發
- 整體歸納
- 狀態及狀態管理
- 零碎知識點
- 添加Flutter到現有應用
- Git知識
- Git命令
- .gitignore文件
- 設計模式
- 創建型模式
- 結構型模式
- 行為型模式
- RxJava
- 基礎
- Linux知識
- 環境變量
- Linux命令
- ADB命令
- 算法
- 常見數據結構及實現
- 數組
- 排序算法
- 鏈表
- 二叉樹
- 棧和隊列
- 算法時間復雜度
- 常見算法思想
- 其他技術
- 正則表達式
- 編碼格式
- HTTP與HTTPS
- 【面試清單】其他知識
- 開發歸納
- Android零碎問題
- 其他零碎問題
- 開發思路