Skip to content

Git

~/.bashrc

# git branch in bash prompt
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "

config

# 查看設定
$ git config --list

# 檢查username
$ git config user.name

# 設置user name
$ git config --global user.name "Ted.Changchien"

# 設置email
$ git config --global user.email "Ted.Changchien@zyxel.com.tw"

# 設置commit log template
$ git config --global commit.template /path/to/log-template-file

# 設置commit編輯器
$ git config --global core.editor "vim"

# git log show ^M
$ git config --global core.whitespace cr-at-eol

# alias
$ git config --global alias.tree "log --graph --decorate --pretty=oneline --abbrev-commit"

# filemode
$ git config core.filemode false

unset

git config --global --unset user.name
git config --global --unset user.email

# edit directly
git config --global --edit

remote

# 設置遠端倉庫的URL
$ git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
$ git remote set-url origin git@192.168.188.95:UploadPlatform.git

# 更改遠端主機資訊
$ git remote -v
$ git remote rm origin
$ git remote add origin git@git-server:project_name

# 連結 remote repository 的 branch 到新建的 local branch
$ git branch --set-upstream-to=origin/feature3 feature3

# 刪除遠端分支
$ git push origin :feature_a

# clean up the reference to the deleted remote branch from your local repository
$ git fetch origin --prune 

stash

暫存working directory 想要拉回遠端repo的新commit但想暫時保存目前還沒有做local commit且正在修改的部分

git stash
git stash apply
git stash drop
git stash pop
git stash show -p stash@{0}

tags

# show tags
git tag -l
git tag -a v1.2.1b3
git push origin v1.2.1b3

# 對過去的commit上加入標籤
git tag -a v1.3.0b2s2 9fceb02

# Delete the tag from the repository
git tag -d <tag name>

git fetch --tags
git push origin --tags

log

# search log by commit message
git log --grep='JIRA ID'

# log filter by author
git log --author="Jon"

# 顯示檔案資訊
git log --name-status

# find commit by hash sha in git
git show a2c25061
git log a2c25061 -n 1
git show a2c25061 --stat

# view the change history of a method/function
git log -L :function:path/to/file

# find a deleted file in the project commit history
# do not know the exact path
git log --all --full-history -- "**/thefile.*"
#  know the path the file was at
git log --all --full-history -- <path-to-file>
https://stackoverflow.com/questions/7124914/how-to-search-a-git-repository-by-commit-message how do I view the change history of a method/function git - How to find a deleted file in the project commit history? - Stack Overflow

patch

# 製作最近n個commit的patch
git format-patch -n

# 製作從指定起始commit到結束commit的patch
git format-patch 5e86795..f2b286a
# 設定輸出的資料夾
git format-patch 5e86795..f2b286a -o {Dir Name}

# 檢查套用patch是否會有問題
git apply --check {patch_file}

# 實際套用patch
git am {patch_file}
notes: []會消失

push

pull

add

Commit only part of a file in Git

例如: refactory時改import, 但其他部分和refactory無關 https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git

git add -p <filename>

Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
Here is a description of each option:

y stage this hunk for the next commit
n do not stage this hunk for the next commit
q quit; do not stage this hunk or any of the remaining hunks
a stage this hunk and all later hunks in the file
d do not stage this hunk or any of the later hunks in the file
g select a hunk to go to
/ search for a hunk matching the given regex
j leave this hunk undecided, see next undecided hunk
J leave this hunk undecided, see next hunk
k leave this hunk undecided, see previous undecided hunk
K leave this hunk undecided, see previous hunk
s split the current hunk into smaller hunks
e manually edit the current hunk
? print hunk help

bundle

Commit

real world example

diff

what changes between two commits and only the filename Git: How to show only filenames for a diff - makandra dev

git diff [commit a]..[commmit b] --stat

git diff [commit a]..[commmit b] --name-only

Flow

持續發布 版本發布

參考

Rebase

Conflict

Revert

某個 Commit 反向的操作 - eset、Revert 跟 Rebase 指令有什麼差別

Discard change

# discard unstaged changes
git checkout -- .
git checkout -- path/to/file/to/revert
# restore all changes
git restore .

cherry pick

# To cherry-pick all the commits from commit A to commit B (where A is older than B)
git cherry-pick A^..B

# If you want to ignore A
git cherry-pick A..B
- How to cherry-pick multiple commits

sparse checkout

bisect

submodule

# 針對 單一個 submodule 做 remote update
git submodule update --remote path/to/submodule

fix corrupt submodule - git submodules - could not get a repository handle for submodule - Stack Overflow

rm -rf .git/modules/the_submodule/ the_submodule/
git submodule init "the_submodule"
git submodule update

LFS

protect secret leak

工作原理

hook

post-commit

#! /bin/bash


python -m venv /tmp/doc
source /tmp/doc/bin/activate
pip install grip

cd doc && grip user_guide.md --export index.html