分类
devops

几点git用法

git daemon

mkdir your-project

cd your-project
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:your/your-project.git
git push -u origin main

cd ../

git daemon --reuseaddr --base-path=. --export-all --verbose

ss -tunlp |grep git
tcp   LISTEN 0      5                 0.0.0.0:9418       0.0.0.0:*    users:(("git-daemon",pid=1597700,fd=3))  
tcp   LISTEN 0      5                    [::]:9418          [::]:*    users:(("git-daemon",pid=1597700,fd=4)) 


git clone git clone git://127.0.0.1/your-project

SSH方式

# -C "your_email@example.com"
# -f bb
# -t rsa
# ~/.ssh/id_rsa.pub
# ~/.ssh/id_rsa
ssh-keygen -t rsa -b 4096 -N '' -m PEM

-m formatThe supported key formats are: “RFC4716” (RFC 4716/SSH2 public or private key), “PKCS8” (PKCS8 public or private key) or “PEM” (PEM public key). By default OpenSSH will write newly-generated private keys in its own format, but when converting public keys for export the default format is “RFC4716”.
https://man7.org/linux/man-pages/man1/ssh-keygen.1.html

-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa
         Specifies the type of key to create.  The possible values
         are “dsa”, “ecdsa”, “ecdsa-sk”, “ed25519”, “ed25519-sk”, or
         “rsa”.
        This flag may also be used to specify the desired signature
         type when signing certificates using an RSA CA key.  The
         available RSA signature variants are “ssh-rsa” (SHA1
         signatures, not recommended), “rsa-sha2-256”, and
         “rsa-sha2-512” (the default).
ssh-keygen -i -f ssh2.pub > openssh.pub
ssh-keygen -e -f openssh.pub > ssh2.pub

How to convert an SSH2 Public Key into an OpenSSH public key
Differences between “BEGIN RSA PRIVATE KEY” and “BEGIN OPENSSH PRIVATE KEY”

ProxyCommandhttp代理ssh(http over ssh)

## http 协议的代理
GIT_SSH_COMMAND='ssh -o ProxyCommand="nc -X connect -x 192.168.1.100:8118 %h %p"' git clone git@github.com:kubernetes/kubernetes.git

## SOCK5 协议的代理
GIT_SSH_COMMAND='ssh -o ProxyCommand="nc -X 5 -x 192.168.1.100:3000 %h %p"' git clone git@github.com:kubernetes/kubernetes.git

## 注此nc为  netcat-openbsd(不能选择netcat-traditional)

或者写入~/.ssh/config文件

Host github.com
    HostName github.com
    User git
    ProxyCommand nc -X connect 192.168.1.100:8118 %h %p

更改仓库地址

git config -l && \
git remote remove origin && \
git remote add origin git@github.com:dyrnq/dist.git && \
git config -l

强制回滚

git reset --hard 70fefa9854e587e1d7855a18978f463bcf84f18b && git push origin HEAD --force

第一次git clone ssh提示

The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no)? 

方式1

ssh-keygen -f "~/.ssh/known_hosts" -R github.com

ssh-keyscan -H github.com >> ~/.ssh/known_hosts

方式2

GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone user@host

方式3

GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking accept-new" git clone user@host

参考https://stackoverflow.com/questions/7772190/passing-ssh-options-to-git-clone

其他

commit之后push之前想要撤回

git reset --soft HEAD^
git push
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

如果是以SSH方式连repo,确认本机id_rsa.pub拷贝到git server

git push
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/dyrnq/dist.git/'

https://github.com/settings/tokens创建Personal access tokens

git config

git config --local user.name "dyrnq" && git config --local user.emal "dyrnq@qq.com";
git config --global user.name "dyrnq" && git config --global user.email "dyrnq@qq.com"

git config --global core.editor "vim"
git config --global pull.ff only

git config --global http.proxy http://192.168.8.100:8118
git config --global https.proxy http://192.168.8.100:8118

git config --global --unset http.proxy
git config --global --unset https.proxy

git config --list --local
git config --list --global

create main branch

# 创建 git 仓库
mkdir example
cd example
git init
touch README.md
git add README.md
git commit -m "first commit"
git branch -M "main"
git remote add origin git@gitee.com:dyrnq/example.git
git push -u origin "main"

remak nc openbsd

nc -h
OpenBSD netcat (Debian patchlevel 1.219-1)
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
      [-m minttl] [-O length] [-P proxy_username] [-p source_port]
      [-q seconds] [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit]
      [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
      [destination] [port]
    Command Summary:
        -4      Use IPv4
        -6      Use IPv6
        -b      Allow broadcast
        -C      Send CRLF as line-ending
        -D      Enable the debug socket option
        -d      Detach from stdin
        -F      Pass socket fd
        -h      This help text
        -I length   TCP receive buffer length
        -i interval Delay interval for lines sent, ports scanned
        -k      Keep inbound sockets open for multiple connects
        -l      Listen mode, for inbound connects
        -M ttl      Outgoing TTL / Hop Limit
        -m minttl   Minimum incoming TTL / Hop Limit
        -N      Shutdown the network socket after EOF on stdin
        -n      Suppress name/port resolutions
        -O length   TCP send buffer length
        -P proxyuser    Username for proxy authentication
        -p port     Specify local port for remote connects
        -q secs     quit after EOF on stdin and delay of secs
        -r      Randomize remote ports
        -S      Enable the TCP MD5 signature option
        -s sourceaddr   Local source address
        -T keyword  TOS value
        -t      Answer TELNET negotiation
        -U      Use UNIX domain socket
        -u      UDP mode
        -V rtable   Specify alternate routing table
        -v      Verbose
        -W recvlimit    Terminate after receiving a number of packets
        -w timeout  Timeout for connects and final net reads
        -X proto    Proxy protocol: "4", "5" (SOCKS) or "connect"
        -x addr[:port]  Specify proxy address and port
        -Z      DCCP mode
        -z      Zero-I/O mode [used for scanning]
    Port numbers can be individual or ranges: lo-hi [inclusive]

ref