HCM

Happy Coding Monkey

Git Usage

2022-07-25


git

# uncommitted changes since the last commit.
git diff

# diff staged changes.
git diff --cached

git diff HEAD

# diff summary
git diff --stat master HEAD

branch

## changes git remote branch follows
git branch branch_name --set-upstream-to origin/branch_name_b
git branch branch_name --set-upstream-to your_new_remote/branch_name

## unset the upstream
git branch db_proxy_flashsale --unset-upstream

## Find out which remote branch a local branch is tracking
git branch -vv
* master 522c66e [origin/master] sed with regex

## download all tags of all branch
git fetch --all --tags

## delete all merged branches
git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d

history and log

## git graph
git log --all --decorate --oneline --graph
https://stackoverflow.com/questions/1057564/pretty-git-branch-graphs

## see orphan commit(not in any branch)
git reflog

pull with proxy

export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

git pull --recurse-submodules

pull submodules

## for the first time
git submodule update --init --recursive

## update
git pull --recurse-submodules

checkout github pr

# using gh
# https://github.com/mobile-shell/mosh/pull/1104

$ git clone https://github.com/mobile-shell/mosh
&& cd mosh
$ gh pr checkout 1104
$ ./autogen.sh && ./configure && make && sudo make install

# or, merge it manually
$ git remote add mgulick https://github.com/mgulick/mosh.git
$ git fetch mgulick osc-52-clipboard-types

# deps:
# sudo apt install pkg-config zlib1g-dev libssl-dev libncurses-dev libprotoc-dev protobuf-compiler libprotobuf-c-dev
# ./configure --prefix=/usr/local

git-fame: get repository contribution per author

pip3 install git-fame

tailscale git:(main) git-fame
Processing: 100%
Total commits: 2607
Total ctimes: 20339
Total files: 1227
Total loc: 91575
| Author                        |   loc |   coms |   fils |  distribution   |
|:------------------------------|------:|-------:|-------:|:----------------|
| Brad Fitzpatrick              | 41429 |   1279 |    314 | 45.2/49.1/25.6  |
| David Anderson                | 18233 |    546 |    206 | 19.9/20.9/16.8  |
| Earl Lee                      |  6848 |      2 |     85 | 7.5/ 0.1/ 6.9   |
| Josh Bleecher Snyder          |  5243 |    231 |    162 | 5.7/ 8.9/13.2   |
| David Crawshaw                |  3188 |    133 |     54 | 3.5/ 5.1/ 4.4   |
| Dmytro Shynkevych             |  3143 |     61 |     55 | 3.4/ 2.3/ 4.5   |
| Avery Pennarun                |  2921 |     95 |     63 | 3.2/ 3.6/ 5.1   |
| Christine Dodrill             |  1789 |     33 |     34 | 2.0/ 1.3/ 2.8   |
| Maisem Ali                    |  1641 |     14 |     39 | 1.8/ 0.5/ 3.2   |
| Denton Gentry                 |  1022 |     43 |     27 | 1.1/ 1.6/ 2.2   |

using http in git instead of ssh for Github repo

[url "https://github.com/"]
insteadOf = "git@github.com:"

Patch

git format-patch HEAD^ online/ > /tmp/pacth1.diff
git apply -p4 /tmp/patch1.diff
* machine 1
$ git bundle create 1.bundle HEAD~1..HEAD

* machine 2
$ git bundle unbundle /tmp/1.bundle
ac3e72aed537d63acee16d6f72f9c8f04a723c88 HEAD
$ git checkout -b patch1 ac3e72aed537d63acee16d6f72f9c8f04a723c88
$ git checkout master
$ git merge --ff patch1

Use binary search to find the commit

## init
$ git bisect start
$ git bisect bad                 # Current version is bad
$ git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good

## start to binary search commit (use `make test` for test here.)
$ make test
### test passed
$ git bisect good

$ make test
### test failed
$ git bisect bad

$ make test
$ git bisect bad

### ... finally find the bad commit

## fix it

## clean
$ git biset reset

~/.gitconfig

[user]
	email = your email@example.com
	name = cowboy

[core]
	editor = vim

[alias]
        lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=format:'%Y-%m-%d %H:%M' --
        ll = log --all --decorate --oneline --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=format:'%Y-%m-%d %H:%M'

[diff]
	tool = bc3

[pull]
	rebase = false
	ff = only

Use beyond compare as the diff tool and enable the compare option in Rules -> Handling -> Follow symbolic links

credentials

git config --global credential.helper store

Attention: This method saves the credentials in plaintext on your PC’s disk. Everyone on your computer can access it.

cat  ~/.git-credentials
https://userxxx:passwordXXXXX@git.xx.io

Debug

trace

GIT_CURL_VERBOSE=1 GIT_TRACE=1 git pull