How to connect Git remote and local repository (GCP CSR)

Git Remote Repository

We describe how to connect Git remote and local repository.
In this knowledge, we will use Cloud Source Repositories of GCP as the Git remote repository. However, most of the steps are also available in other remote repositories such as GitHub.

This procedure is based on the assumption that an empty private remote repository has already been created on Cloud Source Repositories, GitHub, etc.
In addition, this procedure is based on the details of the structure and mechanism of Git such as repositories.
The structure and mechanism are summarized in the knowledge below.

 

Sponsored Links

SSH settings for remote repository connection

An ssh connection is required to connect to a private remote repository. For that, set the following in the local environment.

  • Place the SSH private key (id_rsa) for remote repository connection in any directory.
    • In the example below, it is placed under “~ / xxxxxx /”.
~/xxxxxx/id_rsa

 

  • If the authority of the private key is not correct, ssh connection is not possible. Change permissions.
$ sudo chmod 600 ~/xxxxxx/id_rsa

 

  • Set ssh with a text editor such as emacs or vi.
    • Set the following in the “~/.ssh/config” file. If the file does not exist, create it.
    • Set any connection name in “Host”. In the example below, it is set as “host01”.
    • Set the FQDN and IP of the connection destination with “Hostname”. In the example below, “source.developers.google.com”, which is the connection destination of GCP’s Cloud Source Repositories, is set.
    • Set the location of the SSH private key used in “Identity File”. Set “~/xxxxxx/id_rsa” where the key is placed in the above example.
    • Set the port for SSH connection to the remote repository in “port”. In the example below, “2022” is set as the connection destination port of Cloud Source Repositories of GCP.
$ vi ~/.ssh/config

Host host01
  Hostname source.developers.google.com
  IdentityFile ~/xxxxxx/id_rsa
  port 2022

 

Clone remote repository to local repository

  • If git itself is not installed in the local environment in the first place, install it.
    • The following example is a command using apt on Ubuntu etc. For CentOS etc., use yum.
$ sudo apt-get install git

 

  • Move to the directory where you want to place the worktree, the directory where you put the files you want to manage with git, .
    • In the example below, move to “git_repo”.
$ cd ~/git_repo

 

  • Clone the remote repository to the local repository with the “git clone” command.
    • Execute “git clone” command using ssh connection.
    • [User] is the user of the ssh connection. In the example below it is “user01”.
    • [Host of SSH setting] is the Host set in the above ssh setting. In the example below, “host01”.
    • [Absolute path of repository] can be confirmed by the command or connection information for cloning using SSH from the repository page on the Web portal of GitHub and Cloud Source Repositories. In the example below, “/p/nagarelab-pj01/r/pj01_repo01”.
    • After executing the command, enter the [Passphrase] for the SSH connection.
$ git clone ssh://[User]@[Host of SSH setting][Absolute path of repository]
$ git clone ssh://user01@host01/p/nagarelab-pj01/r/pj01_repo01

Cloning into 'pj01_repo01'...
Enter passphrase for key '/home/xxx/xxxxxx/id_rsa': [Passphrase]
warning: You appear to have cloned an empty repository.

 

We have described “git clone” using SSH settings so far. However, if it is possible to place the SSH private key in the default location below, you can also “git clone” with just a command without SSH settings. The procedure is shown below.

~/.ssh/id_rsa

 

  • Clone the remote repository to the local repository with the “git clone” command.
    • Execute “git clone” command using ssh connection.
    • [User] is the user of the ssh connection. In the example below it is “user01”.
    • [FQDN] is the URL or IP address of the connection repository. In the example below, “source.developers.google.com”.
    • [port] is the port number to connect to. In the example below it is “2022”.
    • [Absolute path of repository] can be confirmed by the command or connection information for cloning using SSH from the repository page on the Web portal of GitHub and Cloud Source Repositories. In the example below, “/p/nagarelab-pj01/r/pj01_repo01”.
    • After executing the command, enter the [Passphrase] for the SSH connection.
$ git clone ssh://[User]@[FQDN]:[port][Absolute path of repository]
$ git clone ssh://user01@source.developers.google.com:2022/p/pj01/r/pj01_repo01

Cloning into 'pj01_repo01'...
Enter passphrase for key '/home/xxx/.ssh/id_rsa': [Passphrase]
warning: You appear to have cloned an empty repository.

 

Register changes from worktree to index

  • Since the procedure from here is performed under the worktree of the repository that was duplicated earlier, move the directory.
    • If you do not specify the directory name of the worktree, the directory name will be the same as the repository name.
    • In this example, we cloned the “pj01_repo01” repository. Therefore, move to the directory “pj01_repo01”.
$ cd pj01_repo01

 

  • Create a new file in the worktree for testing updates.
$ touch test.txt

 

  • Register changes from “worktree” to “Index”.
    • An “index” is a place to temporarily store updates before committing to a local repository.
    • In the example below, specify “*” to target all changes under the worktree. If you want to update only a part, specify the update target.
$ git add *

 

  • Check the current status of files managed by git. For example, it is updated only in the “work tree”, registered in the “index”, and even committed to the repository.
    • In the example below, you can see that “test.txt” is registered in the index but not in the repository.
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   test.txt

 

Commit from index to local repository

  • Commit and update the contents of the index to the local repository with the “git commit” command.
    • Specify the message at the time of commit with the “-m” option, and leave the message so that you can grasp the commit contents later. In the example below, “first commit” is specified.
$ git commit -m "first commit"
[master (root-commit) a5559fa] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt

 

  • Check the current status of files managed by git.
    • In the example below, you can see that there are no uncommitted files in the worktree and they are all committed.
$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean

 

Upload from local repository to remote repository

  • Set the user to be used when committing the current repository with the following command.
    • As mentioned above, we are currently operating on the “pj01_repo01” repository, so the target of this setting is the “pj01_repo01” repository.
    • In the example below, the user name is set to “user-a” and the email address is set to “user-a@xxxx.nagarelab.com”.
    • If this “local” setting is not set, the “system” setting that targets the entire system repository on the local environment or the “global” l setting that targets all the repositories of the current user takes precedence. These settings can be set with commands such as “git config –global”.
$ git config --local user.name "user-a"
$ git config --local user.email "user-a@xxxx.nagarelab.com"

 

  • Check if the set contents are reflected.
    • The substance of the “local” setting that applies only to specific repositories is described in “./.git/config” in the work tree.
    • From the result of the following command, it can be confirmed that “[user]” is set properly.
$ cat ./.git/config

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = ssh://user01@source.developers.google.com:2022/p/pj01/r/pj01_repo01
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[user]
    name = user-a
    email = user-a@xxxx.nagarelab.com

 

  • Upload the contents from the local repository to the remote repository with “git push”.
    • Set the push destination remote branch to the upstream branch with the “-u” option. This branch will be the link destination when using “git push” or “git pull” in the future.
    • origin represents the default repository name. In this example, it points to the repository on Cloud Source Repositories set by the above “git clone” command.
    • master represents the branch name, and in this example it is the master branch created by default in the repository. master represents the branch name of the same name both locally and remotely. If you want to use different names for your local and remote branches, you need to write something like <LOCALBRANCHNAME>: <REMOTEBRANCHNAME> instead of a single branch name.
$ git push -u origin master

Warning: Permanently added the ECDSA host key for IP address '[XX.XXX.XXX.XX]:2022' to the list of known hosts.
Enter passphrase for key '/home/xxx/xxxxxx/id_rsa':
Counting objects: 3, done.
Writing objects: 100% (3/3), 205 bytes | 205.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://user01@source.developers.google.com:2022/p/pj01/r/pj01_repo01
* [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

 

If we check the remote repository side at this point, we can see that the update information has been updated. The updater is the user set earlier.
After that, after updating on the worktree, we can continuously update the remote repository by repeating from “Register changes from worktree to index” to this point.

 

Download and update update information from the remote repository to the local repository

So far, we have described how to clone a remote repository to create one local repository and update both the local repository and the remote repository.
However, when using Git, it is expected that multiple people will develop it. Therefore, it may be necessary to update the contents of a remote repository updated by another person to a local repository in a different environment.
Therefore, we also describe how to download and update update information from the remote repository to the local repository.

The following procedure is the operation after the remote repository is updated from a different local repository.

  • Move to the worktree directory.
    • In the example below, it’s under the “git_repo” directory.
$ cd ~/git_repo/pj01_repo01

 

  • Switch your local branch to the master branch.
    • This command is not necessary unless you have switched to another branch.
    • If you make a change in the branch before switching and switch to another branch while there is a difference in the index or work tree, the difference will move to the branch to switch to. Therefore, you need to commit your changes or save them with “git stash” as needed.
$ git checkout master

 

  • Download and update update information from the remote repository to the local repository with “git pull”.
    • “origin master” represents the remote branch to pull to.
      • “Origin” represents the default repository name. In this example, it points to the repository on Cloud Source Repositories set by the above “git clone” command.
      • “master” represents the branch name. In this example, it points to the “master” branch created by default in the repository.
$ git pull origin master

 

There is no problem using the above command. However, since it is a composite command of the following two commands, it is also possible to execute the following in order instead of the above command.

  • Download update information from the branch of the remote repository to the remote tracking branch of the local repository with “git fetch”.
    • “origin master” below represents the remote branch to fetch to.
      • “origin” represents the default repository name. In this example, it points to the repository on Cloud Source Repositories set by the above “git clone” command.
      • “master” represents the branch name. In this example, it points to the master branch created by default in the repository.
      • This command downloads the update information from the above branch to the remote tracking branch “origin / master” in the local repository. At this point, the master branch and work tree in the local repository have not yet been updated.
$ git fetch origin master

 

  • Switch the local branch to the “master” branch.
    • This command is not necessary unless you have switched to another branch.
$ git checkout master

 

  • Merge the remote tracking branch into the current branch with “git merge”.
    • “origin/master” represents the remote tracking branch to merge.
    • This command updates the master branch and worktree of the local repository.
$ git merge origin/master
Sponsored Links