LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Git & GitHub

Git Remote Operations – Push, Pull and Fetch

When you work alone, your code can stay on your computer. But when you work with a team, you need to share your commits and receive changes made by other developers. A remote repository, such as GitHub, provides a shared place where the team can exchange code.

GitHubBeginner to IntermediatePractical Guide

Imagine you are developing a banking application on your laptop. Your teammate has updated the customer validation logic and pushed the changes to the team's GitHub repository. Meanwhile, you have completed a new feature locally.

How do you download your teammate's latest changes? How do you share your own work? And how can you check what has changed remotely without immediately modifying your local branch?

This is where Git remote operations become important. Git provides three commands that developers use regularly: push, pull and fetch.

TL;DR — Quick Summary
  • git push: Sends your local commits to a remote repository.
  • git fetch: Downloads remote updates without integrating them into your current branch.
  • git pull: Fetches remote updates and integrates them into your current branch.
  • Remote-tracking branches help you understand the state of branches on the remote repository.

1. What Is a Git Remote?

A Git remote is a reference to another repository. It usually points to a repository hosted on a platform such as GitHub, GitLab or Bitbucket.

Your local repository stores your project files, commits and branches. A remote repository provides a shared location where team members can exchange their commits.

For example, a team may use a GitHub repository named banking-loan-service. Each developer clones this repository and works on their own local copy.

Local Repository and Remote Repository
Developer Laptop Local Repository Commits and branches GitHub Remote Repository Shared commits and branches Push Fetch / Pull Exchange commits between repositories
Push sends local commits to the remote. Fetch and pull bring remote information into your local repository.

2. Understanding Remote Names and URLs

When you clone a repository, Git normally creates a remote named origin. This is simply the default name given to the repository from which you cloned.

Check the configured remote repositories using:

Git — List Remote Repositories
git remote -v
origin https://github.com/company/banking-loan-service.git (fetch) origin https://github.com/company/banking-loan-service.git (push)

The output displays the remote name and its URLs. Git can use separate URLs for fetching and pushing.

Useful remote commands

Git — Remote Configuration
# Display configured remotes
git remote -v

# Display remote details
git remote show origin

# Add another remote
git remote add upstream https://github.com/original/banking-loan-service.git

# Change the origin URL
git remote set-url origin https://github.com/company/banking-loan-service.git

# Rename a remote
git remote rename upstream source

# Remove a remote reference
git remote remove source
Remember: Removing a remote with git remote remove removes its local configuration. It does not delete the repository hosted on GitHub.

3. Git Push — Upload Local Commits

The git push command sends commits from your local repository to a remote repository. This is how you share your completed work with other developers.

Example: Push a feature branch

Suppose you are implementing a loan eligibility feature. You create a branch, make changes and commit them locally.

Git — Create and Commit a Feature
# Create and switch to a feature branch
git switch -c feature/loan-eligibility

# Stage the changes
git add .

# Create a local commit
git commit -m "Add loan eligibility validation"

At this point, the commit exists in your local repository. It has not yet been uploaded to GitHub.

Push the branch to the remote repository:

Git — Push a New Branch
git push -u origin feature/loan-eligibility
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Writing objects: 100% (3/3), done. To https://github.com/company/banking-loan-service.git * [new branch] feature/loan-eligibility -> feature/loan-eligibility branch 'feature/loan-eligibility' set up to track 'origin/feature/loan-eligibility'.

What does -u mean?

The -u option is shorthand for --set-upstream. It configures the local branch to track the corresponding remote branch.

After setting the upstream branch, you can normally use the shorter commands:

Git — Subsequent Pushes
git push

Git knows which remote branch to use based on the upstream configuration.

Common push variations

Git — Push Commands
# Push the current branch to its upstream
git push

# Push a specific branch
git push origin feature/loan-eligibility

# Push and configure upstream tracking
git push -u origin feature/loan-eligibility

# Push all local branches
git push --all origin

# Delete a remote branch
git push origin --delete feature/loan-eligibility
Be careful with force push. A normal push is rejected when it would overwrite remote history. Avoid using git push --force on shared branches because it can discard commits that other developers have published. If rewriting history is necessary, understand the impact and prefer --force-with-lease where appropriate.

4. Git Fetch — Download Remote Updates

The git fetch command downloads new commits, branch information and other relevant references from a remote repository. It updates your remote-tracking references but does not automatically merge those changes into your current local branch.

