主要內容節錄至 YouTube頻道-李祐棠:
Git中文教學
git init
git add <file>
git commit
git config user.name
git config user.email
git config core.editor vim
git config merge.tool vimdiff
git config alias.xxx yyy
git config color.ui true
補充:git config --global
可以一次性更改所有 git 設定。
而 git commit
是把 staged 的東西移到 commit 資料庫內。
git commit -a
將 git commit
+ git add
兩個動作一次完成。git status
git add -p
分別檢視與是否加入每一個改變,這就是 patch add 。查看狀態
$ git status
Changed to be committed:
已經透過
git add
把需要加入的修改項目,添加到下一次的 Commit (等待發送的)當中。
Changes not staged for commit:
代表 GitHub 上面有這個檔案,但是還在 Commit 中(還沒更新到 Git 上面的意思)。
Untracked files:
代表 GitHub 上面還沒有這個檔案。
加入想要 Commit 的修改項目
$ git add
$ git add -p
log 查看版本紀錄
$ git log
ignore (不要出現在 status 的方法)
gitignore
,裡面$ vim .gitignore
在欲加入
gitignore
的檔案,加上/
後為現在這個資料夾下的那個檔案/檔名
gitignore
檔案加入 Commit 中$ git add .gitignore
如果要取消加入
$ git reset HEAD .gitignore
在 Commit 中,遵從標題,內文72字的規則,標題清楚標,細節在內文。會有字數限制是因為,在終端機用 $ git log
閱讀 Commit 時,這樣可以一行看完,而不會有些 Commit 標題一行,有些兩行。
>標題在超過字數(50字)後,文字會變成白色提醒使用者。
撰寫 Commit 建議須包含以下:
Relate Issue: Issue #159
The Problem:
The Cause:
The fix:
Relate Issue:
Issue #159
The Problem:
The following source code will create wrong branch arrow.
var graph1 = new GitGraph({
elementId: 'badGraph',
mode: 'compact',
template: 'blackarrow'
});
var m1 = graph1.branch("master");
m1.commit();
var r1 = m1.branch('1.0.x');
m1.commit();
r1.commit();
The arrow pointed to r1 commit will pointed from second commit of m1
not follow the line from first commit of m1.
The Cause:
When create a commit, if user does not specify parent commit in options
gitgraph.js will execute following code:
options.parentCommit = options.parentCommit || _getParentCommitFromBranch(this);
to determine the parent commit of the created commit.
In the function _getParentCommitFromBranch, it will try to get the last
commit from current branch, then from parent branch. Since r1.commit()
is the first commit in its branch 1.0.x, it will set its parent commit
to last commit of its parent branch, that is master. And since master
already add some new commit, the arrow will point from wrong commit, the
last commit of master branch.
There is code that deal with branch case:
if (options.parentCommit instanceof Commit === false && this.parentBranch instanceof Branch) {
options.parentCommit = this.parentCommit;
}
It set parentCommit to the correct commit. However, since
options.parentCommit already been set by previous code, this code will
never execute since options.parentCommit is always a Commit.
The fix:
The fix is combining two scenario. If current branch already has commits,
the parentCommit will be set to last commit in this branch. Otherwise,
it will set to parent commit of this branch, which will be the correct commit
instead of last commit of parent branch.
Hash Value: 指定是哪個Commit。
-(n)
:只顯示最後 n 筆提交。$ git log -n1
HEAD
、master
目前的commit。$ git log -n1 HEAD
$ git log master
^
看目前 Commit 的舊一個。
--oneline
只顯示一行。
$ git log --oneline load.cpp
: 只顯示與 load.cpp
有關的 Commit。Git log <path>|<commit>
diff
:顯示改了什麼( Commit 與未加入 Commit 的不同)。
$ git diff
Modify Commit
git checkout <commit>
Move to HEADgit checkout <path>
WARNING: discard changegit branch
List branchgit branch <name>
Create branch
git checkout -b
git checkout -b <name>
git branch
git log ru8 加下面
git branch -d <name>
git branch -D <name>
for no-merge commit
git merge <branch>
git checkout master
git merge <branch>
<<<<<<
, >>>>>>
git mergetool
(圖形化介面,展示哪裡不同) like vimdiff
上圖的意思:
<<<<<<
未修改的版本
======
新改的地方
>>>>>>
去對照後,手動改成最新版的,再 commit 。
git merge-base HEAD <new-base>
rebase --onto <new base><ref_commit><branch>
<ref_commit>
and <branch>
Example:
git rebase --onto master A dev
git rebase --continue
until endgit rebase --abort
if out of controlgit clone
git remote show <origin>
: list detailgit remote add <name>
URL: add remotegit remote rename <a><b>
: rename<remote>/<branch>
branch -a
git fetch <name>
pull
(=fetch + rebase)git push <remote><branch>
: <branch>
--set-upstream
git push <remote>
: <branch>
delete remote branch<origin>
<upstream>
<origin>
, open Pull Request<upstream>
and rebase mastergit stash
: store stash contentgit stash list
: show stored content
stash@{n}
to indicate stashgit stash pop
: pop topgit stash drop
: drop stash WARNINGgit format-patch <base_commit>
git format-patch -n3 <top_commit>
git diff > name.patch
git apply <patch>
: apply patchgit apply --check
: check no conflict
git am
: apply and commit
git am *.patch
is very usefulgit apply --reject <patch>
git am --continue
git cherry-pick <commit>
git cherry-pick -- continue
git blame -L <start>,<end> <file>
git blame -L
: <function> <file>
Find where program failed
git bisect start
git bisect good <commit>
git bisect bad <commit>
… Do binary search, mark good, bad…git bisect run <shell>