文章目录
git相关原理图解(图1来自菜鸟教程)
1 远程仓库操作
PS > md local_repo_name
PS > cd local_repo_name
# 1、初始化本地仓库
PS > git init
# 2 手动添加一个新的远程 Git 仓库:git remote add <shortname> <url>
PS > git remote add nickname git@gitee.com:***/xxx.git
# git remote add origin git@github.com:***/xxx.git
# 注:
# 添加的同时指定一个简写,在命令行中可使用指定的简写来代替整个 URL
# git clone 命令可自动添加远程仓库
# 3 查看添加的远程仓库:git remote -v 或 git remote show nickname
PS > git remote -v
nickname git@gitee.com:***/xxx.git (fetch)
nickname git@gitee.com:***/xxx.git (push)
# 4 推送到远程仓库:git push <remote> <branch>
PS > git add .
PS > git commit -m "2022-07-26 commit"
PS > git push nickname master
# 5 从远程仓库拉取信息
PS > git fetch nickname
# fetch 拉取远程仓库中有但本地仓库没有的信息
# 6 移除远程仓库
PS > git remote remove nickname
2 设置免密推送/拉取
这里免密操作通过ssh实现。通过配置 ssh 公钥,并关联本地仓库和远程仓库,可避免命令行 git 操作需要输入账户密码的问题。
1、首先确认 github/gitee 绑定的邮箱账户
2、使用 ssh 生成密钥对
3、将生成的 ssh 公钥添加到 github/gitee
4、关联本地仓库与远程仓库
# 1、生成 ssh 密钥对
#ssh-keygen -t rsa -C "***@***.com"
ssh-keygen
# 2、将公钥添加到 github/gitee 后测试是否添加成功
#ssh -T git@github.com
ssh -T git@gitee.com
# 3、关联本地仓库和远程仓库
git remote add <remote> https://gitee.com/用户名/仓库名.git # git remote remove origin
git remote set-url <remote> git@gitee.com:用户名/仓库名.git
2.1 确认 github/gitee 绑定的邮箱账户
2.2 生成 ssh 密钥对
执行命令ssh-keygen -t rsa -C "***@foxmail.com
或ssh-keygen
,会默认在用户家目录.ssh
文件夹下生成 SSH 密钥对
# 默认回车即可
PS > ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\fy/.ssh/id_rsa):
C:\Users\fy/.ssh/id_rsa already exists.
Overwrite (y/n)?
PS > ls ~\.ssh
Directory: C:\Users\fy
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2022/7/26 15:17 2.54KB id_rsa
-a--- 2022/7/26 15:17 573 id_rsa.pub
2.3 将SSH公钥添加到 GitHub/gitee
将文件~\.ssh\id_rsa.pub
内的全部内容复制,粘贴到 GitHub/gitee 对应位置
PS > cat ~\.ssh\id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAA...
github
gitee
注意:使用gitee的话,需要将自己gitee账户绑定的邮箱设为公有,否则push等操作的时候会出错。
# 验证
PS > ssh -T git@gitee.com
Warning: Permanently added the ECDSA host key for IP address '***.64.63.***' to the list of known hosts.
Hi ***! You've successfully authenticated, but GITEE.COM does not provide shell access.
2.4 关联本地仓库与远程仓库
but GITEE.COM does not provide shell access.
# 进入本地git仓库,执行:
# git remote set-url <remote> git@github.com:用户名/仓库名.git
PS > git remote add origin https://gitee.com/fy-gitee/backups.git
PS > git remote set-url origin git@gitee.com:fy-gitee/backups.git
3 示例
1、仓库地址模板:
# ssh
git@gitee.com:用户名/仓库名.git
git@github.com:用户名/仓库名.git
# https
https://gitee.com/用户名/仓库名.git
https://github.com/用户名/仓库名.git
2、简易的命令行入门教程:
Git 全局设置:
git config --global user.name "xxx"
git config --global user.email "xxx@yyy.com"
创建 git 仓库:
mkdir 仓库名
cd 仓库名
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/用户名/仓库名.git # git remote remove origin
git remote set-url origin git@gitee.com:用户名/仓库名.git
# 第一次向远程仓库推送的时候加上 -u 参数
git push -u origin "master"
已有仓库?
cd existing_git_repo
git remote add origin https://gitee.com/用户名/仓库名.git
git remote set-url origin git@gitee.com:用户名/仓库名.git
git push -u origin "master"
4 备注
4.1 git push报错
git pull -f <remote> <branch> # 用远程仓库强制覆盖本地仓库
git push -f <remote> <branch> # 用本地仓库强制覆盖远程仓库
4.2 git pull不覆盖本地修改
git pull命令
git修改代码后,如何再git pull而不覆盖修改的代码
git stash save "注释" # 保存本地修改
git pull origin master # 拉取远端仓库更新
git stash pop # 恢复本地修改
git stash list
git stash show