Remote Repositories
Remote repositories are the key to collaboration in GIT. They are copies of your repository that are stored on an external server, such as GitHub, GitLab or Bitbucket. They allow multiple developers to work on the same project, share their changes and maintain a centralized backup of the code.
Why use remote repositories?
- Collaboration: They facilitate teamwork by allowing multiple developers to work on the same project simultaneously, merging their changes in a controlled manner.
- Backup: They act as a centralized backup of the project, protecting your code against accidental loss or failure on your local machine.
- Code Sharing: Allow you to share your code with other developers, either to collaborate on open source projects or to showcase your work.
- Deployment: Facilitate the deployment of your application on production or test servers.
Remote Repository Types:
- Origin: This is the default name assigned to the original remote repository from which you cloned your local repository.
- Others: You can add connections to other remote repositories, allowing you to collaborate with different teams or projects, or use different code hosting platforms.
Operations with Remote Repositories:
git remote add <name> <URL>: Adds a new remote connection with the specified name and URL of the remote repository.git remote -v: Lists all remote connections configured in your local repository.git remote remove <name>: Removes the specified remote connection.git remote rename <old-name> <new-name>: Renames the specified remote connection.git push <remote> <branch>: Push commits from your local branch to the specified remote repository.git pull <remote> <branch>: Flushes the changes from the specified branch to the remote repository and merges them into your current local branch.git fetch <remote>: Fetches changes from the remote repository without merging them, allowing you to review them before committing.
Workflow with Remote Repositories:
- Clone a remote repository:
git clone <URL-of-the-repository>** - Make changes and commits locally: Work in your local repository, make changes, add files to the Staging Area and create commits.
- Send your changes to the remote repository:
git push origin <branch-name> - Download changes from other developers:
git pull origin <branch-name>Download changes from other developers.
Importance of Remote Repositories:
- Collaboration: They allow multiple developers to work on the same project in an efficient and synchronized manner.
- Security: They provide a centralized backup of the code, protecting it against loss or damage.
- Code Share and Show Code: Facilitate code sharing and collaboration on open source projects.
- Deployment: Simplify the application deployment process by allowing production or test servers to be kept up to date with the latest code.
git remote – Manage Remote Repositories
The git remote command is your tool for establishing and managing connections between your local repository and remote repositories residing on external servers. It allows you to add, list, rename, delete and get information about these remote repositories, making it easy to collaborate and synchronize your work with other developers.
What does git remote do?
git remote(no arguments): Lists the names of all remote repositories configured in your local repository.git remote add <name> <URL>: Adds a new remote connection with the specified name and URL of the remote repository.git remote -v: Displays a detailed list of all remote connections, including their names, URLs and the associated branches for fetching and pushing.git remote remove <name>: Removes the specified remote connection.git remote rename <old-name> <new-name>: Renames the specified remote connection.git remote show <name>: Displays detailed information about a specified remote connection, including branches, fetching and pushing URLs, and other configuration details.
When to use git remote?
- Clone a repository: When you clone a remote repository, GIT automatically creates a remote connection named origin that points to the original repository.
- Add a new remote repository: When you want to collaborate with other developers or use a different remote repository for backups or deployment.
- List remote repositories: To see a list of all remote repositories configured in your project.
- Delete or rename remote repositories: When you no longer need a remote connection or want to rename it.
- Get information about a remote repository: To view the configuration details of a specific remote connection.
Examples:
git fetch – Download changes from a remote repository without merging them
The git fetch command allows you to keep your local repository up to date with changes made in a remote repository, but without automatically merging them into your current branch. This gives you the flexibility to review changes before integrating them, which is especially useful in collaborative environments where you want to make sure that external changes don’t cause conflicts or problems in your local work.
What does git fetch do?
- Downloads all new commits, branches and tags from the specified remote repository.
- Updates your local remote branches (e.g.
origin/main) to reflect the current state of the remote repository. - It does not modify your current local branch or the Working Tree. The downloaded changes are kept in a separate area until you decide to merge them manually.
When to use git fetch?
- Before merging: To see what changes have occurred in the remote repository before merging them into your local branch, allowing you to assess if there are potential conflicts or if you need to update your work before merging.
- Collaboration: To keep you aware of changes made by other developers without affecting your ongoing work.
- Flexibility: To have more control over when and how to integrate remote changes into your local branch.
How to use git fetch:
- Make sure you have a remote connection set up: If you haven’t already, use
git remote add origin <URL-of-repository>to add a remote connection named origin that points to the remote repository you want to download changes from. - Run the command:
git fetch: Download all changes from all branches of the remote repository.git fetch <remote>: Fetches changes from the specified remote repository.git fetch <remote> <branch>: Fetches changes from a specified branch of the remote repository.
Example:
After using git fetch:
- You can use
git log main/<branch>to see the downloaded commits from the remote branch. - You can use
git diff <local-branch> origin/<branch>to compare your local branch with the updated remote branch. - You can use
git merge origin/<branch>to merge the changes from the remote branch into your local branch.
git pull vs git fetch
Although both commands are used to interact with remote repositories, git pull and git fetch have key differences in how they work and when it is appropriate to use each.
git fetch
- Action: Pulls changes from the remote repository (commits, branches, tags) but does NOT automatically merge them into your current local branch.
- Result: Updates your local remote branches (e.g., origin/main) to reflect the current state of the remote repository.
- Use:
- To see what changes have occurred in the remote repository before integrating them.
- To keep up with the work of others without affecting your current local branch.
- When you want to have more control over when and how to integrate remote changes.
git pull
- Action: This is a combination of two commands:
git fetch: Download changes from the remote repository.git merge: Automatically merges the downloaded changes into your current local branch.
- Result: Updates your current local branch with the changes from the remote repository.
- Use:
- To quickly update your local branch with the latest changes from the remote repository.
- When you are confident that remote changes will not cause conflicts with your local work.
- In workflows where change integration is frequent and expected to be seamless.
Comparison Table:
| Feature | git fetch | git pull |
|---|---|---|
| Download changes from remote repository | Yes | Yes (as a first step) |
| Merge changes to your current local branch | No | Yes (as a second step) |
| Update local remote branches | Yes | Yes |
| Modify your Working Tree | No | Yes (if there are no conflicts) |
| May generate merge conflicts | No | Yes |
| Requires manual conflict resolution | No | Yes (if there are conflicts) |
| Level of control over the integration of changes | High | Low |
When to use each?
git fetch:- When you want to see remote changes before committing them.
- When you are working on a branch with important changes that you don’t want to accidentally mix with remote changes.
- When you want to have more precise control over the merge process.
git pull:- When you want to quickly update your local branch with the latest remote changes.
- When you trust that remote changes will not cause conflicts.
- In workflows where change integration is frequent and expected to be seamless.
Pull Requests
Pull Requests (or pull requests) are an essential functionality in code hosting platforms such as GitHub, GitLab or Bitbucket that facilitate collaboration and code review in software projects. They represent a formal approach to integrate changes from one branch to another, usually from a development branch to the main branch (master or main).
How do Pull Requests work?
- Creating a branch: A developer creates a new branch from the main branch to work on new functionality, bug fixes or enhancements.
- Development and commits: The developer makes the necessary changes to his branch, adds the files to the Staging Area and creates commits.
- Sending the branch to the remote repository: The developer uses
git pushto send his branch to the remote repository. - Pull Request: The developer opens a Pull Request on the code hosting platform, specifying the branch he wants to merge and the target branch.
- Code review: Other developers review the proposed changes in the Pull Request, make comments, suggest improvements and discuss the code.
- Discussion and improvements: The author of the Pull Request and reviewers can discuss the changes, ask questions and request clarifications. The author can perform further commits on their branch to address comments and suggestions.
- Approval and merge: Once the reviewers are satisfied with the changes, they approve the Pull Request. The repository owner or a contributor with sufficient permissions can then merge the branch into the main branch.
- Close Pull Request: Once the branch has been merged, the Pull Request is automatically closed.
Advantages of Pull Requests:
- Code Review: They facilitate code review among teammates, which helps improve code quality, detect bugs, and ensure project consistency.
- Collaboration: Promote collaboration and communication between developers, allowing them to discuss changes, ask questions and share knowledge.
- Traceability: They keep a record of all discussions and decisions related to proposed changes, making it easier to understand the project’s history and make future decisions.
- Quality Control: Enable approval and quality control workflows to be established before integrating changes into the main branch, which helps maintain project stability.
Read the first chapter of this guide here or read the entire series here.





