Git - 远程仓库管理

一、建立本地仓库

$ mkdir git-test
$ cd git-test/
$ git init
Initialized empty Git repository in E:/document/git-test/.git/

二、提交文件到本地仓库

# create a new file
$ echo "[0] This is the file to commit" > repository-remote-interaction.md
# view the status
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        repository-remote-interaction.md

nothing added to commit but untracked files present (use "git add" to track)

# add to the stage
$ git add repository-remote-interaction.md
warning: LF will be replaced by CRLF in repository-remote-interaction.md.
The file will have its original line endings in your working directory

# view the status
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   repository-remote-interaction.md

# commit to the local repository
$ git commit -m "repository-remote-interaction"
[master (root-commit) a6a6688] repository-remote-interaction
 1 file changed, 1 insertion(+)
 create mode 100644 repository-remote-interaction.md

# view the status
$ git status
On branch master
nothing to commit, working tree clean

三、添加远程仓库

  • git remote add <remote-repository-name> <remote-repository-address> 添加一个远程仓库,其中
    • <remote-repository-name> : 为远程仓库起的一个名字,方便以后使用,可以随意
    • <remote-repository-address> : 远程仓库的地址,可以在 Gitee(国内),或者 Github 中创建远程仓库,复制创建仓库的地址即可
  • 可以多次使用该命令添加多个远程仓库地址
  • 本例使用的仓库地址 : red1y-git-test
  • git remote show <remote-repository-name> 查看远程仓库的信息
# add a remote repository
$ git remote add remote-test https://github.com/red1y/red1y-git-test

# view added remote repository
$ git remote
remote-test

# view added remote repository and its address
$ git remote -v
remote-test     https://github.com/red1y/red1y-git-test (fetch)
remote-test     https://github.com/red1y/red1y-git-test (push)

# view remote repository status
$ git remote show remote-test
* remote remote-test
  Fetch URL: https://github.com/red1y/red1y-git-test
  Push  URL: https://github.com/red1y/red1y-git-test
  HEAD branch: main # current branch
  Remote branch: # show remote branch info
    main new (next fetch will store in remotes/remote-test)

四、repositoryremote 的交互

1、repository → \rightarrow remote : git push <remote-repository-name> <local-branch>:<remote-branch> 提交本地仓库到远程仓库,其中

  • <remote-repository-name> : 提交到哪一个远程仓库
  • <local-branch> : 提交本地仓库的哪一个分支
  • <remote-branch> : 提交到远程仓库的哪一个分支(缺省和本地分支相同)
# first push, omit <remote-branch>
$ git push remote-test master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 135.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/red1y/red1y-git-test/pull/new/master
remote:
To https://github.com/red1y/red1y-git-test
 * [new branch]      master -> master # create a new branch <master>

# second push, specify <remote-branch>
$ git push remote-test master:branch-1
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'branch-1' on GitHub by visiting:
remote:      https://github.com/red1y/red1y-git-test/pull/new/branch-1
remote:
To https://github.com/red1y/red1y-git-test
 * [new branch]      master -> branch-1 # create a new branch <branch-1>

# view remote repository status
$ git remote show remote-test
* remote remote-test
  Fetch URL: https://github.com/red1y/red1y-git-test
  Push  URL: https://github.com/red1y/red1y-git-test
  HEAD branch: main
  Remote branches: # two branches are added
    branch-1 tracked
    main     new (next fetch will store in remotes/remote-test)
    master   tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)

2、repository ⇐ \Leftarrow remote : git fetch <remote-repository-name> <remote-branch>:<local-branch> 获取远程仓库的分支到本地分支(更多操作)

Guess you like

Origin blog.csdn.net/weixin_39578432/article/details/117964632