[ Git ] 创建 本地/远程 仓库 & [ SSH ] 免密 push

https://www.cnblogs.com/yeungchie/

SSH 免密登录

生成 SSH 密钥/公钥

ssh-keygen

发送 SSH 公钥

ssh-copy-id yeungchie@home-nas

初始化本地仓库 init

mkdir repo
cd repo
git init

Initialized empty Git repository in ~/repo/.git/

添加远程仓库 remote add

git remote add nas git@nas.com:yeungchie/repo.git
git remote

nas

拉取远程仓库 pull

git pull nas main

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.07 KiB | 4.00 KiB/s, done.
From nas.com:yeungchie/repo

  • branch main -> FETCH_HEAD
  • [new branch] main -> nas/main

提交本地改动

添加改动到暂存区 add

git add .

提交暂存区 commit

git commit -m 'new'

[main 20791ab] new
1 file changed, 36 insertions(+)
create mode 100644 newfile.sh

分支 branch

查看分支

git branch
git branch -a              # 查看所有分支 包括远程仓库
git branch --show-current  # 查看当前所在分支

切换分支

git checkout $branchName

推送 push

将远程设置为上游并 push

  • 第一次需要这么操作
git push --set-upstream nas main

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 956 bytes | 956.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by NAS.COM [GNK-6.0]
To nas.com:yeungchie/repo.git
a9a16fa..20791ab main -> main
Branch 'main' set up to track remote branch 'main' from 'nas'.

后面 push 只需要这样

需要确保当前分支为远程仓库主分支 否则需要指定分支

git push nas
git push nas main  # 指定 main 分支

Everything up-to-date

拓展内容

SSH config 格式

Host alias_name                     # HOST100
    HostName        ip_address      # 192.168.1.100
    Port            port_number     # 22
    User            login_name      # yeung
    IdentityFile    ~/.ssh/id_rsa
    # PerferredAuthentications publickey

快速 push 脚本

  • 自动识别已添加的远程仓库并 push
#!/bin/bash
remotes=`git remote 2>&1`
if [[ ! $remotes =~ 'fatal: not a git repository' ]]; then
    for r in $remotes; do
        echo "git push $r"
        git push $r
    done
fi

Git Bash 命令输出中文显示异常

git config --global core.quotepath false

Git 知识大全 - Gitee.com

posted @ 2021-07-17 22:49  YEUNGCHIE  阅读(270)  评论(0编辑  收藏  举报