Git Remote: Advanced Techniques for Remote Repository Management
The aim of this guide is to provide advanced techniques for remote repository management.
The outline for the post is:
- How to Add Remote in Git?
- How to Manage Remote Connection in Git?
- How to Set the Tracking Branch in Git Remotes?
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:
Step 2: Move to Project Directory
Once the new directory is created, move into it using the “cd” command:
Step 3: Initialize Repository
Afterward, initialise the Git repository by running provided command:
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:
Here, “origin” is the remote connection name.
In our case, we are adding the following Git remote connection:
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:
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:
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:
Step 3: Commit Changes
Commit the changes in the current repository using the “git commit” command:
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:
The project has been pulled.
Step 5: List content
To verify that the remote repository is pulled or not, run the “ls” command:
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:
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:
Step 2: Create File
Now, create the new file with the “echo” command to write some content and redirection operator:
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”:
Step 4: Commit Changes
Commit the applied changes through the provided command as follows:
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:
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.
Source: linuxhint.com