Git Commands

配置

1
2
3
4
git config --global user.name "zw3413"
git config --global user.email "zhang*@gmail.com"
git config --global user.password ""
git config --list

遠端庫

1
2
git remote add origin remotegitrepositoryaddress
git remote remote -v 查看遠程節點

獲取

1
2
3
git fetch remoteNodeName    從指定遠程節點獲取更新
git pull 等價於git fetch && git merge
git pull --rebase 等價於git fetch && git rebase

分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git branch -h   喚出分支命令幫助
git branch -r --remote 列出遠程分支
git branch -l --list 列出分支
git branch -a --all 列出所有分支
git branch -D branchname 刪除分支
git branch new_branch 657fce7 在657fce7創建分支
git ckout -b bird 657fce7 在commit657fce7上新建分支並切換過去
git branch bird 657fce7 在commit657fce7上新建分支不跳
git switch -c
git checkout branchname 切換到分支
git checkout --track remote-branchname 切換遠端分支同時在本地創建分支
git branch -a 查看远程分支
git checkout -b xxxx(本地分支名称) yyyy(上条命令查找到的远程分支的名称)
git branch 检查下 本地分支是否创建成功
git branch -vv 查看本地分支與遠程分支的關係
git checkout commitid 切換到指定的提交
git checkout filename 將文件從版本庫中取出,覆蓋當前工作區文件

提交

1
2
3
4
5
6
7
8
git rm filename     等價於 rm filename && git add filename 將文件從工作區和暫存區移除
git rm filename --cached 將文件從暫存區移除,相對於git add filename
git add -p filename 以不同的模式將文件加入暫存區, y-全部 e-部分
git add --all 添加repository中所有文件
git commit -a -m "commit with add first" 在提交前先添加
git commit -a --allow-empty -m "empty commit"
git commit --amend -m 'change the comment of last commit'
git commit --amend --no-edit

重置

1
git reset HEAD^

上傳

1
2
3
4
git push -u origin local_branch_name 在遠程創建同名本地分支,並追蹤
git push origin master:master
git push -f 大招
git push origin :master 刪除遠程分支

日誌

1
2
3
4
5
6
git log             顯示提交歷史
git log --oneline 單行模式顯示提交歷史
git log filename 顯示某一個文件的提交歷史
git log -p filename 顯示某一個文件的提交歷史以及修改詳情
git log -g
git reflog

清理

1
2
git rm --cached     清理gitignore緩存
git clean -fx 清除所有被gitignore忽略的文件

暫存

1
2
3
4
5
6
7
8
git stash       暫存已追蹤
git stash -u 暫存包含未追蹤
git stash list
git stash pop stash@{2}
git stash drop stash@{0}
git stash apply stash@{1}
git filter-branch --tree-filter "rm -f config/database.yml" 批量修改commits
git reset refs/original/refs/heads/master --hard

文件

1
2
3
4
5
6
git diff e37078e d4d8d9d    比對兩個commit
git diff e37078e 比對e37078e與HEAD指向的commit
git diff 比對工作目錄與暫存區
git diff --cached 比對暫存區與HEAD指向的commit
git diff-tree HEAD --name-only -r --no-commit-id
git blame -L 5,10 filename 顯示每一行的修改人,可指定行範圍

cherry-pick

1
2
3
4
5
6
7
git cherry-pick [<options>] <commit-ish>...
options:
--quit quit current chery-pick sequence
--continue continue current chery-pick sequence
--abort abort current chery-pick sequence,restore current branch
-n, --no-commit not auto-commit, changes will be implemnted on branch1 but need commit manually
-e, --edit edit commit message

There two branch- branch1 and branch2 in local repostory, check the commit logs of each branch first:

1
2
3
4
5
6
7
8
9
10
11
$ git checkout branch2    or   git switch branch2
$ git log --oneline -3
23d9422 [Description]:branch2 commit 3
2555c6e [Description]:branch2 commit 2
b82ba0f [Description]:branch2 commit 1
$ git checkout branch1
Switched to branch 'branch1'
$ git log --oneline -3
20fe2f9 commit second
c51adbe commit first
ae2bd14 commit 3th

Now, we want pick up one commit on branch2, and merge it to branch1, we can use cherry-pick command:

1
2
3
4
5
$ git cherry-pick 2555c6e
error: could not apply 2555c6e... [Description]:branch2 commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

If no conflicts, above command will finish the process and success.
If conflict happens, like above hint, we need to resolve the conflicts first, then use git commit to commit manually:

1
2
3
4
5
$ git commit 
[branch1 790f431] [Description]:branch2 commit 2
Date: Fri Jul 13 18:36:44 2018 +0800
1 file changed, 1 insertion(+)
create mode 100644 only-for-branch2.txt

Or use git add. to mark we have reloved the conflicts, and then git cherry-pick –continue to continue the cherry-pick.
Finally, check the commit log of branch1, we can see the target commit show on branch1.

1
2
3
4
$ git log --oneline -3
790f431 [Description]:branch2 commit 2
20fe2f9 commit second
c51adbe commit first

more

1
2
3
4
5
6
7
8
9
10
11
git cherry-pick fd23e1c 6a498ec f4f4442  應用指定提交
git cherry-pick --no-commit fd23e1c 6a498ec f4f4442 應用指定放暫存區不提交
git format-patch fd7cd38..6e6ed76 生成從fd7cd38(之後,不包含)到6e6ed76的patch
git format-patch -2 -o /tmp/patches 生成過去兩個commit的patch,並指定保存位置
git am /tmp/patches 應用patch
git archive 打包指定文件,分支、提交、目錄、項目 都可以
squash 擠入
amend 修正
meld 融合
rewinding 倒退
stash 儲藏

合併兩個分支遇到衝突,合併失敗時,通過rebase待合併的分支來解決衝突

1
2
3
4
5
6
git checkout test-increase-test-coverage    //切換到待合併的分支
git fetch feat-add-daily-mileage //獲取最新的基礎分支
git rebase origin/feat-add-daily-mileage //開始rebase
// Then I fixed conflicts and commited
git rebase --continue //執行rebase
git push -f origin test-increase-test-coverage //更新待合併分支

修改.gitignore後,刷新緩存

1
2
3
git rm -r --cached .
git add .
git commit -m 'update .gitignore'

GIT PULL 強制覆蓋本地

1
2
3
git fetch --all
git reset --hard origin/master
git pull