Files
notes_estom/Git/4 远程操作.md
2023-12-27 23:29:12 +08:00

122 lines
3.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 4 Git的远程操作
远程操作主要是指在不同的仓库之间进行提交和代码更改。是一个明显的对等的分布式系统。其中本地个仓库与远程仓库不同的远程仓库之间都可以建立这种关系。这种关系之间的操作主要有pull和push。
### **远程仓库**
创建SSH key远程仓库和本地仓库一般是通过ssh通信的需要ssh通信的加密钥匙。
id_rsa是私钥id_rsa.pub是公钥可以告诉别人。
```
ssh-keygen -t rsa -C 'yinkanglong@163.com'
```
登录github添加公钥内容。建立本地与远程仓库的通信协议
### **添加远程仓库**
github的界面操作可以很轻松的创建一个远程仓库。但如果想要直接上传自己本地的完整git库必须创建一个没有lisence和readme的空库。
```
git remote add origin git@github.com:michaelliao/learngit.git
```
将本地仓库和远程仓库进行关联。一般都叫远程关联仓库为origin本地的主干分支一般名为master
```
git push -u origin master
```
将本地的主干分支master推送到远程的origin分支上。-u参数实现了两个分支的关联将远程的git仓库的master和本地的master进行了分支管理当再次执行时只需要使用git push origin master命令。
### **从远程仓库克隆**
```
git clone git@github.com:michaelliao/gitskills.git
```
### **git remote**
```
git remote
```
列出你和远程仓库之间的远程连接
```
git remote -v
```
列出每个连接的名字和url
```
git remote add <name> <url>
```
创建一个新的远程连接并添加名字
```
git remote rm <name>
```
移除远程仓库的链接
### **git fetch**
```
git fetch <remote>
```
拉取仓库中的所有分支(包括相关的文件和所有的提交)
```
git fetch <remote> <branch>
```
拉取制定仓库中的所有分支(包括相关的文件和所欲的提交)
> 注意,这个步骤知识拉取远程的分支,在本地并没有合并也没有生成本地分支,知识一个可读的远程分支。
> * 使用git branch -r 命令可以查看所有只读的远程分支。
> * 使用gitcheckout命令可以创建本地分支并与远程分支关联。
> * 使用git merge命令可以将远程分支与本地分支合并。
### **git pull**
```
git pull remote
```
拉取当前分支对应的远程副本并将远程副本的更改写入本地副本。相当于git fetch之后git merge。
```
git pull -rebase \<remote\>
```
使用git rebase命令合并远程分支与本地分支不使用git merge
### **git push**
```
git push <remote> \<branch\>
```
将制定分支推送到远程分支。包括所有的文件和提交。
```
git push <remote> --force
```
强制推送
```
git push <remote> --all
```
本地所有的分支推送到远程仓库当中
```
git push <remote> --tags
```
将本地所有标签推送到远程仓库中
### 使用远程分支覆盖本地分支
git fetch --all
git reset --hard origin/dev (这里dev要修改为对应的分支名将本地分支只想远程分支的地提交)
git pull origin dev