Git remote operation warehouse

Git remote operation warehouse (Github)

Introduce

使用到的 Git 命令都在本地执行,如果你想通过 Git 分享你的代码或者与其他开发人员合作。 你就需要将数据放到一台其他开发人员能够连接的服务器上。当然这台服务器可以是一台联网的电脑,也可以是GitHub托管平台。嘿,通过Git操作GitHub上的仓库,想想还是刺激的,开始吧我们。

[External link image is being transferred...(img-QB1rhKtW-1600961726834)]

工作区可以commit到本地仓库,本地仓库可以通过push上传到Github托管,而本地也能通过pull指令下载云端的仓库。常见的这4个操作如下:
  • git remote Remote warehouse operations

  • git fetch Get the code base remotely

  • git pull Download remote code and merge

  • git push Upload remote code and merge

establish connection

在所有的工作开始之前,我们必须要建立起Github和本地的连接,分为4步:

1. Sign up for a GitHub account

2. Generate local key sshKey

# 执行如下指令
# 这条指令生成本地shhKey,一般放在~/.ssh目录下的id_rsa.pub文件
# 执行完毕之后去目录下找到它
$ ssh-keygen -t rsa -C "你的邮箱@qq.com"

Insert picture description here

3. GitHub side confirm sshKey

我想你肯定找到了sshKey,是时候去gitHub上确认sshKey了
进入目:/settings/keys
新建SSH keys

Insert picture description here

4. Enter the command in git bash to confirm the connection

# 执行如下指令连接到Github托管:
$ ssh -T [email protected]
# 下图展示连接成功

Insert picture description here

Create a local warehouse

# 接下来肯定是你很熟悉的操作了,我们在本地新建一个仓库,叫做testRepository,用于测试:
$ mkdir testRepository

$ cd testRepository/

$ git init

$ vim README.md

$ vim hello.cpp

$ ls
hello.cpp  README.md

$ git add .

$ git commit . -m 'commit 1: add README and hello.c'

$ git log --oneline
3b5640b (HEAD -> master) commit 1: add README and hello.c

Create remote library and push commit

  • Manually create an empty warehouse on GitHub and name ittestRepository
  • Then git bashexecute the following command:
# 添加一个新的远程仓库,指定一个别名,以便将来引用
$ git remote add [shortname] [url]
# push添加到仓库
$ git push -u origin master

# eg:
$ ssh -T [email protected]												# 连接GitHUb
$ git remote add origin [email protected]:LinXiaoDe/testRepository.git	# 连接某一个仓库
$ git push -u origin master											# 执行操作
  • Successful push
    Insert picture description here

Common operations

0. Connect to the warehouse

$ ssh -T [email protected]												# 连接GitHUb
$ git remote add origin [email protected]:LinXiaoDe/testRepository.git	# 连接某一个仓库

1. View the current remote library

  • $ git remote: To view which remote warehouses are currently configured, you can use the command:
$ git remote
origin
$ git remote -v
origin  [email protected]:LinXiaoDe/testRepository.git (fetch)
origin  [email protected]:LinXiaoDe/testRepository.git (push)

Add the -v parameter when executing, you can also see the actual link address of each alias.


2. Extract remote warehouse

Git has two commands to extract updates from remote repositories.

  • git fetch [alias]: [alias]Download new branches and data from remote warehouses
该命令执行完后需要执行 git merge 远程分支到你所在的分支
  • git merge [alias]/[branch]: Extract data from the remote warehouse and try to merge to the current branch:
该命令就是在执行 git fetch 之后紧接着执行 git merge 远程分支到你所在的任意分支
  • eg:
假设你配置好了一个远程仓库,并且你想要提取更新的数据,你可以首先执行
$ git fetch [alias]	 			# 告诉 Git 去获取它有你没有的数据,然后你可以执行 
$ git merge [alias]/[branch]	# 将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支
  • eg:
# 假设我们在 Github 在线修改:" README.md" 
# 然后我们在本地更新修改。
$ git fetch origin								# 获取你没有的数据

remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:tianqixin/runoob-git-test
0205aab..febd8ed  master     -> origin/master 	# 说明 master 分支已被更新,我们可以使用以下命令将更新同步到本地:

$ git merge origin/master						# 将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支

Updating 0205aab..febd8ed
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

3. Push to remote warehouse

  • git push [alias] [branch]: Push your new branch and data to a remote warehouse command:

  • eg

$ git push origin master    # 推送到 Github

Back to our Github warehouse, you can see that the file has been submitted:


4. Delete remote warehouse

  • git remote rm [alias]: To delete a remote warehouse you can use the command:

  • eg

$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

# 添加仓库 origin2
$ git remote add origin2 [email protected]:tianqixin/runoob-git-test.git

$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)
origin2    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin2    [email protected]:tianqixin/runoob-git-test.git (push)shell

# 删除仓库 origin2
$ git remote rm origin2
$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/108785930