Gitリモートオペレーションウェアハウス

Gitリモートオペレーションウェアハウス(Github)

導入する

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

[外部リンク画像が転送されています...(img-QB1rhKtW-1600961726834)]

工作区可以commit到本地仓库,本地仓库可以通过push上传到Github托管,而本地也能通过pull指令下载云端的仓库。常见的这4个操作如下:
  • git remote リモートウェアハウスオペレーション

  • git fetch コードベースをリモートで取得する

  • git pull リモートコードをダウンロードしてマージ

  • git push リモートコードをアップロードしてマージ

接続を確立する

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

1.GitHubアカウントにサインアップします

2.ローカルキーsshKeyを生成します

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

ここに写真の説明を挿入

3.GitHub側がsshKeyを確認します

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

ここに写真の説明を挿入

4. git bashにコマンドを入力して、接続を確認します

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

ここに写真の説明を挿入

ローカル倉庫を作成する

# 接下来肯定是你很熟悉的操作了,我们在本地新建一个仓库,叫做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

リモートライブラリを作成し、コミットをプッシュします

  • GitHubに空のウェアハウスを手動で作成し、名前を付けますtestRepository
  • 次にgit bash、次のコマンドを実行します。
# 添加一个新的远程仓库,指定一个别名,以便将来引用
$ 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											# 执行操作
  • 成功したプッシュ
    ここに写真の説明を挿入

一般的な操作

0.倉庫に接続します

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

1.現在のリモートライブラリを表示する

  • $ git remote:現在構成されているリモートウェアハウスを表示するには、次のコマンドを使用できます。
$ git remote
origin
$ git remote -v
origin  [email protected]:LinXiaoDe/testRepository.git (fetch)
origin  [email protected]:LinXiaoDe/testRepository.git (push)

実行時に-vパラメーターを追加します。各エイリアスの実際のリンクアドレスも確認できます。


2.リモートウェアハウスを抽出します

Gitには、リモートリポジトリから更新を抽出するための2つのコマンドがあります。

  • git fetch [alias]:リモートウェアハウスから[alias]新しいブランチとデータをダウンロードする
该命令执行完后需要执行 git merge 远程分支到你所在的分支
  • git merge [alias]/[branch]:リモートウェアハウスからデータを抽出し、現在のブランチにマージしてみてください。
该命令就是在执行 git fetch 之后紧接着执行 git merge 远程分支到你所在的任意分支
  • 例えば:
假设你配置好了一个远程仓库,并且你想要提取更新的数据,你可以首先执行
$ git fetch [alias]	 			# 告诉 Git 去获取它有你没有的数据,然后你可以执行 
$ git merge [alias]/[branch]	# 将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支
  • 例えば:
# 假设我们在 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.リモートウェアハウスにプッシュします

  • git push [alias] [branch]:新しいブランチとデータをリモートウェアハウスコマンドにプッシュします。

  • 例えば

$ git push origin master    # 推送到 Github

Githubウェアハウスに戻ると、ファイルが送信されたことがわかります。


4.リモートウェアハウスを削除します

  • git remote rm [alias]:リモートウェアハウスを削除するには、次のコマンドを使用できます。

  • 例えば

$ 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)

おすすめ

転載: blog.csdn.net/weixin_44307065/article/details/108785930