-
推薦 UI 工具:Sourcetree : free for Windows and Mac
-
在做自己不熟悉的任何 -d 命令前,先要做好備份。。。
-
常用命令
查看所有分支
git branch -a
拿特定branch的代碼
git checkout origin/daily/1.4.1
切換branch
git checkout #branch name#
(如果是線上已有的 branch,一定不要加 -b,加 -b 就是從當前 branch 新建的)
創建branch
git checkout –b #branch name#
刪除branch
git branch –d #branch name#
丟棄最新的commit
git reset --hard HEAD~1
Revert 最新的commit,去除commit,但是不丟棄改動本身
git reset --soft HEAD~1
查看單個文件 history
gitk [filename]
or to follow filename past renames
gitk --follow [filename]
git uncommit 自己最近的 commit
如果自己最近 commit 了多個,就執行如下,其中 asdf 是自己最近第一條 commit 的前一條 commit
git reset --soft "asdf"
把新添加的文件也stash
git stash -u
查看某branch自己的提交歷史
git log develop --author=your.name --oneline -10
git revert 已經 push 的 commit
git revert <commit_hash>
git revert <oldest_commit_hash>..<latest_commit_hash>
https://stackoverflow.com/questions/22682870/git-undo-pushed-commits
Git cherry-pick 多個 commit
git cherry-pick -n 90f121ae7 4a42a1d33 e257dcda6 6760de43d 0784f0cde 80a4c3bf6 321482012
加 -n 是爲了 cherry-pick 過來後不自動 commit
cherry-pick 建議流程
1. 多個 commit 同時 cherry-pick
2. 如果中間提示有衝突,則 處理衝突,然後 git cherry-pick --continue
3. 如果git cherry-pick --continue 提示本地 需要 stash/commit,則先 stash,在 continue (不過用 commit 可能更好?因爲會保留 commit 號碼?)
cherry-pick 全部完成後,按照 stash 順序重新應用到 本地 (如果前面不用 stash 而用 commit 就不需此)
查看 branch 的地址
git remote -v
origin http://gitlab.alibaba-inc.com/DRDS/manager.git (fetch)
origin http://gitlab.alibaba-inc.com/DRDS/manager.git (push)
查看一個 branch 內兩個日期之間的 不同文件
https://stackoverflow.com/questions/1161609/how-can-i-get-the-diff-between-all-the-commits-that-occurred-between-two-dates-w
git diff --stat @{2018-10-24}..@{2018-8-1}
or
git diff --stat @{2.weeks.ago}..@{last.week}
git clone 指定 branch
git clone --single-branch -b branch host:/dir.git
如 git clone -b feature/support_cross_schema_query http://gitlab.my-inc.com/middleware/my_proj.git
-
git 要把一個 fork(repo) 的改動應用到另一個 fork
在本 repo 上 commit,但不 push
通過 source tree push 時,手動選擇相應 branch。
http://xigua366.iteye.com/blog/2400153
-
如果錯誤地把改動 commit 到了 HEAD detached
https://stackoverflow.com/questions/4845505/gitx-how-do-i-get-my-detached-head-commits-back-into-master/4845543
1. git reflog and git reflog --all will give you the commit hashes 找到你提交的 commit
git merge HEAD@{1} (後面數字從 git reflog 結果查看)將改動 merge 到當前 branch
-
Fatal Error when updating submodule using GIT
https://stackoverflow.com/questions/8197089/fatal-error-when-updating-submodule-using-git
修改 /Users/your.name/Documents/Work/my_proj/.git/config 和
/Users/your.name/Documents/Work/my_proj/.gitmodules 裏內容:
[submodule "my-calcite"]
path = my-calcite
url = http://gitlab.my-inc.com/my_proj/my-calcite.git
branch = master
改完之後,一定要執行 git submodule sync
-
在 sourcetree 上查看兩個 commit 之間的不同
在 history 頁面只顯示當前 branch 的,然後按 ⌘ (OSX) 選中任意兩個 commit,就可以查看二者之間的不同了。
-
如果 sourcetree git 拉代碼突然沒權限,可能是改了系統用戶密碼的原因
這是需要通過命令行拉一次,就可以了。
-
如果碰到 本地並無自己的改動,但從一個乾淨的遠程 branch 切換到另一個乾淨的 branch 時,出現:
則直接採用 force
git checkout -f another-branch
-
如果 git pull 一個乾淨 branch 時出現
error: The following untracked working tree files would be overwritten by merge:
my-calcite/src/main/java/org/apache/calcite/rel/core/SemiJoin.java
my-calcite/src/main/java/org/apache/calcite/rel/metadata/RelMdPredicates.java
把本地相應文件刪除再 pull
或者:
The only commands that worked for me were:
git fetch --all
git reset --hard origin/{{your branch name}}
如:git reset --hard origin/my_dev_mpp_stable_partialy_merged
-
查看哪些文件被 ignore 了
git status --ignored
-
ignore 的幾個地方
1. 工程 git 庫根目錄
work/project_A/ .gitignore
2. 總目錄
/Users/your_name/.gitignore_global
3. exculde 文件
work/manager2/manager/.git/info/exclude
ignore 只會ignore 未 add 的文件,已經add 的文件修改的話不會被 ignore。
例:想把 idea 產生的文件都 ignore 掉,則修改 project_A/.git/info/exclude 文件爲:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
.idea
*.iml
target