This is useful when you want to inspect your teammates' changes before integrating them.

Example: Fetch the latest changes

Git — Fetch from Origin
git fetch origin
From https://github.com/company/banking-loan-service 8f31a20..b72c9d1 main -> origin/main * [new branch] feature/audit-log -> origin/feature/audit-log

Git has downloaded the latest information and updated the remote-tracking references. Your current local branch has not been merged with these updates.

Inspect fetched changes

Git — Compare Local and Remote State
# Show local and remote-tracking branches
git branch -a

# Compare current branch with remote main
git diff HEAD origin/main

# Show commits available remotely but not locally
git log HEAD..origin/main --oneline

# Show commits in either side that differ
git log --oneline --left-right HEAD...origin/main

The expression origin/main refers to your local remote-tracking reference for the remote's main branch. It is not a live connection to GitHub; it reflects the most recent information fetched from that remote.

Fetch a specific branch

Git — Fetch Specific Branch
git fetch origin main

This fetches the specified branch from the remote and updates the corresponding remote-tracking reference. It does not automatically switch your current branch or merge the changes.

5. Git Pull — Fetch and Integrate Changes

The git pull command retrieves changes from a remote repository and integrates them into your current local branch.

Conceptually, a pull performs two operations:

  1. Fetch updates from the remote repository.
  2. Integrate the fetched updates into the current branch, using the configured pull strategy.

With the default merge strategy, Git performs a fetch followed by a merge.

Git — Pull Latest Changes
# Pull from the configured upstream branch
git pull

# Pull from a specific remote and branch
git pull origin main
From https://github.com/company/banking-loan-service * branch main -> FETCH_HEAD Updating 8f31a20..b72c9d1 Fast-forward LoanService.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)

This illustrative output shows a fast-forward update. Git moved the current branch forward because the remote branch contained commits that followed the current local branch history.

Pull with merge or rebase

Git supports different ways to integrate fetched changes.

Git — Choose an Integration Strategy
# Fetch and merge
git pull --no-rebase

# Fetch and rebase local commits
git pull --rebase

# Fetch without integrating
git fetch origin

With merge, Git combines the histories and may create a merge commit. With rebase, Git replays your local commits on top of the updated upstream branch.

Team convention matters: Some teams prefer merge commits, while others prefer rebasing feature branches. Follow the repository's contribution guidelines. Avoid rebasing commits that other developers already depend on unless the team has agreed to it.

6. Push vs Pull vs Fetch — Key Differences

Command Direction What it does Integrates into current branch?
push Local → Remote Uploads local commits. No
fetch Remote → Local Downloads remote updates and updates remote-tracking references. No
pull Remote → Local Fetches and integrates remote changes. Yes

7. Visualising the Push, Fetch and Pull Workflow

Consider a local branch and a remote branch that are initially in sync. The developer creates a commit locally, while a teammate publishes another commit remotely.

How Remote Operations Change Repository State
1. PUSH — Publish your local commit Local: A → B Push commit B Remote: A → B 2. FETCH — Download remote information Local branch: A Fetch commit C Remote: A → C 3. PULL — Fetch and integrate Local: A → C Fetch + integrate Remote: A → C
Simplified illustration: actual commit histories may diverge, contain multiple commits or require a merge or rebase.

8. Remote-Tracking Branches Explained

When you fetch from origin, Git updates references such as origin/main and origin/feature/loan-eligibility.

These are called remote-tracking branches. They represent the last fetched state of branches on the remote repository.

Git — View Local and Remote Branches
# Local branches
git branch

# Remote-tracking branches
git branch -r

# Both local and remote-tracking branches
git branch -a

For example, you might see:

* main feature/loan-eligibility remotes/origin/main remotes/origin/feature/audit-log

The local main branch and origin/main are separate references. Fetching updates the remote-tracking reference; it does not automatically move your local branch.

9. Handling Common Remote Operation Problems

Problem 1: Push rejected because the remote contains new commits

This commonly happens when another developer pushes commits to the same branch before you push your own work. Your local branch is now behind or has diverged from the remote branch.

Git — Typical Recovery
# Fetch and integrate remote changes
git pull --rebase

# Resolve conflicts if Git reports any
# Then continue the rebase when required
git rebase --continue

# Push your updated branch
git push

If conflicts occur, edit the affected files, stage the resolved files using git add, and continue the rebase. If the rebase becomes confusing, you can cancel it with git rebase --abort.

