原创不易,转发请表明出处
最近在项目中使用git,遇到了一些坑,感觉需要总结下提高效率了
入门篇
- 本地的公钥拷贝到git仓库中
这样就可以不用账户密码登陆了,首先在本地生成公钥私钥对,执行以下语句:
ssh-keygen -t rsa -C 'wusi'
注意 wusi可以改成你自己的名字
cd ~/.ssh
拷贝 id_rsa.pub的内容
在github -> 个人 -> settings -> SSH and GPG keys -> new SSH key title随意,key拷贝过去 - git本地仓库初始化
mkdir git-learning
cd git-learning
git init
touch readme
添加文件到版本控制
git add readme
- 查看代码变动情况
git status
- 提交代码到本地仓库
git commit -m 'add readme'
提示信息:
[master (root-commit) e0811b5] add readme1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme
- 将本地仓库推送到远程仓库
首先在github上建立仓库,我建立的仓库名称为 git-learning
// 远程添加仓库
git remote add origin git@github.com:wusi05/git-learning.git
// 推送代码
git push -u origin master
提示信息
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXWGl7E1IGOCspRomTxdCARKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 201 bytes | 201.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:wusi05/git-learning.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
查看github代码推送成功了
- 将本地分支和远程分支建立链接
git branch --set-upstream-to=origin/<branch> local-branch
提高篇
推送本地仓库到远程
- git init
- git add .
- git commit -m 'first commit'
- git remote add origin 你的远程库地址
- git pull --rebase origin master
- git push -u origin master
- git status