git的学习笔记(二):git远程操作

git的学习笔记(一):git本地操作

1.创建ssh key

ssh-keygen -t rsa -C "your_email@example.com"

执行命令后会在用户的家目录生成.ssh的隐藏文件夹,文件夹里有公钥id_rsa.pub和私钥id_rsa

2.登录github网站,在用户的settting页面,添加ssh key,把刚才生成的私钥内容粘贴到key文本中.

在gitlab或者gitee网站添加密钥的方式与github添加密钥的方式相同

3.远程仓库的管理

git remote add github <remote URL>	                            使用https方式将本地仓库关联到远程仓库(远程仓库名默认为origin)
git remote add github git@github.com:用户名/repo_name.git       使用git方式将本地仓库关联到远程仓库
git remote remove github                                        删除关联到本地的远程仓库,origin是远程库的名字
git branch --set-upstream-to=origin/smart smart                 将分支smart设置为跟踪来自origin的远程分支smart
git push                                                        把本地的所有提交推送到默认的远程的仓库
git push github -all
git pull github master                                          将远程origin代码拉到本地master分支
git push github master                                          将本地master分支代码推送到远端仓库
git clone <HTTP URLs/remote URL>                                复制远程项目到本地

4.本地文件与远程仓库文件不同,不相关时的合并

先切换到需要合并的分支,例如:本地master分支

git checkout master

合并本地分支和远程分支

# 把本地分支和远程仓库的master这两个不相关,独立的分支合并
git merge --allow-unrelated-histories github/master

在弹出的窗口中修改内容并退出,即为commit的内容

5.不同人修改了不同文件处理方式

不同的人修改了同一个项目的不同文件,且都已经提交到过程仓库时

首先用户A把远程仓库的分支拉取到本地仓库

git fetch github
git branch -av      # 查看本地仓库和远程仓库的所有分支信息

在这个时间段内,另一个用户B又在他的分支做修改并提交到远程仓库后,用户A使用 git push github 命令同步本地修改到远程仓库会提示异常

用户A的解决方法:

git fetch github                 # 拉取远程仓库的分支信息
git merge github/用户B的分支      # 把用户B的分支与本地分支进行合并
git push github                  # 推送到远程仓库

6.不同人修改了同一个文件的不同区域处理方式

用户A修改某个文件,提交并同步到远程仓库

用户B也修改这个文件,然后commit,在同步到远程仓库时会报错

用户B解决方法:

git fetch
git merge origin/用户A的远程分支        # 用户A修改后提交到哪个分支,就合并哪个分支
git push                               # 用户B同步分支到远程仓库的指定分支 

7.不同人修改了同一个文件的同一区域处理方式

用户A修改某个文件的某一行,提交并同步到远程仓库

用户B也修改这个文件的同一行,然后commit,在同步到远程仓库时会报错

用户B的解决方法:

git pull                # 把远程仓库的用户A的修改拉取到本地并合并,会提示 conflict 
修改冲突文件,解决冲突
git commit -m "commit message"      # 提交修改
git push github                     # 同步到远程仓库

8.多人协作,同时变更了文件名和文件内容时处理方式

用户A修改文件名,提交并同步到远程仓库

用户B也修改这个文件的内容,然后commit,在同步到远程仓库时会报错

用户B的解决方法:

git pull                # 把远程仓库的用户A的修改拉取到本地并合并,会提示修改合并后的提交信息
git push github                     # 同步到远程仓库

git会自动把用户B的文件名修改成用户A修改后的文件名

9.多人协作,把同一文件修改成两个不同的文件名时的处理方式

用户A修改文件名,提交并同步到远程仓库

用户B也修改这个文件的文件名,然后commit,在同步到远程仓库时会报错

用户B的解决方法:

git pull                # 把远程仓库的用户A的修改拉取到本地并合并,并同时显示两个内容相同但文件名不同的文件
git diff file1 file2
git status 
git rm file             # 这里的file为没有被修改之前的文件名
git add file1           # 把 file1 添加到暂存区
git rm file2            # 删除 file1
git commit -m "commit message"
git push

10.禁止向集成分支执行 push

多人协作开发时,禁止使用的命令,会造成以前提交的信息丢失

git push -f             # 强制 push

11.同一台电脑添加多个git密钥

参考gitee网站:Git配置多个SSH-Key

11.1 生成多个网站的公钥和私钥

ssh-keygen -t rsa -C 'xxxxx@qq.com' -f ~/.ssh/gitee_id_rsa
ssh-keygen -t rsa -C 'xxxxx@qq.com' -f ~/.ssh/github_id_rsa
ssh-keygen -t rsa -C 'xxxxx@qq.com' -f ~/.ssh/gitlab_id_rsa

11.2 在~/.ssh 目录下新建一个config文件

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

11.3 用ssh命令分别测试

Administrator@DESKTOP-B5TMUVT MINGW64 ~/.ssh
$ ssh -T git@gitee.com
Warning: Permanently added the ECDSA host key for IP address '120.55.226.24' tothe list of known hosts.
Hi SING890925! You've successfully authenticated, but GITEE.COM does not provide shell access.

Administrator@DESKTOP-B5TMUVT MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi renpingsheng! You've successfully authenticated, but GitHub does not provideshell access.

Administrator@DESKTOP-B5TMUVT MINGW64 ~/.ssh
$ ssh -T git@gitlab.com
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.
Welcome to GitLab, @renpingsheng!

如下图所示

12.打开浏览器查看git帮助文档

git help --web log      # 打开浏览器查看git log帮助文档
posted @ 2019-04-26 19:51  renpingsheng  阅读(673)  评论(0编辑  收藏  举报