此处记录了一些git使用的笔记

显示本地未push的commits

git log origin..
git log origin/master..HEAD

恢复到某一个commits

git reset d6e63190
git reset --soft HEAD@{1}
git commit -m 'reset to xxxx'
#Update working copy to reflect the new commit
git reset --hard

update a branch without checkout it

git checkout dev; # now in dev branch
git fetch origin master:master; 

remove any old, conflicting branches

git remote prune origin

diff of local branch and remote branch

git fetch origin 
git diff dev origin/dev

git show history

git log --oneline --decorate --graph --all
git log --name-status
git log --stat
git log --stat --pretty=short --graph

# 显示详细的改动信息
git log -p

# 用patch的形式显示最后一次commit
git log -p HEAD^..HEAD

delete tag

git push origin :tagname
git push --delete origin tagname
git tag --delete tagname

bundle

git bundle create ../my-db.bundle d7e11e3bd..HEAD
git bundle create ../my-db.bundle --since=10.days master

move back to where the origin is

git reset --hard origin/develop

reset to previous version

git reset --hard 56e05fced
git reset --soft HEAD@{1}
git diff --cached
git commit

show git version

git describe
git describe  --tags

git deploy

#https://grimoire.ca/git/stop-using-git-pull-to-deploy
git fetch --all
git checkout --force origin/master
git checkout --force 63d0a6c
git checkout --force v5.2

set local branch track to upstream

git branch --set-upstream ssl-test origin/ssl-test

show git commit hash

git rev-parse --short HEAD

archive

git archive master | tar -x -C /somewhere/else
git archive --format zip --output /full/path/to/zipfile.zip master 
git archive master | gzip > latest.tgz

diff, current head to some branch

git diff HEAD master

reset local branch to origin branch

# https://gist.github.com/Chaser324/ce0505fbed06b947d962

git fetch origin
git reset --hard origin/master

git clean -f

grep in history

git grep TODO $(git rev-list --all)

# 显示涉及到这个关键字的提交
git log -G TODO

# 显示涉及到这个关键字的提交,并显示diff
git log  -p -G TODO

set master to a branch.

# https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

abort a merge

git merge --abort

checkout remote branch

git checkout -t origin/ipv6

设置alias

git config --global --add alias.lg  "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

git lg

rebase

git fetch upstream
git checkout master
git rebase upstream/master
git push -f origin master

显示被忽略的文件

git status --ignored

列出文件的最后一次提交时间

git ls-tree -r --name-only HEAD | while read filename; do
	echo "$(git log -1 --format="%ai" -- $filename) $filename"
done

submodule

git submodule init
git submodule update --rebase --remote

个人仓库开发

git clone [email protected]:abc/xyz.git xyz
git remote add upstream XXXXX
git fetch upstream
git checkout -b fix-xxx upstream/master
git commit -a -m 'change ...‘
git push origin fix-xxx

恢复master

git fetch upstream
git checkout master
git rebase upstream/master
git push -f origin master

克隆大仓库

可以先设置depth=1

git clone --depth=1 https://github.com/v8/v8.git

git fetch --unshallow
git fetch --tags


# if you keep getting aborted fetch, you can run this in loop
git fetch --depth=100

# or this
git fetch --deepen=100

使用patch来在本地检查MR代码

从gitlab上下载diff文件

patch -p1 <~/Downloads/2464.diff

显示一个tag的commit id

git rev-list -n 1 $TAG

暂存改动

git stash
git pull --rebase
git stash pop

reset

git add a.txt
# if you do not want a.txt to be commited
git reset a.txt

# reset all
git reset

using github-cli

pacman -S github-cli

# login
gh auth login

# clone large project
gh repo clone ClickHouse/ClickHouse -- --depth 1

github 连接报错

往github push时经常报错

wang@archlinux:~/xxx/ >  git push origin main 
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

参考了下这个 https://stackoverflow.com/questions/15589682/ssh-connect-to-host-github-com-port-22-connection-timed-out

~/.ssh/config增加了配置

Host github.com
Hostname ssh.github.com
Port 443

原因应该是防火墙的某种限制,导致无法用ssh连接github.com,也可以试下不加Port 443,这样至少换了连接的主机,如果依然不能成功,可以加上port 443, 这样会使用443端口进行ssh连接,防火墙一般不会做限制。