前置需求

配置用户名和邮箱

这个是commit的用户信息, 会展示在github或其他gitlab平台上的commit history中.

git config --global user.name "<username>"
git config --global user.email "<email@server>"

(可选) 配置默认分支

git config --global init.defaultBranch

或者使用编辑器:

git config --global -e

进入editor, 编辑git config. 写入:

[init]
	defaultBranch = master

(可选) 配置ssh

Generate Key

创建ssh公钥私钥:

ssh-keygen -t ed25519

可选参数:

ssh-keygen: illegal option -- -
usage: ssh-keygen [-q] [-a rounds] [-b bits] [-C comment] [-f output_keyfile]
                  [-m format] [-N new_passphrase] [-O option]
                  [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]
                  [-w provider] [-Z cipher]
       ssh-keygen -p [-a rounds] [-f keyfile] [-m format] [-N new_passphrase]
                   [-P old_passphrase] [-Z cipher]
       ssh-keygen -i [-f input_keyfile] [-m key_format]
       ssh-keygen -e [-f input_keyfile] [-m key_format]
       ssh-keygen -y [-f input_keyfile]
       ssh-keygen -c [-a rounds] [-C comment] [-f keyfile] [-P passphrase]
       ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
       ssh-keygen -B [-f input_keyfile]
       ssh-keygen -D pkcs11
       ssh-keygen -F hostname [-lv] [-f known_hosts_file]
       ssh-keygen -H [-f known_hosts_file]
       ssh-keygen -K [-a rounds] [-w provider]
       ssh-keygen -R hostname [-f known_hosts_file]
       ssh-keygen -r hostname [-g] [-f input_keyfile]
       ssh-keygen -M generate [-O option] output_file
       ssh-keygen -M screen [-f input_file] [-O option] output_file
       ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]
                  [-n principals] [-O option] [-V validity_interval]
                  [-z serial_number] file ...
       ssh-keygen -L [-f input_keyfile]
       ssh-keygen -A [-a rounds] [-f prefix_path]
       ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
                  file ...
       ssh-keygen -Q [-l] -f krl_file [file ...]
       ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file
       ssh-keygen -Y match-principals -I signer_identity -f allowed_signers_file
       ssh-keygen -Y check-novalidate -n namespace -s signature_file
       ssh-keygen -Y sign -f key_file -n namespace file [-O option] ...
       ssh-keygen -Y verify -f allowed_signers_file -I signer_identity
                  -n namespace -s signature_file [-r krl_file] [-O option]
Link to original

创建新的git仓库

进入需要创建git仓库的文件夹, 使用下面的指令新建git repo:

git init

设置远程仓库

如果有远程仓库, 可以通过下面命令列出:

git remote -v

添加远程仓库:

git remote add <remote_name> <remote_url>

一般而言, remote_name是origin, remote_url是github仓库(或者其他的gitlab平台仓库)的url, 可以使用ssh: git@github.com:<username>/<repo>

修改远程仓库: 如果需要修改现存的remote仓库(如, 远程仓库重命名等), 可以使用下面的命令修改已存在的remote url:

git remote set <remote_name> <remote_url>

设置上游仓库

Push

在任何commit提交之后, 可以使用下面的方法将本地的分支推送到远程:

git push <remote_name> <local_branch>:<remote_branch>

如果local_branchremote_branch相同, 可以只输入一次:

git push <remote_name> <branch>

每次推送都需要输入remote_namebranch, 可以设置上游来免于重复输入:

git push -u <remote_name> <branch>

后续的推送会自动读取upstream, 命令可以简化:

git push
Link to original