基本設定

全域設定(雙引號不要少打)

git config --global user.name "your name"
git config --global user.email "yourEmail@email.com"

現在上傳到github上不能用帳號密碼上傳,要用帳號和token(去Developer settings那產生token),
如果不想每次上傳都要打帳號和token,可以在上傳一次之後,再打上
「2022/11/13更新」先用credential再打帳號和token(password)

git config --global credential.helper store

可以儲存帳號和token。

如果想要在單個repository上傳的人不同,就輸入

git config --local user.name "your name"
git config --local user.email "yourEmail@email.com"


Git gui

How to run the gui mode of git in ubuntu?

git citool

or

gitk


基本git指令

一次加入全部更新的資料

// three types
git add .
//
git add *
//
git add --all

Except adding files or directory contains
// -- let git know path
// windows
git add --all -- :!path/to/file :!path/to/*

// mac and linux
git add --all -- ':!path/to/file' ':!path/to/*'

拿掉加入的資料


git reset filename.extension


查看push歷史記錄

git log

查看現在branch的push位置

用完後會回到那次push的樣子(id五碼就行了)
git checkout commit_id

在現在的位置開新的branch

git checkout -b new_branch_name

強制重置push位置

git log
git reset --hard "5 digits commit id"

換branch

git switch branchName

刪除branch

git branch -d branchName

merge branch

確認現在的branch(有星號的)
git checkout
其他branch的內容加到現在的branch裏面, 但兩個branch不會消失
git merge other_branch

fetch和pull差異

pull用法比fetch簡單,pull簡單來說是fetch和merge的組合。
fetch會下載github(或其他放code的地方)上最新更改的內容,但不會更改現在local的資料,這樣可以確保不會因爲merge造成相同資料損壞的風險。
stackoverflow - What is the difference between 'git pull' and 'git fetch'?

remote

連接online code repository:
git remote add remoteName https://gitRepository...
remoteName是自己命名的,後面網址是連接code repository的地方。

推送時的指令:
git push -u remoteName branchName
一般預設remoteName是origin,branchName是master。
即 git push
等於 git push -u origin master
沒有特別branch的話clone回來的repository可以直接用git push。

push help說明: -u, -f 參數
       -u, --set-upstream
           For every branch that is up to date or successfully pushed, add
           upstream (tracking) reference, used by argument-less git-pull(1)
           and other commands. For more information, see branch."name".merge
           in git-config(1).

       -f, --force
           Usually, the command refuses to update a remote ref that is not an
           ancestor of the local ref used to overwrite it. Also, when
           --force-with-lease option is used, the command refuses to update a
           remote ref whose current value does not match what is expected.

           This flag disables these checks, and can cause the remote
           repository to lose commits; use it with care.

           Note that --force applies to all the refs that are pushed, hence
           using it with push.default set to matching or with multiple push
           destinations configured with remote.*.push may overwrite refs other
           than the current branch (including local refs that are strictly
           behind their remote counterpart). To force a push to only one
           branch, use a + in front of the refspec to push (e.g git push
           origin +master to force a push to the master branch). See the
           ...  section above for details.

查看log

可以看所有的commit

git shortlog

參數: -n 依那個人上傳commit次數排列

git shortlog -n

參數: -e 依那個人上傳的email排列

git shortlog -e

參數: -s 那個人上傳總數

git shortlog -s


git log 參數

--skip 跳過最新幾個log
假設不要顯示最近10個commit的歷史記錄

git log --skip=10


Filter logs with date range

參數可以是指定的日期之後 或 指定日期之前 或 一個範圍
假設想要知道2022-01-01到2022-04-01的log

git log --since=2022-01-01 --until=2022-04-01


關鍵字尋找log關鍵字

大小寫有區別
假設要查有關Merge的關鍵字

git log --grep="Merge"


git clean

clean 常需要 -f or --force 來強制刪除
-d 刪除沒有被track的檔案和資料夾
-n or --dry-run 不會刪除資料, 但會顯示哪些會被刪除。
-q or --quiet 如果刪除成功,不會顯示出來,除了有錯誤訊息。
-e<pattern> or --exclude=<pattern> 刪除有關鍵字的檔案。

tag

tag有分Lightweight和有Annotated,簡單來說lightweight是沒有注解說明, 而Annotated是有注解説明的。
tag類似branch,但不同的是,tag是只記錄當下的狀態,不會因爲commit之後就改變了tag的內容。
tag適用區分版本的節點。
Lightweight:

git tag tagName

Annotated:

git tag -a tagName -m "tag note here"

可以用checkout去轉成tag的地方

git checkout tagName

刪除tag:

git tag -d tagName


Rename commit


git commit --amend -a

or

git commit --amend -m "new commit message"


git commit錯誤,但還沒push

git reset --soft <5 digits>