YouTip LogoYouTip

Git Remote Repo

Git does not have a central server like SVN. So far, all the Git commands we’ve used are executed locally. If you want to share your code via Git or collaborate with other developers, you need to place your data on a server accessible to other developers. This tutorial uses GitHub as the remote repository. You may first read our (#). !(#) * * * ## Adding a Remote Repository To add a new remote repository, you can specify a simple name for future reference. The command syntax is as follows: git remote add This example uses GitHub as the remote repository. If you don’t yet have a GitHub account, you can register one at [https://github.com/](https://github.com/). Since data transfer between your local Git repository and the GitHub repository is encrypted via SSH, we need to configure authentication information: Use the following command to generate an SSH key: $ ssh-keygen -t rsa -C "youremail@example.com" Replace **your_email@youremail.com** with the email address you used to register on GitHub. You’ll then be prompted to confirm the file path and enter a passphrase; just press Enter to accept the defaults. If successful, an **.ssh** folder will be created under **~/**. Navigate into it, open **id_rsa.pub**, and copy its contents (the **key**). $ ssh-keygen -t rsa -C "12345678@qq.com"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 EnterEnter same passphrase again: # Press EnterYour 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 12345678@qq.comThe key's randomart image is: +-------+ |E*+.+=**oo | |%Oo+oo=o. . | |%**.o.o. | |OO. o o | |+o+ S | |. | | | | | | | +---------+ Go back to GitHub, navigate to Account => Settings. !(#) On the left sidebar, select **SSH and GPG keys**, then click the **New SSH key** button. Set a title (any descriptive name works), and paste the key generated on your computer. !(#) !(#) After successful addition, the interface appears as shown below: !(#) To verify success, run the following command: $ ssh -T git@github.comThe 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/)? yes # Type 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 above output confirms that we have successfully connected to GitHub. Next, after logging in, click β€œNew repository”, as shown in the figure below: !(#) Then fill in **-git-test** (the remote repository name) for Repository name, leave other settings as default, and click the β€œCreate repository” button to successfully create a new Git repository: !(#) After successful creation, the following information is displayed: !(#) This information tells us that we can clone a new repository from this URL, or push content from our local repository to the GitHub repository. Now, following GitHub’s instructions, run the following commands in your local repository: $ mkdir -git-test # Create test directory $ cd -git-test/ # Enter test directory $ echo "# Git Test" >> README.md # Create README.md and write content $ ls # List files in current directory README $ git init # Initialize $ git add README.md # Add file $ git commit -m "Add README.md Files" # Commit with message[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 git@github.com:tianqixin/-git-test.git $ git push -u origin master Please copy the commands from your own GitHub repository page where you successfully created the new repository β€” do not use the commands shown here, since our GitHub usernames and repository names differ. Next, return to your GitHub repository page, and you’ll see that the file has been uploaded to GitHub: !(#) * * * ## Viewing Current Remote Repositories To view the currently configured remote repositories, use the command: git remote ### Example $ git remote origin $ git remote -v origingit@github.com:tianqixin/-git-test.git (fetch) origingit@github.com:tianqixin/-git-test.git (push) Adding the `-v` flag displays the actual URLs associated with each remote alias. * * * ## Fetching from Remote Repositories Git provides two commands to fetch updates from remote repositories. 1. Download new branches and data from a remote repository: git fetch After running this command, you must execute `git merge` to merge the remote branch into your current branch. 2. Fetch data from a remote repository and attempt to merge it into your current branch: git merge This command effectively executes **git fetch**, followed immediately by **git merge** of the remote branch into your current branch. !(#) Assume you have configured a remote repository and wish to fetch updates. First run **git fetch ** to instruct Git to retrieve any data it has that you don’t. Then run **git merge /** to merge any updates from the server (e.g., if someone else pushed changes while you were working) into your current branch. Next, click β€œREADME.md” on GitHub and edit it online: !(#) Then update 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 0Unpacking objects: 100% (3/3), done.From github.com:tianqixin/-git-test 0205aab..febd8ed master -> origin/master The line β€œ0205aab..febd8ed master β†’ origin/master” above indicates the master branch has been updated. You can synchronize these changes to your local repository using: $ git merge origin/master Updating 0205aab..febd8ed Fast-forward README.md | 1 + 1 file changed, 1 insertion(+) View the contents of README.md: $ cat README.md # Git Test## First modification * * * ## Pushing to a Remote Repository To push your new branch and data to a remote repository, use the command: git push This command pushes your local `` to the `` branch on the `` remote repository. Example: $ touch -test.txt # Add file $ git add -test.txt $ git commit -m "Add to remote" master 69e702d] Add to remote 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 -test.txt $ git push origin master # Push to GitHub Return to your GitHub repository page, and you’ll see the file has been successfully committed: !(#) * * * ## Removing a Remote Repository To remove a remote repository, use the command: git remote rm ### Example $ git remote -v origingit@github.com:tianqixin/-git-test.git (fetch) origingit@github.com:tianqixin/-git-test.git (push)# Add repository origin2 $ git remote add origin2 git@github.com:tianqixin/-git-test.git $ git remote -v origingit@github.com:tianqixin/-git-test.git (fetch) origingit@github.com:tianqixin/-git-test.git (push) origin2git@github.com:tianqixin
← Linux Shell ArrayDocker Compose β†’