Skip to content

Instantly share code, notes, and snippets.

@hochun836
Last active November 6, 2023 06:15
Show Gist options
  • Select an option

  • Save hochun836/0c6b9cd3b441da4462cdff95be2a932c to your computer and use it in GitHub Desktop.

Select an option

Save hochun836/0c6b9cd3b441da4462cdff95be2a932c to your computer and use it in GitHub Desktop.
# base
# config
<some-folder>/.git/config // local
%USERPROFILE%/.gitconfig // global
%GIT_HOME%/etc/gitconfig // system
# concept
-> add -> commit -> push
unstaging area | staging area | local repository | remote repository
(變更區) (暫存變更區) (本地倉庫) (遠端倉庫)
clone <-
fetch
pull (= fetch + merge)
IMPORTANT: branch action
- branch / checkout
- merge / rebase
Q: merge vs. rebase
=> ref: https://backlog.com/git-tutorial/tw/stepup/stepup1_4.html
=> ref: https://www.maxlist.xyz/2020/05/02/git-merge-rebase/
#
merge
- git merge <branch>
- commit merge (=> the same file, auto merge => if fail, conflict)
# command
git --version
git --help
git help -a // list available commands
git help -g // list concept guides
git help <command>
git help <concept>
git config -h
git config --global --list
git config --global <section>.<variable> // getter
git config --global <section>.<variable> <value> // setter
git config --global user.name
git config --global user.name <name>
git config --global user.email
git config --global user.email <email>
git config user.name
git config user.name <name>
git config user.email
git config user.email <email>
git config --global --get-regexp alias // list aliases
git config --global --list | grep alias // list aliases
git config --global alias.ls "log --oneline --graph --all" // set an ls alias
git config --global --unset alias.ls // unset an ls alias
git config --global core.editor notepad2 // change editor to notepad2
git config --global core.editor code // change editor to vscode
git config --global --edit // open .gitconfig by editor
git config --global --add aaa.xxx 100
git config --global --add aaa.yyy 200
git config --global --add aaa.zzz 300
git config --global --remove-section aaa
git config --global push.default
git config --global http.sslVerify <true|false>
git config --global http.sslCAInfo <path-of-ca-bundle.crt>
git clone -h
git clone <remote-repo>
git clone <remote-repo> <local-dir>
git init -h
git init // create .git directory
git init -q // -q <=> --quiet
touch .gitignore // create .gitignore (for linux)
notepad .gitignore // create .gitignore (for windows)
git status -h
git status // if the current branch has been set upstream, then also display upstream
git log -h
git log --oneline --all --graph
git add -h
git add .
git commit -h
git commit // switch to input message
git commit -m "<message>"
git commit -a // commit all changed files (but not include untracked files)
git commit -a -m "<message>"
git commit --allow-empty -m "<message>" // create a commit with empty contents
git push -h
git push
git push -f // -f <=> --force
git push --following-tags // push commits and tags
git push --tags // only push tags
git pull -h
git pull
git pull -v
git tag -h
git tag <name> // add a tag
git tag -d <name> // delete a tag
git branch -h
git branch // show all branches (the current branch starts with *)
git branch -v // -v <=> --verbose
git branch -vv // show also with the upstream branch
git branch -a // -a <=> --all
git branch -a -vv
git branch <new-branch> // new a branch
git branch -M <new-name> // move/rename a branch, even if target exists
git branch -d <branch> // -d <=> --delete
git branch -d -f <branch> // -f <=> --force
git branch -D <branch> // -D <=> --delete --force
git branch -u <remote-name>/<remote-branch-name> // set upstream
git checkout -h
git checkout <branch> // switch to the specified branch, if no exists then error
git checkout -b <new-branch> // switch to the new branch after creating
git checkout -b <new-branch> -t <remote-name>/<remote-branch-name> // -t <=> --track
git checkout --orphan <new-branch> // new unparented branch (see: git status)
git reset -h
git reset --hard <commit>
git remote -h
git remote // show the remote
git remote -v // -v <=> --verbose
git remote add <name> <url>
git remote rename <old-name> <new-name>
git remote remove <name>
git remote set-url <repo> // change fetch and push
git remote set-url --push <repo> // only change push
# [note] create new branch with empty folder structure
git checkout --orphan <new-branch>
git stauts
git rm -rf .
git stauts
=> ref: https://gist.github.com/j8/8997663
# [note] encountering 'SSL certificate problem: unable to get local issuer certificate'
- step1. look for the ca-bundle.crt path
git config --list | findstr http.sslCAInfo // look for all http.sslCAInfo
git config http.sslCAInfo // look for the efficient http.sslCAInfo
- step2. copy the ca-bundle.crt to the new path
- step3. add the public key to the new ca-bundle.crt
- step4. use the new ca-bundle.crt
git config --global http.sslCAInfo <path-of-ca-bundle.crt>
- step5. look for the ca-bundle.crt path again
git config --list | findstr http.sslCAInfo // look for all http.sslCAInfo
git config http.sslCAInfo // look for the efficient http.sslCAInfo
=> ref: https://blog.darkthread.net/blog/vs2017-git-ssl-issue
# [note] credential helper
cache --timeout=<seconds>
store // ~/.git-credentials
manager // credential manager (windows, macos)
wincred // credential manager (windows)
osxkeychain // credential manager (macos)
manager-core
=> ref: https://blog.miniasp.com/post/2018/05/28/Git-Credential-Howto
# [note] gui
=> ref: https://git-scm.com/downloads/guis/
# [note] learn
=> ref: https://www.bookstack.cn/books/git-tutorial
git push <url-name> <branch-name> 把本機某個分支的內容,推向遠端倉庫
git push -u <url-name> <branch-name> 把本機某個分支的內容,推向遠端倉庫,同時設定好此分支的上游 (upstream - 上游)
git push <url-name> <local-branch-name>:<remote-branch-name>
git commit -m "xxxx" -m "yyyy" -m "zzzz" 將 "暫存變更" commit 到本地倉庫,由多個 -m 合併成 commit message
git commit -F <file> 將 "暫存變更" commit 到本地倉庫,由"指定檔案"的內容作為 commit message (-F <=> --file)
git push heroku master 將本地倉庫的 commit push 上遠端倉庫 (此處範例為: heroku)
git log 查看"目前"分支的 commit 紀錄
git log --oneline --all --graph -p 查看"所有"分支的 commit 紀錄,每個紀錄以一行顯示,用線圖顯示,並顯示檔案 commit 做了什麼修改
git log <branch-name> 查看"某個"分支的 commit 紀錄
git log <filename> 查看"目前"分支"某個"檔案的 commit 紀錄
git log -p <filename> 查看"目前"分支"某個"檔案的 commit 紀錄,並顯示檔案 commit 做了什麼修改
git diff 查看工作目錄與尚未進暫存區(unstaged)全部檔案的差異
git diff --cached 查看工作目錄與尚已進暫存區(staged)全部檔案的差異
git diff <filename> 查看工作目錄與尚未進暫存區(unstaged)檔案<filename>的差異
git diff <commit-id> 查看所在的分支上<commit-id>跟HEAD之間的差異
git diff <old commit-id> <new commit-id> 查看兩個commit-id之間的差異
git diff-tree -r --name-only <commit-id> 列出某個commit-id的異動檔案
git show 顯示"最新" commit 紀錄,並顯示檔案 commit 做了什麼修改
git show <commit-id> 顯示"某個" commit 紀錄,並顯示檔案 commit 做了什麼修改
git show -s <commit-id> 顯示"某個" commit 紀錄,僅此而已
git show --name-only <commit-id> 顯示"某個" commit 紀錄,並顯示 commit 了哪些檔案(名稱)
git show <filename> 顯示"最新" commit 紀錄中,"某個"檔案 commit 做了什麼修改
git fetch 把目前線上有,但本機沒有的內容,抓了一份下來,同時移動 origin 相關的分支
git rebase <branch-name> 將"目前"分支 rebase "指定"分支
git merge <branch-name> 將"目前"分支 merge "指定"分支 ex. git merge origin/master
git merge <branch-name> --no-ff --no-ff 參數是「不要使用快轉模式合併」的意思,這樣一來就會額外做出一個 Commit
git merge <branch-name> -m <message> merge by using meaningful message
git pull <url-name> <branch-name> 把遠端倉庫的某個分支的內容,拉回本機
git reset <commit-id> (絕對) reset 應視作 go to (前往)
git reset master^ (相對) 每一個 ^ 符號,表示「前一次」的意思
git reset master^^ (相對) 有兩個 ^ 符號,代表往前兩次
git reset master~1 (相對) 等價於 git reset master^
git reset master~2 (相對) 等價於 git reset master^^
git reset master~2 --soft Commit 拆出來的檔案,丟回暫存區
git reset master~2 --mixed Commit 拆出來的檔案,丟回工作目錄
git reset master~2 --hard Commit 拆出來的檔案,直接丟掉
git blame <filename> 查看某個檔案的某一行是誰寫的
git blame -L 5,10 <filename> 查看某個檔案的第幾行到第幾行是誰寫的
git checkout <branch-name> 切換分支
git checkout -- <filename> 使 tracked file 回到未修改前的樣子, git checkout: Switch branches or restore working tree files
git checkout -- . 使全部 tracked files 回到未修改前的樣子
git clean -n 查看將被刪除的檔案 (dry run - 空運行), git clean: Remove "untracked" files from the working tree
git clean -i 以交互的方式來刪除檔案
git clean -f 以強制的方式來刪除檔案
git clean -f -d (-fd) 以強制的方式來刪除 directories
git clean -f -X (-fX) 以強制的方式來刪除 ignored files
git clean -f -x (-fx) 以強制的方式來刪除 ignored files and non-ignored files
git stash 將目前所有的修改"藏"起來 (Untracked 狀態的檔案預設沒辦法被 Stash ,需要額外使用 -u 參數)
git stash -u 將目前所有的修改"藏"起來,包含 Untracked 狀態的檔案
git stash list 查看所有"藏"起來的紀錄
git stash pop <stash@{x}> 將某個"藏"起來的紀錄撿回來做,套用在目前的分支,套用後該 Stash 就會被刪除
git stash pop 沒有指定要 pop 哪一個 Stash ,會從編號最小的,也就是 stash@{0} 開始拿 (也就是"最後(最新)"疊上去的那次)
git stash show <stash@{x}> 查看某個"藏"起來的紀錄裡面有什麼
git stash drop <stash@{x}> 丟棄某個"藏"起來的紀錄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment