Git Remote Repository (Github)
Git does not have a central server like SVN.
Currently, the Git commands we are using are executed locally. If you want to share your code or collaborate with other developers through Git, you need to place your data on a server that other developers can connect to.
This example uses Github as the remote repository. You can read our Github Concise Tutorial first.
Adding a Remote Repository
To add a new remote repository, you can specify a simple name for future reference. The command format is as follows:
git remote add [shortname] [url]
This example uses Github as the remote repository. If you don't have a Github account, you can register at https://github.com/.
Since the transmission between your local Git repository and the GitHub repository is encrypted via SSH, we need to configure the authentication information:
Use the following command to generate an SSH Key:
$ ssh-keygen -t rsa -C "[email protected]"
Replace [email protected] with the email you registered on Github. You will be asked to confirm the path and enter a password. We will use the default settings and press Enter all the way.
Successfully, a .ssh folder will be generated in ~/, go inside, open id_rsa.pub, and copy the key inside.
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): # Press Enter
Enter same passphrase again: # Press Enter
Your identification has been saved in /Users/tianqixin/.ssh/id_rsa.
Your public key has been saved in /Users/tianqixin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|E*+.+=**oo |
|%Oo+oo=o. . |
|%**.o.o. |
|OO. o o |
|+o+ S |
|. |
| |
| |
| |
+----[SHA256]-----+
Go back to Github, enter Account => Settings (Account Configuration).
On the left, select SSH and GPG keys, then click the New SSH key button. Set the title (you can fill it in randomly) and paste the key generated on your computer.
After adding successfully, the interface will look like this:
To verify if it was successful, enter the following command:
$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # Enter yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # Success message
The following command indicates that we have successfully connected to Github.
After logging in, click "New repository" as shown below:
Then fill in the Repository name with tutorialpro-git-test (remote repository name), keep the other default settings, and click the "Create repository" button to successfully create a new Git repository:
After successful creation, the following information will be displayed:
This information tells us that we can clone a new repository from this repository or push the content of the local repository to the GitHub repository.
Now, according to the GitHub prompts, run the commands under the local repository:
$ mkdir tutorialpro-git-test # Create a test directory
$ cd tutorialpro-git-test/ # Enter the test directory
$ echo "# tutorialpro.org Git Test" >> README.md # Create a README.md file and write content to it
$ ls # View the files in the directory
README
$ git init # Initialize
$ git add README.md # Add file
$ git commit -m "Add README.md file" # Commit and add a comment
[master (root-commit) 0205aab] Add README.md file
1 file changed, 1 insertion(+)
create mode 100644 README.md
# Push to Github
$ git remote add origin [email protected]:tianqixin/tutorialpro-git-test.git
$ git push -u origin master
Please copy the following commands based on where you successfully created a new repository on Github, not based on the commands I provided, as our Github usernames and repository names are different.
Next, return to the repository created on Github, and you can see that the file has been uploaded to Github:
Viewing the Current Remote Repository
To view the currently configured remote repositories, you can use the command:
git remote
Example
$ git remote
origin
$ git remote -v
origin [email protected]:tianqixin/tutorialpro-git-test.git (fetch)
origin [email protected]:tianqixin/tutorialpro-git-test.git (push)
By adding the -v parameter, you can also see the actual link addresses for each alias.
Fetching from the Remote Repository
Git has two commands to fetch updates from the remote repository.
- Download new branches and data from the remote repository:
git fetch
This command needs to be followed by git merge
to merge the remote branch into your current branch.
- Fetch data from the remote repository and attempt to merge it into the current branch:
git merge
This command is executed after git fetch
followed by git merge
to merge the remote branch into your current branch.
Suppose you have configured a remote repository and want to fetch updated data. You can first execute git fetch [alias]
to tell Git to get the data it has that you don't, and then execute git merge [alias]/[branch]
to merge any updates from the server (assuming someone has pushed to the server) into your current branch.
Next, we will click "README.md" on Github and modify it online:
Then we update the changes locally.
$ 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/tutorialpro-git-test
0205aab..febd8ed master -> origin/master
The above information "0205aab..febd8ed master -> origin/master" indicates that the master branch has been updated. We can use the following command to synchronize the updates to the local repository:
$ git merge origin/master
Updating 0205aab..febd8ed
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
View the contents of the README.md file:
$ cat README.md
# tutorialpro.org Git Test
## First Modification
Pushing to the Remote Repository
To push your new branch and data to a remote repository, use the command:
git push [alias] [branch]
The above command pushes your [branch] branch to the [branch] branch of the [alias] remote repository. Here is an example:
$ touch tutorialpro-test.txt # Add file
$ git add tutorialpro-test.txt
$ git commit -m "Push to remote"
master 69e702d] Push to remote
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 tutorialpro-test.txt
$ git push origin master # Push to Github
Go back to our Github repository, and you can see that the file has been committed:
Deleting the Remote Repository
You can delete the remote repository using the command:
git remote rm [alias]
Example
$ git remote -v
origin [email protected]:tianqixin/tutorialpro-git-test.git (fetch)
origin [email protected]:tianqixin/tutorialpro-git-test.git (push)
# Add repository origin2
$ git remote add origin2 [email protected]:tianqixin/tutorialpro-git-test.git
$ git remote -v
origin [email protected]:tianqixin/tutorialpro-git-test.git (fetch)
origin [email protected]:tianqixin/tutorialpro-git-test.git (push)
origin2 [email protected]:tianqixin/tutorialpro-git-test.git (fetch)
origin2 [email protected]:tianqixin/tutorialpro-git-test.git (push)
origin2 [email protected]:tianqixin/tutorialpro-git-test.git (fetch) origin2 [email protected]:tianqixin/tutorialpro-git-test.git (push)
Delete repository origin2
$ git remote rm origin2 $ git remote -v origin [email protected]:tianqixin/tutorialpro-git-test.git (fetch) origin [email protected]:tianqixin/tutorialpro-git-test.git (push)