| by Arround The Web | No comments

Git Remote: Advanced Techniques for Remote Repository Management

Git is a popular and most utilized tool for developers to manage coding projects. It permits the user to work in a secure and collaborative environment. Furthermore, all contributions in Git are done with a remote connection to project repositories from sources like GitHub and GitLab.

The aim of this guide is to provide advanced techniques for remote repository management.

The outline for the post is:

How to Add Remote Git?

As mentioned earlier, the user can work on the hosted projects on GitHub and GitLab by adding a remote connection. Let’s add a remote connection by following the below-stated steps.

Step 1: Make a Project Directory

Initially, create the new project directory on your Git bash by executing the “mkdir” command:

mkdir remote-project

Step 2: Move to Project Directory

Once the new directory is created, move into it using the “cd” command:

cd remote-project

Step 3: Initialize Repository

Afterward, initialise the Git repository by running provided command:

git init

The directory “remote-project” has been initialized.

Step 4: Add Remote Connection

To add the remote connection, copy the HTTPS or SSH URL from the desired Git repository and use the following syntax:

git remote add <origin> <Link>

Here, “origin” is the remote connection name.

In our case, we are adding the following Git remote connection:

git remote add origin https://github.com/Mateen900/perk

The remote connection for the above-given repository has been added.

Step 5: Verify Remote Connection

To verify whether the remote connection has been added or not, list available remote connections with the below-stated command:

git remote -v

From the above output, it is clear that our remote connection has been built successfully.

How to Manage Remote Connection in Git?

Suppose, you want to manage your Git remote connection. Let’s say, you want to update the remote connection with the latest update done locally. For that purpose, you can simply update and push it to the remote host. Let’s check out the below-mentioned steps.

Step 1: Create New File

For instance, we are adding the new file in our current directory with some random text using the echo command:

echo "this is the remote connection file" > remote.txt

The file “remote.txt” has been created.

Step 2: Track Changes

Track the added file in the repository by executing the “git add” command as provided below:

git add .

Step 3: Commit Changes

Commit the changes in the current repository using the “git commit” command:

git commit -m "remote file"

Step 4: Pull Project

Now, let’s pull the project from the remote repository. In our case, we are pulling the master branch of our remote repository:

git pull https://github.com/Mateen900/perk --allow-unrelated-histories master

The project has been pulled.

Step 5: List content

To verify that the remote repository is pulled or not, run the “ls” command:

ls

The remote repository has two folders named “demo1” and “project”.

Step 6: Push and Update Project

If you want to push and update the whole project to your remote repository, use the “git push” command, specify the remote link and give branch name in which you want to update:

git push https://github.com/Mateen900/perk master

The updated directory content has been pushed to the “master” branch of the repository.

Step 7: Verification

To verify whether the remote repository has been updated or not, go to GitHub and open the repository:

As you can see our repository has been updated with an additional file “remote.txt”.

How to Set the Tracking Branch in Git Remotes?

The user can also set the tracking branch of the Git remotes, so that, you don’t need to specify the remote URL and branch name each time. To do this, walk through the mentioned instructions.

Step 1: Set Tracking Branch

To set the existing local branch as a tracking branch in Git, you can use the “-u” flag along with the remote URL and branch name:

git push -u https://github.com/Mateen900/perk master

Step 2: Create File

Now, create the new file with the “echo” command to write some content and redirection operator:

echo "new file" > new-file.txt

The file “new-file.txt” has been created.

Step 3: Add File

Add the created file to the staging area by executing the “git add”:

git add new-file.txt

Step 4: Commit Changes

Commit the applied changes through the provided command as follows:

git commit -m "new file added"

The changes for the new file have been committed.

Step 5: Push Project

Now, use the provided command to push the project without specifying the remote URL and branch:

git push

Conclusion

To add the remote connection in Git Bash, use the “git remote add” command and specify the HTTPS or SSH URL. To manage the remote repositories, pull it from GitHub, perform some tasks, and push the updated repository to the remote host. Similarly, you can also set the tracking branch and repository to push them automatically without specifying. This tutorial has lightened up the Git remote advance techniques for remote repository management.

Share Button

Source: linuxhint.com

Leave a Reply