Problem 2: Authentication failed

GitHub does not accept your account password for Git operations over HTTPS. Depending on your setup, use a personal access token through a secure credential manager or configure SSH authentication.

Git — Inspect Remote URL
git remote -v
Do not place access tokens directly in Git remote URLs, source code or scripts. Use a credential manager or SSH keys and follow your organisation's security policy.

Problem 3: No upstream branch configured

If Git does not know which remote branch your local branch should track, configure it during the first push:

Git — Set Upstream
git push -u origin feature/loan-eligibility

Problem 4: Merge conflicts during pull

A conflict can occur when local and remote changes affect the same parts of a file and Git cannot automatically combine them.

Open the conflicting files, review both changes, edit the file to the intended final version, then stage the resolution. The remaining steps depend on whether you are merging or rebasing.

Git — Resolve a Merge Conflict
# Check which files have conflicts
git status

# After editing and resolving the files
git add LoanService.cs

# Complete the merge
git commit

When resolving a rebase conflict instead, use git rebase --continue rather than creating a merge commit.

A disciplined remote workflow helps avoid unnecessary conflicts and keeps your branch updated.

  1. Check your current branch and working tree.
  2. Fetch the latest remote information.
  3. Review the changes available on the remote.
  4. Integrate the relevant updates into your feature branch.
  5. Make and test your changes locally.
  6. Commit meaningful changes.
  7. Push your branch and open a pull request.
Git — Example Daily Commands
# Check the current state
git status

# Download remote updates
git fetch origin

# Switch to your feature branch
git switch feature/loan-eligibility

# Integrate the latest main branch
git rebase origin/main

# Stage and commit your changes
git add .
git commit -m "Improve loan eligibility validation"

# Publish your branch
git push
Important: The example assumes your team uses rebase to update feature branches. If your team follows a merge-based workflow, use the agreed merge process instead. Always check your working tree and resolve any existing changes before integrating remote updates.

11. Best Practices

  • Fetch regularly to stay aware of changes made by teammates.
  • Review remote changes before integrating them when working on sensitive features.
  • Use meaningful branch names and commit messages.
  • Push feature branches regularly so work is shared and backed up remotely.
  • Never force-push shared branches without team agreement.
  • Use pull requests for review instead of directly pushing unfinished work to protected branches.
  • Follow a consistent merge or rebase policy across the team.
  • Keep credentials and access tokens out of source code.

12. Hands-On Practice

Exercise: Publish and Synchronise a Feature Branch

Practise remote operations using a test repository. Do not experiment on a production repository.

  1. Create a repository on GitHub and clone it locally.
  2. Create a branch named feature/customer-audit.
  3. Add a file named audit-notes.md and commit it.
  4. Push the branch using git push -u origin feature/customer-audit.
  5. Make a separate change on GitHub, such as updating the README.
  6. Run git fetch origin and inspect the remote updates.
  7. Integrate the relevant changes using your chosen team strategy.
  8. Push your latest work and verify it on GitHub.

Learning outcome: You will understand how local and remote branches exchange commits and how fetch differs from pull.

13. Interview Questions and Answers

Beginner

1. What is the difference between git push and git pull?

Answer: Push uploads local commits to a remote repository. Pull downloads remote changes and integrates them into the current local branch.

Beginner

2. Does git fetch modify your current working files?

Answer: Fetch updates remote-tracking references and downloads objects. It does not automatically merge those changes into your current branch or modify its working files.

Intermediate

3. What is the purpose of the -u option in git push?

Answer: It sets the upstream tracking branch. Future push and pull commands can use that tracking relationship without requiring the remote and branch names each time.

Intermediate

4. What is origin/main?

Answer: It is a remote-tracking reference representing the main branch of the remote named origin as of the latest fetch. It is not the same as your local main branch.

Advanced

5. What is the difference between git pull and git pull --rebase?

Answer: A pull using the merge strategy integrates fetched changes through a merge. A pull with --rebase replays local commits on top of the updated upstream branch. The resulting history can differ, so teams should agree on a workflow.

14. Conclusion

Git remote operations are essential for everyday team development. Push helps you publish your work, fetch lets you inspect remote updates safely, and pull helps you integrate those updates into your current branch.

Understanding the difference between these commands, remote-tracking branches and upstream configuration will help you collaborate confidently and avoid common repository problems.

Practise these commands with a test repository until the workflow becomes part of your daily development routine.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.